Max sum in sub-arrays | Practice | GeeksforGeeks

Given an array, find maximum sum of smallest and second smallest elements chosen from all possible sub-arrays. More formally, if we write all (nC2) sub-arrays of array of size >=2 and find the sum of smallest and second smallest, then our answer will be maximum sum among them.
 
Example 1:
Input : arr[] = [4, 3, 1, 5, 6]
Output : 11
Subarrays with smallest and 
second smallest are,
[4, 3]        
smallest = 3    second smallest = 4
[4, 3, 1]    
smallest = 1    second smallest = 3
[4, 3, 1, 5]    
smallest = 1    second smallest = 3
[4, 3, 1, 5, 6]    
smallest = 1    second smallest = 3
[3, 1]         
smallest = 1    second smallest = 3
[3, 1, 5]     
smallest = 1    second smallest = 3
[3, 1, 5, 6]    
smallest = 1    second smallest = 3
[1, 5]        
smallest = 1    second smallest = 5
[1, 5, 6]    
smallest = 1    second smallest = 5
[5, 6]         
smallest = 5    second smallest = 6
Maximum sum among all 
above choices is, 5 + 6 = 11
Input : arr[] = {5, 4, 3, 1, 6} 
Output : 9
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function pairWithMaxSum() which takes the array Arr[] and its size N as inputs and returns the maximum sum of smallest and second smallest elements chosen from all possible subarrays.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
2 ≤ N ≤ 105
1 ≤ A[i] ≤ 1018
0
prabhakarsati294262 weeks ago
 def pairWithMaxSum(self, arr, N):       # Your code goes here       if (N<2):           return -1       res = arr[0]+arr[1]       for i in range(1,N-1):           res = max(res,arr[i]+arr[i+1])       return res       
0
abhishekabhi73521 month ago
class Solution {      public static long pairWithMaxSum(long arr[], long N)   {       long max=0;       for(int i=0;i<N-1;i++)       {           if(arr[i]+arr[i+1]>max)           {               max=arr[i]+arr[i+1];           }       }              return max;   }}
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION WITHOUT STACK(1.6/7.8 SEC)
class Solution {      public static long pairWithMaxSum(long arr[], long N)   {       long max=0;       for(int i=0;i<N-1;i++)       {           if(arr[i]+arr[i+1]>max)           {               max=arr[i]+arr[i+1];           }       }              return max;   }}
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (1.9/7.8 SEC)
class Solution {      public static long pairWithMaxSum(long arr[], long N)   {       Stack<Long> stack=new Stack<Long>();       for(int i=0;i<N-1;i++)       {           stack.push(arr[i]+arr[i+1]);       }       long max=0;       for(long x : stack)       {           if(x>max)           {               max=x;           }       }       return max;   }}
0
deveshamway3 months ago
GFG has gone nuts...... why is this even a stack problem.
This should be put in “basic” section of “Array”.
 
SOL : 
 
long long pairWithMaxSum(long long arr[], long long N)   {       long long sum=0;       for(int i = 0 ;i<N-1;i++){           if(sum<(arr[i]+arr[i+1])){               sum=arr[i]+arr[i+1];           }       }       return sum;   }
+1
kartikeyashokgautam3 months ago
Java Solution using STACK :-
 
    public static long pairWithMaxSum(long arr[], long N)   {
      Stack<Long> stack = new Stack<Long>();            for(int i=0;i<N-1;i++)      {          stack.push(arr[i]+arr[i+1]);      }                       long max = stack.pop();                      int j=0;                     while(j< N-2)          {              max=Math.max(max,stack.peek());              stack.pop();              j++;          }       return max;   }
+1
kartikeyashokgautam3 months ago
JAVA Solution :- 
 
    public static long pairWithMaxSum(long arr[], long N)   {       // Your code goes here       long[]  brr = new long[(int)N] ;              for(int i =0;i<N-1;i++)       {           brr[i]=arr[i]+arr[i+1];       }              long max=brr[0];              for(int j =1;j<N-1;j++)       {          if(brr[j]>max)          {              long temp = max;              max=brr[j];              brr[j]=temp;          }       }       return max;          }
0
feyza3 months ago
    java
    
    public static long pairWithMaxSum(long arr[], long N)
    {
        long result = Integer.MIN_VALUE;
         Stack<Long> stack = new  Stack<>();
         for(int i=1; i<N; i++){
             stack.push(arr[i]+arr[i-1]);
             if(result < stack.peek())
                 result = stack.peek();
            stack.pop(); 
         }
         
         return result;
    }
+2
avinav26113 months ago
Easiest C++ Solution
 
+1
chessnoobdj4 months ago
C++
long long pairWithMaxSum(long long arr[], long long N)
    {
        long long ans = INT_MIN;
        for(long long i=0; i<N-1; i++)
            ans = max(ans, arr[i]+arr[i+1]);
        return ans;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.