Largest subset having with sum less than equal to sum of respective indices
Given an array arr[], the task is to find the length of the largest subset with the sum of elements less than or equal to the sum of its indexes(1-based indexing).
Examples:
Input: arr[] = {1, 7, 3, 5, 9, 6, 6}
Output: 5
Explanation:
Largest Subset is {1, 3, 5, 6, 6}
Sum of indexes = 1 + 3 + 4 + 6 + 7 = 21
Sum of elements = 1 + 3 + 5 + 6 + 6 = 21
Input: arr[] = {4, 1, 6, 7, 8, 2}
Output: 3
Naive Approach:
The simplest approach to solve the problem is to generate all possible subsets and calculate the length of the subsets which have the sum of elements less than or equal to the sum of its respective indices.
Time Complexity: O(N*2N)
Auxiliary Space: O(N)
Efficient Approach:
Follow the steps below to solve the problem:
- Iterate over all indices and consider only those indices whose values are greater than or equal to the values of the respective values stored in them.
- Keep updating the sum of the differences obtained in the above step.
- For the remaining elements, store their differences with their respective indexes. Sort the differences.
- Include elements into the subset one by one and subtract the difference from the sum. Keep including until an element is encountered whose difference with its index exceeds the remaining sum or all array elements have already been included.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSubset( int * a, int n)
{
int sum = 0;
int cnt = 0;
vector< int > v;
for ( int i = 1; i <= n; i++) {
if (a[i - 1] - i <= 0) {
sum += a[i - 1] - i;
cnt += 1;
}
else {
v.push_back(a[i - 1] - i);
}
}
sort(v.begin(), v.end());
int ptr = 0;
while (ptr < v.size()
&& sum + v[ptr] <= 0) {
cnt += 1;
ptr += 1;
sum += v[ptr];
}
return cnt;
}
int main()
{
int arr[] = { 4, 1, 6, 7,
8, 2 };
int n = sizeof (arr)
/ sizeof (arr[0]);
cout << findSubset(arr, n)
<< endl;
}
|
Java
import java.util.*;
class GFG{
public static int findSubset( int [] a, int n)
{
int sum = 0 ;
int cnt = 0 ;
Vector<Integer> v = new Vector<>();
for ( int i = 1 ; i <= n; i++)
{
if (a[i - 1 ] - i <= 0 )
{
sum += a[i - 1 ] - i;
cnt += 1 ;
}
else
{
v.add(a[i - 1 ] - i);
}
}
Collections.sort(v);
int ptr = 0 ;
while (ptr < v.size() &&
sum + v.get(ptr) <= 0 )
{
cnt += 1 ;
ptr += 1 ;
sum += v.get(ptr);
}
return cnt;
}
public static void main(String[] args)
{
int arr[] = { 4 , 1 , 6 , 7 , 8 , 2 };
int n = arr.length;
System.out.println(findSubset(arr, n));
}
}
|
Python3
def findSubset(a, n):
sum = 0
cnt = 0
v = []
for i in range ( 1 , n + 1 ):
if (a[i - 1 ] - i < = 0 ):
sum + = a[i - 1 ] - i
cnt + = 1
else :
v.append(a[i - 1 ] - i)
v.sort()
ptr = 0
while (ptr < len (v) and
sum + v[ptr] < = 0 ):
cnt + = 1
ptr + = 1
sum + = v[ptr]
return cnt
if __name__ = = "__main__" :
arr = [ 4 , 1 , 6 , 7 , 8 , 2 ]
n = len (arr)
print (findSubset(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int findSubset( int [] a, int n)
{
int sum = 0;
int cnt = 0;
List< int > v = new List< int >();
for ( int i = 1; i <= n; i++)
{
if (a[i - 1] - i <= 0)
{
sum += a[i - 1] - i;
cnt += 1;
}
else
{
v.Add(a[i - 1] - i);
}
}
v.Sort();
int ptr = 0;
while (ptr < v.Count &&
sum + v[ptr] <= 0)
{
cnt += 1;
ptr += 1;
sum += v[ptr];
}
return cnt;
}
public static void Main(String[] args)
{
int []arr = { 4, 1, 6, 7, 8, 2 };
int n = arr.Length;
Console.WriteLine(findSubset(arr, n));
}
}
|
Javascript
<script>
function findSubset(a, n)
{
let sum = 0;
let cnt = 0;
let v = [];
for (let i = 1; i <= n; i++)
{
if (a[i - 1] - i <= 0)
{
sum += a[i - 1] - i;
cnt += 1;
}
else
{
v.push(a[i - 1] - i);
}
}
v.sort();
let ptr = 0;
while (ptr < v.length &&
sum + v[ptr] <= 0)
{
cnt += 1;
ptr += 1;
sum += v[ptr];
}
return cnt;
}
let arr = [ 4, 1, 6, 7, 8, 2 ];
let n = arr.length;
document.write(findSubset(arr, n));
</script>
|
Time Complexity: O(N*log(N)), the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(N), an extra vector is used and in the worst case all elements will be stored inside it the hence algorithm takes up linear space
Last Updated :
02 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...