Find pair in array with maximum sum using recursion Not sure the best way to solve this using recursion. Below is what I have tried so far:
public static void findSum(int[] a, int sum, int i, int j) {

    if (i == a.length)
        return;
    if (j == a.length)
        return;

    sum = (a[i] + a[j] &gt; sum) ? (a[i] + a[j]) : sum;
    System.out.println(&quot;Sum:&quot; + sum + &quot;, i:&quot; + i + &quot;,j:&quot; + j);

    findSum(a, sum, i, j + 1);
    i++;
    j = i;
    findSum(a, sum, i, j + 1);

    return;
}

Given input : int[] a = {5, 4, 7, 3, 9, 2}
Expected output : 7+9 -&gt; 16
Plus I am getting many number of iterations which must be reduced.
Notice that the answer is the sum of the two greatest elements in the array. You only need one pass to find both of them.
The idea is that you go from through the array and keep track of top 2 elements in the array. The code will be simplier if the function requires to be called on a valid input data (an array of length &gt;= 2) and first two elements already compared. The function then takes the next element of the array, updates the top 2 pair and moves onto the next element.
I assume you have to use recursion, but the code could be easily rewritten using a loop.
public static void findSum(int[] a, int greatest, int secondGreatest, int currentIndex) {
    if (currentIndex == a.length) {
        System.out.println(&quot;Sum:&quot; + (a[greatest] + a[secondGreatest]) + &quot;, i:&quot;+greatest+&quot;,j:&quot;+secondGreatest);
        return;
    }
    if (a[currentIndex] &gt; a[greatest]) {
        // found the new top; the current top becomes the second
        secondGreatest = greatest;
        greatest = currentIndex;
    } else if (a[currentIndex] &gt; a[secondGreatest]) {
        // found the new second-greatest, but so the current top stays the same
        secondGreatest = currentIndex;
    }
    findSum(a, greatest, secondGreatest, currentIndex + 1);
}

And the usage would be:
if (a.length &gt;= 2) {
    // Rank the first two elements in the array and initiate the recursion
    int greatest = a[0] &gt; a[1] ? 0 : 1;
    int secondGreatest = greatest == 0 ? 1 : 0;
    findSum(a, greatest, secondGreatest, 2);
}