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)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find maximum (or minimum) sum of a subarray of size k
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Size of The Subarray With Maximum Sum
- Find the subarray of size K with minimum XOR
- Maximum circular subarray sum of size K
- 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
- Smallest subarray containing minimum and maximum values
- Minimum and Maximum of all subarrays of size K using Map
- Sum of minimum and maximum elements of all subarrays of size k.
- Minimum partitions of maximum size 2 and sum limited by given value
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Product of all Subsequences of size K except the minimum and maximum Elements
- Maximum length of subarray such that sum of the subarray is even
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.