# Minimum size Subarray with maximum sum in non-increasing order

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:

Approach:

1. 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.

2. 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.

3. 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 ` `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 nums) {`   `    ``ArrayList A = ``new` `ArrayList ();`   `    ``// sort numbers in descending order` `    ``Collections.sort(nums);`   `    ``// get total sum of the given array. ` `    ``int` `sum = ``0``;` `    ``for` `(``int` `i = ``0``; 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 gfg = ``new` `ArrayList ();` `    ``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 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

 ``

Output:

`13 13 12`

Time Complexity: O(N * log N)
Auxiliary Space Complexity: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next