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++
#include <bits/stdc++.h>
using namespace std;
void minSet(vector< int >& nums)
{
vector< int > A;
sort(nums.begin(), nums.end());
int sum = 0;
for ( int i = 0; i < nums.size(); i++)
sum += nums[i];
int temp = 0;
for ( int i = nums.size() - 1;
i >= 0 && temp <= sum / 2;
i--) {
A.push_back(nums[i]);
temp += nums[i];
}
for ( int i = 0; i < A.size(); i++)
cout << A[i] << " " ;
}
int main()
{
vector< int > vec
= { 7, 6, 13, 13, 12, 11 };
minSet(vec);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void minSet(ArrayList<Integer> nums) {
ArrayList<Integer> A = new ArrayList<Integer> ();
Collections.sort(nums);
int sum = 0 ;
for ( int i = 0 ; i<nums.size(); i++)
sum += nums.get(i);
int temp = 0 ;
for ( int i = nums.size() - 1 ;
i >= 0 && temp<= sum / 2 ;
i--) {
A.add(nums.get(i));
temp += nums.get(i);
}
for ( int i = 0 ; i<A.size(); i++)
System.out.print(A.get(i) + " " );
}
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 );
minSet(gfg);
}
}
|
Python3
def minSet(nums) :
A = []
nums.sort()
sum = 0
for i in range ( 0 , len (nums)):
sum + = nums[i]
temp = 0
for i in range ( len (nums) - 1 , - 1 , - 1 ):
if (temp > sum / 2 ):
break
A.append(nums[i])
temp + = nums[i]
for i in range ( 0 , len (A)):
print (A[i], end = ' ' )
vec = [ 7 , 6 , 13 , 13 , 12 , 11 ]
minSet(vec);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void minSet(List< int > nums) {
List< int > A = new List< int > ();
nums.Sort();
int sum = 0;
for ( int i = 0; i < nums.Count; i++)
sum += nums[i];
int temp = 0;
for ( int i = nums.Count - 1;
i >= 0 && temp<= sum / 2;
i--) {
A.Add(nums[i]);
temp += nums[i];
}
for ( int i = 0; i<A.Count; i++)
Console.Write(A[i] + " " );
}
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);
minSet(gfg);
}
}
|
Javascript
<script>
function minSet(nums)
{
var A = [];
nums.sort((a,b)=>a-b);
var sum = 0;
for ( var i = 0; i < nums.length; i++)
sum += nums[i];
var temp = 0;
for ( var i = nums.length - 1;
i >= 0 && temp <= sum / 2;
i--) {
A.push(nums[i]);
temp += nums[i];
}
for ( var i = 0; i < A.length; i++)
document.write( A[i] + " " );
}
var vec
= [7, 6, 13, 13, 12, 11];
minSet(vec);
</script>
|
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!
Last Updated :
19 May, 2021
Like Article
Save Article