Fractional knapsack implementation in java (But not correct answer) - java

I am having problems with a fractional knapsack implementation in java. I am not getting the correct answer.
Here's my code:
import java.util.Scanner;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import static java.lang.Integer.min;
public class FractionalKnapsack {
// this method for calculating the maximum index
public static int select_max_index(int[] values, int[] weights, int n) {
int index = -1;
double max = 0;
for (int i = 1; i <= n; i++) {
if (weights[i] > 0 && (double) values[i] / (double) weights[i] > max) {
max = (double) values[i] / (double) weights[i];
index = i;
}
}
return index;
}
private static double getOptimalValue(int capacity, int[] values, int[] weights, int n) {
// fractional knapsack problem
DecimalFormat df = new DecimalFormat("#.####"); // for getting the decimal point upto 4 digits
df.setRoundingMode(RoundingMode.CEILING);
int i;
double value = 0.0000d;
if (capacity == 0)
return value;
for (i = 0; i < n; i++) {
int max_index = select_max_index(values, weights, n);// call the maximum index
if (max_index >= 0) {
int b = min(capacity, (weights[max_index]));
value = value + b * ((double) values[max_index] / (double) weights[max_index]);
weights[i] = (weights[max_index] - b);
capacity = capacity - b;
}
}
return Double.parseDouble(df.format(value));
//return value;
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int capacity = scanner.nextInt();
int[] values = new int[n + 2];
int[] weights = new int[n + 2];
for (int i = 0; i < n; i++) {
values[i] = scanner.nextInt();
weights[i] = scanner.nextInt();
}
System.out.println(getOptimalValue(capacity, values, weights, n));
}
}
Inputs:
3 50
60 20
100 50
120 30
Correct output:
180.0000
my output:
200.0
I have followed all the steps. But there are 2 issues:
Rounding off up to 4 digits after point.
For some values there is a big gap between my answer and expected answer.

Is this knapsack with replacement? Usually in fractional knapsack once you choose an item you cannot choose it again. In your implementation of select_max_index it seems you always select the highest value/ratio which means you will always use it. In the case of your example:
first choice is value=120, weight 30 so there remains 20.
Then you select again value=120 but this time you take 2/3 of its value to fit which is 80
This is why you get 200.
You need to fix your code so you don't select the same item all the time. The easiest way is to use a priority queue with deleteMin or deleteMax

Related

Specific pattern as output with modulo operator

The task is to generate with one loop and the modulo-operator the following pattern.
12345
23451
34512
45123
51234
I do have a solution but i think it is not solved in a good way.
So i am looking for a more elegant way to solve the problem, without hurting the requirements.
class Test{
public static void main(String[] args){
int num = 12345;
System.out.println(num);
for(int i = 0; i < 4; i++){
int tmp = num%10000+21106+i;
System.out.println(tmp);
}
}
}
You need to extract the first (most significant) digit with / 10000 and the remaining digits with % 10000. Then you can construct the next number in the series.
public static void main(String[] args){
int num = 12345;
System.out.println(num);
for(int i = 0; i < 4; i++) {
int first = num / 10000;
int last4 = num % 10000;
num = last4 * 10 + first;
System.out.println(num);
}
}
Eran's answer is correct, but hardcodes the order of the number (it will only work for numbers in the range 10000-99999). But, we can generalize it by noting that the length of a number (in decimal) is equal to the log10 of the number. Something like
int num = 123456;
System.out.println(num);
int log10 = (int) Math.log10(num);
for (int i = 0; i < log10; i++) {
int pow10 = (int) Math.pow(10, log10);
int first = num / pow10;
int last4 = num % pow10;
num = last4 * 10 + first;
System.out.println(num);
}

Adding numbers which are stored in string variables

