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: 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++
// 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 |
Python3
# 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#
// 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 |
Javascript
<script> // Javascript program for the above approach // Function to find the length // of the longest subset function findSubset(a, n) { // Stores the sum of differences // between elements and // their respective index let sum = 0; // Stores the size of // the subset let cnt = 0; let v = []; // Iterate over the array for (let 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(a[i - 1] - i); } } // Sort the differences // in increasing order v.sort(); let ptr = 0; // Include the differences while // sum remains positive or while (ptr < v.length && sum + v[ptr] <= 0) { cnt += 1; ptr += 1; sum += v[ptr]; } // Return the size return cnt; } // Driver Code let arr = [ 4, 1, 6, 7, 8, 2 ]; let n = arr.length; // Function Calling document.write(findSubset(arr, n)); </script> |
3
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
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!