Largest subset having with sum less than equal to sum of respective indices
Last Updated :
02 Sep, 2022
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++
// 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>
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
Similar Reads
Largest Subset with sum less than each Array element Given an array arr[] containing N elements, the task is to find the size of the largest subset for each array element arr[i] such that the sum of the subset is less than that element. Examples: Input: arr[] = { 5, 2, 1, 1, 1, 6, 8}Output: 3 1 0 0 0 4 4 Explanation: For i = 0 -> subset = {1, 1, 1}
12 min read
Largest integers with sum of setbits at most K Given an array of integer nums[] and a positive integer K, the task is to find the maximum number of integers that can be selected from the array such that the sum of the number of 1s in their binary representation is at most K. Examples: Input: nums[] = [3, 9, 4, 6], K = 3Output: 2Explanation: The
7 min read
Maximum sum subarray having sum less than or equal to given sum You are given an array of non-negative integers and a target sum. Your task is to find a contiguous subarray whose sum is the maximum possible, while ensuring that it does not exceed the given target sum.Note: The given array contains only non-negative integers.Examples: Input: arr[] = [1, 2, 3, 4,
6 min read
Find the size of Largest Subset with positive Bitwise AND Given an array arr[] consisting of N positive integers, the task is to find the largest size of the subset of the array arr[] with positive Bitwise AND. Note : If there exist more than one such subsets then return size of only one subset. Examples: Input: arr[] = [7, 13, 8, 2, 3]Output: 3Explanation
6 min read
For each A[i] find smallest subset with all elements less than A[i] sum more than B[i] Given two arrays A[] and B[] of N integers, the task is to find for each element A[i], the size of the smallest subset S of indices, such that : Each value corresponding to the indices in subset S is strictly less than A[i].Sum of elements corresponding to the indices in B is strictly greater than B
10 min read