Given two non-negative numbers num1 and num2 represented as strings, return the sum of num1 and num2.
The length of both num1 and num2 is less than 5100.
Both num1 and num2 contain only digits 0-9.
Both num1 and num2 do not contain any leading zeros.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
I tried my solution but it doesn't work. Suggestions?
public class Solution {
public String addStrings(String num1, String num2) {
double multiplier = Math.pow(10, num1.length() - 1);
int sum = 0;
for (int i = 0; i < num1.length(); i++){
sum += ((((int) num1.charAt(i)) - 48) * multiplier);
multiplier /= 10;
}
multiplier = Math.pow(10, num2.length() - 1);
for (int i = 0; i < num2.length(); i++){
sum += ((((int) num2.charAt(i)) - 48) * multiplier);
multiplier /= 10;
}
return "" + sum;
}
}
You must not use any built-in BigInteger library or convert the inputs to integer directly.
Note that you are adding two integers of up to 5100 digits each. That is not that max value, but the max number of digits.
An int (your sum variable) cannot hold values like that. BigInteger can, but you're not allowed to use it.
So, add the numbers like you would on paper: Add last digits, write lower digit of the sum as last digit of result, and carry-over a one if needed. Repeat for second-last digit, third-last digit, etc. until done.
Since the sum will be at least the number of digits of the longest input value, and may be one longer, you should allocate a char[] of length of longest input plus one. When done, construct final string using String(char[] value, int offset, int count), with an offset of 0 or 1 as needed.
The purpose of this question is to add the numbers in the string form. You should not try to convert the strings to integers. The description says the length of the numbers could be up to 5100 digits. So the numbers are simply too big to be stored in integers and doubles. For instance In the following line:
double multiplier = Math.pow(10, num1.length() - 1);
You are trying to store 10^5100 in a double. In IEEE 754 binary floating point standard a double can a store number from ±4.94065645841246544e-324 to ±1.79769313486231570e+308. So your number won't fit. It will instead turn into Infinity. Even if it fits in double it won't be exact and you will encounter some errors in your follow up calculations.
Because the question specifies not to use BigInteger or similar libraries you should try and implement string addition yourself.
This is pretty straightforward just implement the exact algorithm you follow when you add two numbers on paper.
Here is working example of adding two strings without using BigInteger using char array as intermediate container. The point why double can't be used has been explained on #Tempux answer. Here the logic is similar to how adding two numbers on paper works.
public String addStrings(String num1, String num2) {
int carry = 0;
int m = num1.length(), n = num2.length();
int len = m < n ? n : m;
char[] res = new char[len + 1]; // length is maxLen + 1 incase of carry in adding most significant digits
for(int i = 0; i <= len ; i++) {
int a = i < m ? (num1.charAt(m - i - 1) - '0') : 0;
int b = i < n ? (num2.charAt(n - i - 1) - '0') : 0;
res[len - i] = (char)((a + b + carry) % 10 + '0');
carry = (a + b + carry) / 10;
}
return res[0] == '0' ? new String(res, 1, len) : new String(res, 0, len + 1);
}
This snippet is relatively small and precise because here I didn't play with immutable String which is complicated/messy and yield larger code. Also one intuition is - there is no way of getting larger output than max(num1_length, num2_length) + 1 which makes the implementation simple.
You have to addition as you do on paper
you can't use BigInteger and the String Length is 5100, so you can not use int or long for addition.
You have to use simple addition as we do on paper.
class AddString
{
public static void main (String[] args) throws java.lang.Exception
{
String s1 = "98799932345";
String s2 = "99998783456";
//long n1 = Long.parseLong(s1);
//long n2 = Long.parseLong(s2);
System.out.println(addStrings(s1,s2));
//System.out.println(n1+n2);
}
public static String addStrings(String num1, String num2) {
StringBuilder ans = new StringBuilder("");
int n = num1.length();
int m = num2.length();
int carry = 0,sum;
int i, j;
for(i = n-1,j=m-1; i>=0&&j>=0;i--,j--){
int a = Integer.parseInt(""+num1.charAt(i));
int b = Integer.parseInt(""+num2.charAt(j));
//System.out.println(a+" "+b);
sum = carry + a + b;
ans.append(""+(sum%10));
carry = sum/10;
}
if(i>=0){
for(;i>=0;i--){
int a = Integer.parseInt(""+num1.charAt(i));
sum = carry + a;
ans.append(""+(sum%10));
carry = sum/10;
}
}
if(j>=0){
for(;j>=0;j--){
int a = Integer.parseInt(""+num2.charAt(j));
sum = carry + a;
ans.append(""+(sum%10));
carry = sum/10;
}
}
if(carry!=0)ans.append(""+carry);
return ans.reverse().toString();
}
}
You can run the above code and see it works in all cases, this could be written in more compact way, but that would have been difficult to understand for you.
Hope it helps!
you can use this one that is independent of Integer or BigInteger methods
public String addStrings(String num1, String num2) {
int l1 = num1.length();
int l2 = num2.length();
if(l1==0){
return num2;
}
if(l2==0){
return num1;
}
StringBuffer sb = new StringBuffer();
int minLen = Math.min(l1, l2);
int carry = 0;
for(int i=0;i<minLen;i++){
int ind = l1-i-1;
int c1 = num1.charAt(ind)-48;
ind = l2-i-1;
int c2 = num2.charAt(ind)-48;
int add = c1+c2+carry;
carry = add/10;
add = add%10;
sb.append(add);
}
String longer = null;
if(l1<l2){
longer = num2;
}
else if(l1>l2){
longer = num1;
}
if(longer!=null){
int l = longer.length();
for(int i=minLen;i<l;i++){
int c1 = longer.charAt(l-i-1)-48;
int add = c1+carry;
carry = add/10;
add = add%10;
sb.append(add);
}
}
return sb.reverse().toString();
}
The method takes two string inputs representing non-negative integers and returns the sum of the integers as a string. The algorithm works by iterating through the digits of the input strings from right to left, adding each digit and any carryover from the previous addition, and appending the resulting sum to a StringBuilder. Once both input strings have been fully processed, any remaining carryover is appended to the output string. Finally, the string is reversed to produce the correct output order.
Hope this will solve the issue.!
public string AddStrings(string num1, string num2)
{
int i = num1.Length - 1, j = num2.Length - 1, carry = 0;
StringBuilder sb = new StringBuilder();
while (i >= 0 || j >= 0 || carry != 0) {
int x = i >= 0 ? num1[i--] - '0' : 0;
int y = j >= 0 ? num2[j--] - '0' : 0;
int sum = x + y + carry;
sb.Append(sum % 10);
carry = sum / 10;
}
char[] chars = sb.ToString().ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
Previous solutions have excess code. This is all you need.
class ShortStringSolution {
static String add(String num1Str, String num2Str) {
return Long.toString(convert(num1Str) + convert(num2Str));
}
static long convert(String numStr) {
long num = 0;
for(int i = 0; i < numStr.length(); i++) {
num = num * 10 + (numStr.charAt(i) - '0');
}
return num;
}
}
class LongStringSolution {
static String add(String numStr1, String numStr2) {
StringBuilder result = new StringBuilder();
int i = numStr1.length() - 1, j = numStr2.length() - 1, carry = 0;
while(i >= 0 || j >= 0) {
if(i >= 0) {
carry += numStr1.charAt(i--) - '0';
}
if(j >= 0) {
carry += numStr2.charAt(j--) - '0';
}
if(carry > 9) {
result.append(carry - 10);
carry = 1;
} else {
result.append(carry);
carry = 0;
}
}
if(carry > 0) {
result.append(carry);
}
return result.reverse().toString();
}
}
public class Solution {
static String add(String numStr1, String numStr2) {
if(numStr1.length() < 19 && numStr2.length() < 19) {
return ShortStringSolution.add(numStr1, numStr2);
}
return LongStringSolution.add(numStr1, numStr2);
}
}
For the sake of comprehension of the question
your method's name is addition
you are trying to do a power operation but the result is stored in a variable named multiplication...
there is more than one reason why that code doesnt work...
You need to do something like
Integer.parseInt(string)
in order to parse strings to integers
here the oficial doc

Erroneous behaviour of Java 8 Stream.sum()

Today while solving this question on HackerRank I used Array stream .sum() function to sum all the entries and proceeded with my algorithm. But for sum reason I found that my algorithm fails for some cases. I used diff to find out it passes 99% cases and for 1% the output is nearly equal but is less than the original answer. That's why I replaced the stream .sum() with a for loop and unexpectedly it passed all the test cases. I tried but couldn't ascertain this uncertain behaviour.
My implementation using stream.sum() :
public class MandragoraForest {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
for (int i = in.nextInt(); i > 0; i--) {
int number = in.nextInt();
int[] h = new int[number];
for (int j = 0; j < number; j++) h[j] = in.nextInt();
System.out.println(new MandragoraForestSolver().solve(h));
}
}
}
class MandragoraForestSolver {
public long solve(int[] h) {
if (h.length==1) return h[0];
Arrays.parallelSort(h);
long sum = Arrays.stream(h)
.sum();
long ans = -1;
for (long i=0, strength = 2; i<h.length; i++, strength++) {
sum -= h[(int)i];
ans = Math.max(ans, strength * sum);
}
return ans;
}
}
Implementation without Java stream :
public class MandragoraForest {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
for (int i = in.nextInt(); i > 0; i--) {
int number = in.nextInt();
int[] h = new int[number];
long sum = 0;
for (int j = 0; j < number; j++) {
h[j] = in.nextInt();
sum += h[j];
}
System.out.println(new MandragoraForestSolver().solve(h, sum));
}
}
}
class MandragoraForestSolver {
public long solve(int[] h, long sum) {
if (h.length==1) return h[0];
Arrays.parallelSort(h);
long ans = -1;
for (long i=0, strength = 2; i<h.length; i++, strength++) {
sum -= h[(int)i];
ans = Math.max(ans, strength * sum);
}
return ans;
}
}
Is there something that I'am missing out ? What could be the reason for this behaviour?
There is one significant difference between using a stream and a loop - the possibility of arithmetic overflow.
Arrays.stream(int[]) returns an IntStream, whose sum() method returns an int result. If the sum exceeds Integer.MAX_VALUE, a silent integer overflow will occur.
However your loop sums by adding int values to a long total, which would not suffer from arithmetic overflow.
The sum of integers in one of the tests must exceed Integer.MAX_VALUE, testing that a long is used to (correctly) calculate the total.
If you want to use a stream to sum, you need to convert the IntStream to a LongStream, which you can do like this:
long sum = Arrays.stream(big).asLongStream().sum();

