Largest Pair Sum
Iterate the list and store the max element in J and add with the elements in the list to check if it greater than SUM.
public class LargestPairSum {
public static void main(String args[]) {
int arr[] = {-1, 3, 5, -6, 8, -9, 10, 4, -2, 7};
largestPairSum(arr, arr.length);
}
private static int largestPairSum(int[] arr, int n) {
int j = 0;
int max = n == 1 ? arr[0] + arr[1] : arr[0];
for (int i = 0; i < n; i++) {
int sum = arr[j] + arr[i];
if (sum > max) {
max = sum;
if (arr[j] < arr[i]) {
j = i;
}
}
}
System.out.println("Max--" + max);
return max;
}
}
Comments
Post a Comment