Minimum size Subarray with maximum sum in non-increasing order Last Updated : 19 May, 2021 Comments Improve Suggest changes Like Article Like Report 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> Output: 13 13 12 Time Complexity: O(N * log N) Auxiliary Space Complexity: O(N) Comment More infoAdvertise with us Next Article Minimum size Subarray with maximum sum in non-increasing order coder001 Follow Improve Article Tags : Mathematical Write From Home DSA Arrays subarray subarray-sum +2 More Practice Tags : ArraysMathematical Similar Reads Find maximum (or minimum) sum of a subarray of size k Given an array of integers and a number k, find the maximum sum of a subarray of size k. Examples: Input : arr[] = {100, 200, 300, 400}, k = 2Output : 700Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}, k = 4 Output : 39Explanation: We get maximum sum by adding subarray {4, 2, 10, 23} of size 4.Input 14 min read Maximize non decreasing Array size by replacing Subarray with sum Given an array arr[] of size N. In one operation only one subarray can be selected and replaced with the sum of the subarray. The task is to find the maximum size of the array after making it non-decreasing.Examples:Input: N = 5, arr[] = {5, 1, 6, 6, 6}Output: 4Explanation: maximum size non-decreasi 15+ min read Find Maximum Sum Strictly Increasing Subarray Given an array of positive integers. Find the maximum sum of strictly increasing subarrays. Note that this problem is different from maximum subarray sum and maximum sum increasing subsequence problems. Examples: Input : arr[] = {1, 2, 3, 2, 5, 1, 7}Output : 8Explanation : Some Strictly increasing s 7 min read Minimize the maximum subarray sum with 1s and -2s Given two integers X and Y. X and Y represent the frequency of elements 1 and -2 you have. You have to arrange all elements such that the maximum sum over all subarrays is minimized, and then find the maximum subarray sum of such rearrangement. Examples: Input: X = 1, Y = 1Output: 1Explanation: X = 5 min read Count maximum non-overlapping subarrays with given sum Given an array arr[] consisting of N integers and an integer target, the task is to find the maximum number of non-empty non-overlapping subarrays such that the sum of array elements in each subarray is equal to the target. Examples: Input: arr[] = {2, -1, 4, 3, 6, 4, 5, 1}, target = 6Output: 3Expla 7 min read Maximize product of subarray sum with its minimum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of subarray sum with the minimum element of that subarray. Examples: Input: arr[] = {3, 1, 6, 4, 5, 2}Output: 60Explanation:The required maximum product can be obtained using subarray {6, 4, 5}Therefore, 10 min read Maximum count of distinct sized subarrays with given sum Given a binary array arr[] of N integers, the task is to find the maximum count of distinct sized subarrays such that the sum of each subarray is K. Example: Input: arr[] = {0, 1, 1 , 0}, K = 2Output: 3Explanation: The subset {{0, 1, 1, 0}, {0, 1, 1}, {1, 1}} is the subset of 3 subarrays such that t 7 min read Maximum sum two non-overlapping subarrays of given size Given an array, we need to find two subarrays with a specific length K such that sum of these subarrays is maximum among all possible choices of subarrays. Examples: Input : arr[] = [2, 5, 1, 2, 7, 3, 0] K = 2 Output : 2 5 7 3 We can choose two arrays of maximum sum as [2, 5] and [7, 3], the sum of 12 min read Maximize the sum of maximum elements of at least K-sized subarrays Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examp 7 min read Maximize product of subarray sum with its maximum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray. Examples: Input: arr[] = {2, -3, 8, -2, 5}Output: 88Explanation:The required maximum product can be obtained using subarray {8, -2, 5}Therefo 8 min read Like