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: 8

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
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.





I am currently pursuing B Tech (Hons) in Computer Science I love technology and explore new things I want to become a very good programmer I love to travel and meet new peoples

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Article Tags :
Practice Tags :