Whats wrong in this Binomial Theorem calculator?

After thinking about for 1 hour I am still not able to figure out whats the problem with my calculator. I have made 3 function which include main(), calculateBinomialTheorem() and factorial(). Yes, factorial() to calculate the coefficient.
public static void main(String[] args) {
Scanner a_input = new Scanner(System.in);
Scanner b_input = new Scanner(System.in);
Scanner n_input = new Scanner(System.in);
int a = 0;
int b = 0;
int n = 0;
System.out.println("Welcome to Binomial Theorem Solver:");
System.out.print("a: ");
a = a_input.nextInt();
System.out.print("b: ");
b = b_input.nextInt();
System.out.print("n: ");
n = n_input.nextInt();
System.out.print(calculateBinomialTheorem(a, b, n));
a_input.close();
b_input.close();
n_input.close();
}
private static int calculateBinomialTheorem(int a, int b, int n) {
int result = 0;
int coefficient = 0;
ArrayList<Integer> products = new ArrayList<Integer>();
for(int i = 1; i <= n; i++) {
int product = 0;
coefficient = factorial(n) / (factorial(i) * factorial(n - i));
product = (int) (coefficient*Math.pow(a, n - i)*Math.pow(b, i));
products.add(product);
}
for(int c : products) {
result += c;
}
return result;
}
private static int factorial(int num) {
int factorial = 1;
if(num > 0) {
for ( int c = 1 ; c <= num ; c++ )
factorial = factorial*c;
} else {
return 0;
}
return factorial;
}
I tried to run it with the values of 3, 3, 3 that should give me the answer of 216 but its not giving! Why? Every time I run it with those values this is the error that I get:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at binomial_thorem_solver.Main.calculateBinomialTheorem(Main.java:46)
at binomial_thorem_solver.Main.main(Main.java:29)
I know that I am dividing the number by 0 but I am not getting how to resolve that issue.
Please help.
UPDATE: Thanks for the answers. You all figured out what the problem was but then there was another problem aswell that the loop was iterating one less time because i waas initially set to 1. I set that to 0 and it worked!
The problem is in your factorial method.. for 0 your factorial will return 0..
and you are dividing the value with the result of factorial (i.e. 0).. the factorial of 0 is 1. so your code should be
private static int factorial(int num) {
int factorial = 1;
if(num > 0) {
for ( int c = 1 ; c <= num ; c++ )
factorial = factorial*c;
} else {
return 1;
}
return factorial;
}
In the first iteration, i = 1, you have:
coefficient = factorial(n) / (factorial(i) * factorial(n - i));
What is factorial(1)? It's 1 according to your code.
What is dactorial(0)? It's 0 according to your code (if(num > 0) is false, so you go to else - there you return 0).
So, as the exception is telling you, you are trying to divide by zero.
How to fix this?
0! is defined to be 1. So you should check this special case and return 1 if the number is 0.
0! = 1 by convention. Not 0. This might cause problem to you.
Moreover, for loop should go from 0 to n, not from 1 to n as there are n+1 terms.
You are missing C(n,0)*a^0*b^n part as your iteration is not going from 0 to n.
So, your loop should be
for(int i = 0; i <= n; i++) {
int product = 0;
coefficient = factorial(n) / (factorial(i) * factorial(n - i));
product = (int) (coefficient*Math.pow(a, n - i)*Math.pow(b, i));
products.add(product);
}
In your case, since C(3,0)*3^0*3^3 that is 27 is missing from the final product. That is why you are getting 216 - 27 = 189.
You need to return 1 from the factorial functiom if num is zero.
factorial 0 equals 1.
if(num > 0) {
for ( int c = 1 ; c <= num ; c++ )
factorial = factorial*c;
} else {
return 1;
}

