There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls. The buckets n both roads are kept in such a way that they are sorted according to the number of balls in them. Geek starts from the end of the road which has the bucket with lower number of balls(i.e. if buckets are sorted in increasing order, then geek will start from the left side of road). The geek can change the road only at the point of intersection(which means , buckets with the same number of balls on two roads). Now you need to help Geek to collect the maximum number of balls.
Input:
1
5 5
1 4 5 6 8
2 3 4 6 9
Output: 29
Explanation:
The path with maximum sum is (2,3,4)5,6. Integers in [] are the buckets of first road and in () are the buckets of second road. So, max balls geek can collect is 29.
I have done this problem but recursing into two functions ,I'm a beginner in java,I have solved it almost 99% I just don't know how to return my final sum back to main. My code and output are as follows..
public class functionUse {
public static int array1(int []a,int[] b,int pos,int sum)
{
System.out.println("FUNCTION 1");
System.out.println("Sum : "+sum);
int i=pos,flag=0,rep=0;
while(rep==0&&i<a.length)
{
for( int j=0;j<a.length;j++)
{
flag=0;
if(a[i]==b[j]&&b[j]!=0)
{
flag=1;
rep=1;
pos=j+1;
b[j]=0;
break;
}
}
if(flag==0)
{
System.out.println("FUNCTION 1 INSIDE IF ");
sum=sum+a[i];
System.out.println("Sum : "+sum);
i++;
}
else
{
sum=sum+a[i];
System.out.println("FUNCTION 1 INSIDE ELSE ");
System.out.println("Sum : "+sum);
functionUse.array2(a,b,pos,sum);
}
}
return sum;
}
public static int array2(int[]a2,int[] b2,int pos,int sum)
{
System.out.println("FUNCTION 2");
System.out.println("Sum : "+sum);
int i=pos,flag=0,rep=0;
while (rep==0&&i<a2.length)
{
for(int j=0;j<a2.length;j++)
{
flag=0;
if(b2[i]==a2[j]&&a2[j]!=0)
{
flag=1;
pos=j+1;
rep=1;
a2[j]=0;
break;
}
}
if(flag==0)
{
System.out.println("FUNCTION 2 INSIDE IF ");
sum=sum+b2[i];
System.out.println("Sum : "+sum);
i++;
}
else
{
sum=sum+b2[i];
System.out.println("FUNCTION 2 INSIDE ELSE ");
System.out.println("Sum : "+sum);
functionUse.array1(a2,b2,pos,sum);
}
}
return sum;
}
public static void main(String[] args) {
int[]arr1={4,5,7,8,9};
int[] arr2= {1,4,6,8,9};
int sum1=0;
int sum2=0;
int lowest;
int pos=0;
int sum=0;
for(int i=0;i<arr1.length;i++)
{
sum1 = sum1+arr1[i];
sum2 = sum2+arr2[i];
}
lowest = Math.min(sum1, sum2);
if(lowest==sum1)
{
System.out.println(functionUse.array1(arr1,arr2,pos,sum));
}
else
{
System.out.println(functionUse.array2(arr1,arr2,pos,sum));
}
} }
For this input
int[]arr1={4,5,7,8,9};
int[] arr2= {1,4,6,8,9};
My output is as follows
FUNCTION 2 Sum : 0
FUNCTION 2 INSIDE IF Sum : 1
FUNCTION 2 INSIDE ELSE Sum : 5
FUNCTION 1 Sum : 5 //This answer is returned to main..I don't know why..
FUNCTION 1 INSIDE IF Sum : 10
FUNCTION 1 INSIDE IF Sum : 17
FUNCTION 1 INSIDE ELSE Sum : 25
FUNCTION 2 Sum : 25
FUNCTION 2 INSIDE ELSE Sum : 34
FUNCTION 1 Sum : 34 //This answer is perfect
Final Answer I'm getting is 5
First of all, a while loop in a recursion is kind of couter intuitive. Use the recursion, not a while if you can.
As M. Prokhorov said, you probably want to affect the result of
functionUse.array1(a2,b2,pos,sum);
if your lastelse
.Probably
sum = functionUse.array1(a2,b2,pos,sum);
will do.