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.
Examples:
Input: arr = [7, 6, 13, 12, 11]
Output: 13 12
Explanation:
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 = [8]
Output: 8
Approach:
- 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:
C++
// C++ program to find the Minimum size Subarray // with maximum sum in non-increasing order #include <bits/stdc++.h> using namespace std; // Function to find the Minimum size Subarray void minSet(vector< int >& nums) { vector< int > A; // sort numbers in descending order sort(nums.begin(), nums.end()); // get total sum of the given array. int sum = 0; for ( int i = 0; i < nums.size(); i++) sum += nums[i]; int temp = 0; // Loop until the sum of numbers // is greater than sum/2 for ( int i = nums.size() - 1; i >= 0 && temp <= sum / 2; i--) { A.push_back(nums[i]); temp += nums[i]; } // Print the Minimum size Subarray for ( int i = 0; i < A.size(); i++) cout << A[i] << " " ; } // Driver code int main() { vector< int > vec = { 7, 6, 13, 13, 12, 11 }; minSet(vec); return 0; } |
Java
// Java program to find the Minimum size Subarray // with maximum sum in non-increasing order import java.util.*; class GFG { // Function to find the Minimum size Subarray static void minSet(ArrayList<Integer> nums) { ArrayList<Integer> A = new ArrayList<Integer> (); // sort numbers in descending order Collections.sort(nums); // get total sum of the given array. int sum = 0 ; for ( int i = 0 ; i<nums.size(); i++) sum += nums.get(i); int temp = 0 ; // Loop until the sum of numbers // is greater than sum/2 for ( int i = nums.size() - 1 ; i >= 0 && temp<= sum / 2 ; i--) { A.add(nums.get(i)); temp += nums.get(i); } // Print the Minimum size Subarray for ( int i = 0 ; i<A.size(); i++) System.out.print(A.get(i) + " " ); } // Driver Code public static void main(String[] args) { ArrayList<Integer> gfg = new ArrayList<Integer> (); gfg.add( 7 ); gfg.add( 6 ); gfg.add( 13 ); gfg.add( 13 ); gfg.add( 12 ); gfg.add( 11 ); // Function calling minSet(gfg); } } // This code is contributed by rutvik_56 |
Python3
# Python3 program to find the Minimum size Subarray # with maximum sum in non-increasing order # Function to find the Minimum size Subarray def minSet(nums) : A = [] # sort numbers in descending order nums.sort() # get total sum of the given array. sum = 0 for i in range ( 0 , len (nums)): sum + = nums[i] temp = 0 # Loop until the sum of numbers # is greater than sum/2 for i in range ( len (nums) - 1 , - 1 , - 1 ): if (temp > sum / 2 ): break A.append(nums[i]) temp + = nums[i] # Print the Minimum size Subarray for i in range ( 0 , len (A)): print (A[i], end = ' ' ) # Driver code vec = [ 7 , 6 , 13 , 13 , 12 , 11 ] minSet(vec); # This code is contributed by Sanjit_Prasad |
C#
// C# program to find the Minimum size Subarray // with maximum sum in non-increasing order using System; using System.Collections.Generic; class GFG { // Function to find the Minimum size Subarray static void minSet(List< int > nums) { List< int > A = new List< int > (); // sort numbers in descending order nums.Sort(); // get total sum of the given array. int sum = 0; for ( int i = 0; i < nums.Count; i++) sum += nums[i]; int temp = 0; // Loop until the sum of numbers // is greater than sum/2 for ( int i = nums.Count - 1; i >= 0 && temp<= sum / 2; i--) { A.Add(nums[i]); temp += nums[i]; } // Print the Minimum size Subarray for ( int i = 0; i<A.Count; i++) Console.Write(A[i] + " " ); } // Driver Code public static void Main(String[] args) { List< int > gfg = new List< int > (); gfg.Add(7); gfg.Add(6); gfg.Add(13); gfg.Add(13); gfg.Add(12); gfg.Add(11); // Function calling minSet(gfg); } } // This code is contributed by sapnasingh4991 |
Javascript
<script> // Javascript program to find the Minimum size Subarray // with maximum sum in non-increasing order // Function to find the Minimum size Subarray function minSet(nums) { var A = []; // sort numbers in descending order nums.sort((a,b)=>a-b); // get total sum of the given array. var sum = 0; for ( var i = 0; i < nums.length; i++) sum += nums[i]; var temp = 0; // Loop until the sum of numbers // is greater than sum/2 for ( var i = nums.length - 1; i >= 0 && temp <= sum / 2; i--) { A.push(nums[i]); temp += nums[i]; } // Print the Minimum size Subarray for ( var i = 0; i < A.length; i++) document.write( A[i] + " " ); } // Driver code var vec = [7, 6, 13, 13, 12, 11]; minSet(vec); </script> |
13 13 12
Time Complexity: O(N * log N)
Auxiliary Space Complexity: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!