public class Ques1{
public static int func(int i, int j, int ans, int[] arr){
if(j == arr.length-1){
return Math.max(ans,arr[j]);
}
if(i == arr.length-1){
return Math.max(ans,arr[i]);
}
int maxi = Math.max(ans,Math.max(arr[i],arr[j]));
System.out.println(maxi);
return func(i, j+1, maxi, arr);
}
public static void main(String[] args) {
int[] arr = {12,10,2,4,6,1,9,28};
int n = arr.length;
for(int i = 1; i < n-1; i++){
int ans = func(i-1, i, Integer.MIN_VALUE, arr);
}
}
}
While finding the largest value in a subsequence, between two indices i and j in an array, initially i=1 in the main function and in the defined function 'func', i=0 and j=1 and it continues, the answer is correct until the last step, where the value of j is the length of array - 1. I've tried debugging, the problem I'm facing is when the value of j increases to 7, and the length of array is 8, so instead of the first 'if' block proceeding, the value of j decrements back to 0 and the last return block executes, and then the function exits the loop and i starts from i=2. The answer should be 12, 12, 12, 12, 12, 12, 28, but the last answer is also 12 instead of 28.
The problem I'm facing is when the value of j increases to 7, and the length of array is 8, so instead of the first 'if' block proceeding, the value of j decrements back to 0 and the last return block executes, and then the function exits the loop and i starts from i=2. The answer should be 12, 12, 12, 12, 12, 12, 28, but the last answer is also 12 instead of 28.