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

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 sum. Keep including until an element is encountered whose difference with its index exceeds remaining sum or all array elements have already been included.

Below is the implementation of the above approach:

 `// C++ Program to implement` `// the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the length` `// of the longest subset` `int` `findSubset(``int``* a, ``int` `n)` `{` `    ``// Stores the sum of differences` `    ``// between elements and` `    ``// their respective index` `    ``int` `sum = 0;`   `    ``// Stores the size of` `    ``// the subset` `    ``int` `cnt = 0;`   `    ``vector<``int``> v;`   `    ``// Iterate over the array` `    ``for` `(``int` `i = 1; i <= n; i++) {`   `        ``// If an element which is` `        ``// smaller than or equal` `        ``// to its index is encountered` `        ``if` `(a[i - 1] - i <= 0) {`   `            ``// Increase count and sum` `            ``sum += a[i - 1] - i;` `            ``cnt += 1;` `        ``}`   `        ``// Store the difference with` `        ``// index of the remaining` `        ``// elements` `        ``else` `{` `            ``v.push_back(a[i - 1] - i);` `        ``}` `    ``}`   `    ``// Sort the differences` `    ``// in increasing order` `    ``sort(v.begin(), v.end());`   `    ``int` `ptr = 0;`   `    ``// Include the differences while` `    ``// sum remains positive or` `    ``while` `(ptr < v.size()` `           ``&& sum + v[ptr] <= 0) {` `        ``cnt += 1;` `        ``ptr += 1;` `        ``sum += v[ptr];` `    ``}`   `    ``// Return the size` `    ``return` `cnt;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `arr[] = { 4, 1, 6, 7,` `                  ``8, 2 };`   `    ``int` `n = ``sizeof``(arr)` `            ``/ ``sizeof``(arr);`   `    ``// Function Calling` `    ``cout << findSubset(arr, n)` `         ``<< endl;` `}`

 `// Java program to implement ` `// the above approach` `import` `java.util.*; ` `class` `GFG{` `    `  `// Function to find the length ` `// of the longest subset ` `public` `static` `int` `findSubset(``int``[] a, ``int` `n) ` `{ ` `    `  `    ``// Stores the sum of differences ` `    ``// between elements and ` `    ``// their respective index ` `    ``int` `sum = ``0``; `   `    ``// Stores the size of ` `    ``// the subset ` `    ``int` `cnt = ``0``; `   `    ``Vector v = ``new` `Vector<>(); `   `    ``// Iterate over the array ` `    ``for``(``int` `i = ``1``; i <= n; i++)` `    ``{ ` `        `  `        ``// If an element which is ` `        ``// smaller than or equal ` `        ``// to its index is encountered ` `        ``if` `(a[i - ``1``] - i <= ``0``)` `        ``{ ` `            `  `            ``// Increase count and sum ` `            ``sum += a[i - ``1``] - i; ` `            ``cnt += ``1``; ` `        ``} `   `        ``// Store the difference with ` `        ``// index of the remaining ` `        ``// elements ` `        ``else` `        ``{ ` `            ``v.add(a[i - ``1``] - i); ` `        ``} ` `    ``} `   `    ``// Sort the differences ` `    ``// in increasing order ` `    ``Collections.sort(v); `   `    ``int` `ptr = ``0``; `   `    ``// Include the differences while ` `    ``// sum remains positive or ` `    ``while` `(ptr < v.size() && ` `           ``sum + v.get(ptr) <= ``0``)` `    ``{ ` `        ``cnt += ``1``; ` `        ``ptr += ``1``; ` `        ``sum += v.get(ptr); ` `    ``} `   `    ``// Return the size ` `    ``return` `cnt; ` `} `   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `arr[] = { ``4``, ``1``, ``6``, ``7``, ``8``, ``2` `}; ` `    ``int` `n = arr.length; `   `    ``// Function Calling ` `    ``System.out.println(findSubset(arr, n)); ` `}` `}`   `// This code is contributed by divyeshrabadiya07`

 `# Python3 program to implement  ` `# the above approach `   `# Function to find the length  ` `# of the longest subset ` `def` `findSubset(a, n):` `    `  `    ``# Stores the sum of differences  ` `    ``# between elements and  ` `    ``# their respective index  ` `    ``sum` `=` `0` `  `  `    ``# Stores the size of  ` `    ``# the subset  ` `    ``cnt ``=` `0` `  `  `    ``v ``=` `[]  ` `  `  `    ``# Iterate over the array  ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``):` `        `  `        ``# If an element which is  ` `        ``# smaller than or equal  ` `        ``# to its index is encountered  ` `        ``if` `(a[i ``-` `1``] ``-` `i <``=` `0``):` `            `  `            ``# Increase count and sum  ` `            ``sum` `+``=` `a[i ``-` `1``] ``-` `i  ` `            ``cnt ``+``=` `1` `    `  `        ``# Store the difference with  ` `        ``# index of the remaining  ` `        ``# elements  ` `        ``else``:` `            ``v.append(a[i ``-` `1``] ``-` `i)` `            `  `    ``# Sort the differences  ` `    ``# in increasing order  ` `    ``v.sort()` `  `  `    ``ptr ``=` `0` `  `  `    ``# Include the differences while  ` `    ``# sum remains positive or  ` `    ``while` `(ptr < ``len``(v) ``and` `           ``sum` `+` `v[ptr] <``=` `0``):` `        ``cnt ``+``=` `1` `        ``ptr ``+``=` `1`  `        ``sum` `+``=` `v[ptr]  ` `    `  `    ``# Return the size  ` `    ``return` `cnt`   `# Driver code` `if` `__name__``=``=``"__main__"``:` `    `  `    ``arr ``=` `[ ``4``, ``1``, ``6``, ``7``, ``8``, ``2` `]  ` `    ``n ``=` `len``(arr)` `  `  `    ``# Function calling  ` `    ``print``(findSubset(arr, n))`   `# This code is contributed by rutvik_56`

 `// C# program to implement ` `// the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{` `    `  `// Function to find the length ` `// of the longest subset ` `public` `static` `int` `findSubset(``int``[] a, ``int` `n) ` `{ ` `    `  `    ``// Stores the sum of differences ` `    ``// between elements and ` `    ``// their respective index ` `    ``int` `sum = 0; `   `    ``// Stores the size of ` `    ``// the subset ` `    ``int` `cnt = 0; `   `    ``List<``int``> v = ``new` `List<``int``>(); `   `    ``// Iterate over the array ` `    ``for``(``int` `i = 1; i <= n; i++)` `    ``{ ` `        `  `        ``// If an element which is ` `        ``// smaller than or equal ` `        ``// to its index is encountered ` `        ``if` `(a[i - 1] - i <= 0)` `        ``{ ` `            `  `            ``// Increase count and sum ` `            ``sum += a[i - 1] - i; ` `            ``cnt += 1; ` `        ``} `   `        ``// Store the difference with ` `        ``// index of the remaining ` `        ``// elements ` `        ``else` `        ``{ ` `            ``v.Add(a[i - 1] - i); ` `        ``} ` `    ``} `   `    ``// Sort the differences ` `    ``// in increasing order ` `    ``v.Sort(); `   `    ``int` `ptr = 0; `   `    ``// Include the differences while ` `    ``// sum remains positive or ` `    ``while` `(ptr < v.Count && ` `           ``sum + v[ptr] <= 0)` `    ``{ ` `        ``cnt += 1; ` `        ``ptr += 1; ` `        ``sum += v[ptr]; ` `    ``} `   `    ``// Return the size ` `    ``return` `cnt; ` `} `   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `[]arr = { 4, 1, 6, 7, 8, 2 }; ` `    ``int` `n = arr.Length; `   `    ``// Function calling ` `    ``Console.WriteLine(findSubset(arr, n)); ` `}` `}`   `// This code is contributed by amal kumar choubey`

Output:
```3

```

Time Complexity:O(N)
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.

Check out this Author's contributed articles.

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 :