- 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.
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));
}
}