Dividing a number into m parts uniformly randomly

How do I divide a large positive integer n into m parts uniformly randomly.
Post-condition: Adding up all the m parts should give n.
Below is my attempt(in java like pseudocode), but I don't think it will give me uniformly random distribution.
I am first finding the average part avg by dividing n/m. Then I am generating m-1 random numbers which are around avg in magnitude(by alternately generating random numbers between 0 & avg, and *avg & 2*avg*. Then I am subtracting the sum of these m-1 numbers from original number n and setting that as the m'th part.
Assume that the function rand(x, y) returns a random number uniformly between x and y.
int[] divideUniformlyRandomly(int n, int m)
{
int[] res = new int[m];
int avg = n / m;
int sum = 0;
bool alternator = false;
for(int i = 0; i < m - 1; i++)
{
if(alternator == false)
{
res[i] = rand(0, avg);
alternator = true;
}
else
{
res[i] = rand(avg, 2*avg);
alternator = false;
}
sum += res[i];
}
res[m-1] = n - sum;
return res;
}
public double[] divideUniformlyRandomly(double number, int part) {
double uniformRandoms[] = new double[part];
Random random = new Random();
double mean = number / part;
double sum = 0.0;
for (int i=0; i<part / 2; i++) {
uniformRandoms[i] = random.nextDouble() * mean;
uniformRandoms[part - i - 1] = mean + random.nextDouble() * mean;
sum += uniformRandoms[i] + uniformRandoms[part - i -1];
}
uniformRandoms[(int)Math.ceil(part/2)] = uniformRandoms[(int)Math.ceil(part/2)] + number - sum;
return uniformRandoms;
}
You should divide n in m parts using m - 1 uniformy distributed fences. Your code could be :
int[] divideUniformlyRandomly(int n, int m)
{
int[] fences = new int[m-1];
for(int i = 0; i < m - 2; i++)
{
fences[i] = rand(0, n-1);
}
Arrays.sort(fences);
int[] result = new int[m];
result[0] = fences[0];
for(int i = 1; i < m - 2; i++)
{
result[i] = fences[i+1] - fences[i];
}
result[m-1] = n - 1 - fences[m-2];
return result;
}
To illustrate this :

Categories

Resources