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

Joined
Apr 6, 2023
Messages
21
Reaction score
0
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));
    }
}
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
short answer:

Initialise maxSum to a low value.

long answer:

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.
 
Joined
Apr 6, 2023
Messages
21
Reaction score
0
short answer:

Initialise maxSum to a low value.

long answer:

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.
 
Joined
Apr 6, 2023
Messages
21
Reaction score
0
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.
 
Joined
Apr 6, 2023
Messages
21
Reaction score
0
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.
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top