05. Max Score from Subarray Mins
β GFG solution to the Max Score from Subarray Mins problem: find maximum sum of smallest and second smallest elements across all subarrays using optimized approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[]
of integers. Your task is to find the maximum sum of the smallest and second smallest elements across all subarrays (of size >= 2) of the given array.
A subarray is a contiguous sequence of elements within an array. For each subarray of size at least 2, we need to find the two smallest elements and calculate their sum. The goal is to find the maximum such sum across all possible subarrays.
π Examples
Example 1
Input: arr[] = [4, 3, 5, 1]
Output: 8
Explanation: All subarrays with at least 2 elements and find the two smallest numbers in each:
[4, 3] β 3 + 4 = 7
[4, 3, 5] β 3 + 4 = 7
[4, 3, 5, 1] β 1 + 3 = 4
[3, 5] β 3 + 5 = 8
[3, 5, 1] β 1 + 3 = 4
[5, 1] β 1 + 5 = 6
Maximum Score is 8.
Example 2
Input: arr[] = [1, 2, 3]
Output: 5
Explanation: All subarray with at least 2 elements and find the two smallest numbers in each:
[1, 2] β 1 + 2 = 3
[1, 2, 3] β 1 + 2 = 3
[2, 3] β 2 + 3 = 5
Maximum Score is 5
π Constraints
$2 \le \text{arr.size()} \le 10^5$
$1 \le \text{arr}[i] \le 10^6$
β
My Approach
The key insight is that we don't need to generate all subarrays. Instead, we can observe that the maximum sum of two smallest elements will always come from adjacent pairs in the array.
Basic Single Pass
Key Observation:
For any subarray of size > 2, adding more elements can only decrease or maintain the sum of two smallest elements.
The maximum sum will always be achieved by some adjacent pair in the array.
Algorithm:
Iterate through the array once.
For each adjacent pair
(arr[i], arr[i+1])
, calculate their sum.Track the maximum sum encountered.
Why This Works:
Consider subarray
[a, b, c]
wherea β€ b β€ c
.Sum of two smallest =
a + b
.But we already considered pair
[a, b]
which gives the same sum.Adding more elements never increases the sum of two smallest elements.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We iterate through the array once to check all adjacent pairs.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space to store the maximum sum and loop variables.
π§βπ» Code (C++)
class Solution {
public:
int maxSum(vector<int> &arr) {
int ans = 0;
for (int i = 1; i < arr.size(); i++)
ans = max(arr[i] + arr[i - 1], ans);
return ans;
}
};
π§βπ» Code (Java)
class Solution {
public int maxSum(int arr[]) {
int ans = 0;
for (int i = 1; i < arr.length; i++)
ans = Math.max(arr[i] + arr[i - 1], ans);
return ans;
}
}
π Code (Python)
class Solution:
def maxSum(self, arr):
ans = 0
for i in range(1, len(arr)):
ans = max(arr[i] + arr[i - 1], ans)
return ans
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated