Error
|
|
---|---|
@Ibrahim Nash | 5761 |
@blackshadows | 5701 |
@akhayrutdinov | 5111 |
@mb1973 | 4989 |
@Quandray | 4944 |
@saiujwal13083 | 4506 |
@sanjay05 | 3762 |
@marius_valentin_dragoi | 3516 |
@sushant_a | 3459 |
@verma_ji | 3341 |
@KshamaGupta | 3318 |
Complete Leaderboard | |
|
|
@aroranayan999 | 938 |
@bt8816103042 | 739 |
@SHOAIBVIJAPURE | 428 |
@codeantik | 412 |
@SherlockHolmes3 | 407 |
@neverevergiveup | 348 |
@mahlawatep | 347 |
@shalinibhataniya1097 | 343 |
@murarry3625 | 333 |
@saiujwal13083 | 326 |
@rohitanand | 314 |
Complete Leaderboard |
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
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes