Given an array arr, the task is to find a subarray of the array elements whose sum is strictly greater than the rest of the elements. The size of the subarray should be minimum and the sum should be maximum and it must be in non-increasing order.
Input: arr = [7, 6, 13, 12, 11]
Output: 13 12
The subarray [13, 12] and [13, 11] are minimal such that the sum of their elements is strictly greater than the rest of the elements. However the subarray [13, 12] has the maximum total sum of its elements and hence it is returned in non-increasing order.
Input: arr = 
- Initially we will sort the array arr and define a vector named A. Now we will first calculate the sum of whole array and then iterate the whole loop from the rear side while updating the temp consecutively. The loop runs until temp becomes zero.
- Actually what happens while iterating the loop from the rear side is that when we iterate the loop from the rear side we are considering the maximum values. Hence we can say that we can reach the target condition in a lesser amount of time and also by taking fewer variables.
- Now as loop runs we keep on updating the value of temp by just adding nums[i] to it and also adding nums[i] to the vector A. At last we return the vector A which represents the output result what we want.
Below is the implementation of the above approach:
13 13 12
Time Complexity: O(N * log N)
Auxiliary Space Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- First subarray having sum at least half the maximum sum of any subarray of size K
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Find maximum (or minimum) sum of a subarray of size k
- Maximum sum subarray of size K with sum less than X
- Maximum length of subarray such that sum of the subarray is even
- Size of The Subarray With Maximum Sum
- Maximum circular subarray sum of size K
- Maximum sum subarray of size range [L, R]
- Create an array of size N with sum S such that no subarray exists with sum S or S-K
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Maximum length of subarray such that all elements are equal in the subarray
- Maximum Unique Element in every subarray of size K
- Maximum average of a subarray of size of atleast X and atmost Y
- Maximum count number of valley elements in a subarray of size K
- Maximum number of Perfect Numbers present in a subarray of size K
- Find the subarray of size K with minimum XOR
- Maximize the minimum array element by M subarray increments of size S
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Count of subarray that does not contain any subarray with sum 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.