# Getting incorrect output in finding the maximum pair sum in the given array.

#### Shruti Magar

Hello, I am Shruti Magar.
I was solving a question using Java in which we have to find and print the maximum pair sum in the given array. For example: The input array is {23,54,30,7,15,18} and the sum of pairs is (23 + 54 = 77), (23 + 30 = 53), (23 + 7 =30), ... ,upto (15 + 18 = 33). The maximum pair sum is (54 + 30 = 84) but I am getting output as 2147483647. I am trying to find where my code is going wrong and looking forward to suggestions on this. I have included my code below. Thankyou.

Code:
``````public class MaxPairSum {

static int findMaxSum(int a[], int n) {

int maxSum = Integer.MAX_VALUE;

for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
int sum=a[i]+a[j];

if(sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}

public static void main(String[] args) {
int a[]= {23,54,30,7,15,18};
int n=a.length;
System.out.println("Maximum pair sum : "+ findMaxSum(a,n));
}
}``````

#### WhiteCube

Initialise maxSum to a low value.

The maximum pair sum, is the sum of the 2 largest numbers. You don't need a nested loop for that.

Find the maximum, remove it, find the maximum again.

#### Shruti Magar

Initialise maxSum to a low value.

The maximum pair sum, is the sum of the 2 largest numbers. You don't need a nested loop for that.

Find the maximum, remove it, find the maximum again.

Thanks for the suggestion. I set maxSum value to 1 (small value) and got the correct answer. Also by using one for loop and `int sum=a[i]+a[i+1]; ` the program is working.

#### FResher

Hello !

one other option is to use "bubble sort" to have an array from the Min value to the Max value,
then : add result_tab[max_slot] + result_tab[max_slot -1]

look at this explanation : https://www.geeksforgeeks.org/bubble-sort/
with code as feature

#### Shruti Magar

Hello !

one other option is to use "bubble sort" to have an array from the Min value to the Max value,
then : add result_tab[max_slot] + result_tab[max_slot -1]

look at this explanation : https://www.geeksforgeeks.org/bubble-sort/
with code as feature

Thanks for the solution. I tried to sort the array using bubble sort and then did `result[max_num] + result[max_num - 1]` and it is giving the correct maximum sum in the output.

#### Shruti Magar

Thanks for the solution. I tried to sort the array using bubble sort and then did
Code:
``result[maxnum] + result[maxnum - 1]``
and it is giving the correct maximum sum in the output.

#### FResher

do you have understand the error in your first code try ?

in it, you fetch the array from first index to last, by adding one value with the value aside. [ i ] + [ i + 1 ]
but without sorting the array,

you were on the risk about index i outgoing array limit.
and , that the case in your first code, the max value ( but without sorting ) was aside the second max value...
kind of cheat, but for a random good result. irrelevant.

with the bubble sort, you make ready the values array, to make a good addition of the two value seek.

#### Shruti Magar

Yes sorting will give us a sorted array and then we can find the pair sum.

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.