Algorithm to find pair with max sum from two arrays
A[0 ... n] and B[0 ... n]
A[i] + B[j] = max (A[i] + B[j], with i < j)
i,j < 99999; n < 99999
My best result is this:
int[] testArrayA = { 7, 1, 4, 5, 1};
int[] testArrayB = { 3, 2, 1, 5, 3};
String res = "";
int maxSum = 0;
for (int i = 0; i < testArrayA.length; i++)
{
int i1 = testArrayA[i];
for (int j = i; j < testArrayB.length; j++)
{
int j1 = testArrayB[j];
if((j1 + i1) > maxSum)
{
maxSum = j1 + i1;
res = i + " " + j;
}
}
}
System.out.println(res);
Expected answer "0 3" Current complexity \$O(n^2)\$ I think it must be much lower. Can we do it better?