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 = 21Input: arr[] = {4, 1, 6, 7, 8, 2}
Output: 2
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++
// C++ Program to implement // the above approach #include <bits/stdc++.h> 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[0]); // Function Calling cout << findSubset(arr, n) << endl; } |
Java
// 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<Integer> 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 |
C#
// 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 |
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.
Recommended Posts:
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Largest number less than N with digit sum greater than the digit sum of N
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Largest number less than X having at most K set bits
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Sum of largest prime factor of each number less than equal to n
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Find all possible subarrays having product less than or equal to K
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Largest number less than or equal to N in BST (Iterative Approach)
- Find Largest Special Prime which is less than or equal to a given number
- Largest number less than or equal to N/2 which is coprime to N
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Highest and Smallest power of K less than and greater than equal to N respectively
- Maximum sum subset having equal number of positive and negative elements
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Number of subarrays having sum less than K
- Count of subsets having sum of min and max element less than K
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.

