Maximize subsequences having array elements not exceeding length of the subsequence
Last Updated :
19 May, 2021
Given an array arr[] consisting of N positive integers, the task is to maximize the number of subsequences that can be obtained from an array such that every element arr[i] that is part of any subsequence does not exceed the length of that subsequence.
Examples:
Input: arr[] = {1, 1, 1, 1}
Output: 4
Explanation:
Form 4 subsequences of 1 which satisfy the given condition {1}, {1}, {1}, {1}.
Input: arr[] = {2, 2, 3, 1, 2, 1}
Output: 3
Explanation:
The possible group are {1}, {1}, {2, 2}
So, the output is 3.
Approach: The idea is to use Greedy Technique to solve this problem. Follow the steps below to solve the problem:
- Initialize a map to store the frequency of every array element.
- Initialize a variable, say count to 0, to store the total number of subsequences obtained.
- Keep track of the number of elements that are left to be added.
- Now, iterate over the map and count the number of elements that can be included in a particular group.
- Keep on adding the elements on the valid subsequences.
- After completing the above steps, print the count of subsequences.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the number
// of subsequences that can be formed
int No_Of_subsequences(map<int, int> mp)
{
// Stores the number of subsequences
int count = 0;
int left = 0;
// Iterate over the map
for (auto x : mp) {
x.second += left;
// Count the number of subsequences
// that can be formed from x.first
count += (x.second / x.first);
// Number of occurrences of
// x.first which are left
left = x.second % x.first;
}
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
void maximumsubsequences(int arr[], int n)
{
// Stores the frequency of arr[]
map<int, int> mp;
// Update the frequency
for (int i = 0; i < n; i++)
mp[arr[i]]++;
// Print the number of subsequences
cout << No_Of_subsequences(mp);
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 1, 1, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
maximumsubsequences(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to calculate the number
// of subsequences that can be formed
public static int No_Of_subsequences(HashMap<Integer,
Integer> mp)
{
// Stores the number of subsequences
int count = 0;
int left = 0;
// Iterate over the map
for(Map.Entry<Integer, Integer> x : mp.entrySet())
{
mp.replace(x.getKey(), x.getValue() + left);
// Count the number of subsequences
// that can be formed from x.first
count += (x.getValue() / x.getKey());
// Number of occurrences of
// x.first which are left
left = x.getValue() % x.getKey();
}
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
public static void maximumsubsequences(int[] arr,
int n)
{
// Stores the frequency of arr[]
HashMap<Integer, Integer> mp = new HashMap<>();
// Update the frequency
for(int i = 0; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.replace(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
// Print the number of subsequences
System.out.println(No_Of_subsequences(mp));
}
// Driver code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 1, 1, 1, 1 };
int N = arr.length;
// Function call
maximumsubsequences(arr, N);
}
}
// This code is contributed divyeshrabadiya07
Python3
# Python3 program for
# the above approach
from collections import defaultdict
# Function to calculate
# the number of subsequences
# that can be formed
def No_Of_subsequences(mp):
# Stores the number
# of subsequences
count = 0
left = 0
# Iterate over the map
for x in mp:
mp[x] += left
# Count the number of
# subsequences that can
# be formed from x.first
count += (mp[x] // x)
# Number of occurrences of
# x.first which are left
left = mp[x] % x
# Return the number
# of subsequences
return count
# Function to create the
# maximum count of subsequences
# that can be formed
def maximumsubsequences(arr, n):
# Stores the frequency of arr[]
mp = defaultdict (int)
# Update the frequency
for i in range (n):
mp[arr[i]] += 1
# Print the number of subsequences
print(No_Of_subsequences(mp))
# Driver Code
if __name__ == "__main__":
# Given array arr[]
arr = [1, 1, 1, 1]
N = len(arr)
# Function Call
maximumsubsequences(arr, N)
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate the number
// of subsequences that can be formed
public static int No_Of_subsequences(Dictionary<int,
int> mp)
{
// Stores the number
// of subsequences
int count = 0;
int left = 0;
// Iterate over the map
foreach(KeyValuePair<int,
int> x in mp)
{
if(!mp.ContainsKey(x.Key))
mp.Add(x.Key, x.Value + left);
// Count the number of subsequences
// that can be formed from x.first
count += (x.Value / x.Key);
// Number of occurrences of
// x.first which are left
left = x.Value % x.Key;
}
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
public static void maximumsubsequences(int[] arr,
int n)
{
// Stores the frequency of []arr
Dictionary<int,
int> mp = new Dictionary<int,
int>();
// Update the frequency
for(int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
// Print the number of subsequences
Console.WriteLine(No_Of_subsequences(mp));
}
// Driver code
public static void Main(String[] args)
{
// Given array []arr
int []arr = {1, 1, 1, 1};
int N = arr.Length;
// Function call
maximumsubsequences(arr, N);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program for the above approach
// Function to calculate the number
// of subsequences that can be formed
function No_Of_subsequences(mp)
{
// Stores the number of subsequences
var count = 0;
var left = 0;
// Iterate over the map
mp.forEach((value, key) => {
value += left;
// Count the number of subsequences
// that can be formed from key
count += (value / key);
// Number of occurrences of
// x.first which are left
left = value % key;
});
// Return the number of subsequences
return count;
}
// Function to create the maximum count
// of subsequences that can be formed
function maximumsubsequences(arr, n)
{
// Stores the frequency of arr[]
var mp = new Map();
// Update the frequency
for (var i = 0; i < n; i++)
{
if(mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
// Print the number of subsequences
document.write( No_Of_subsequences(mp));
}
// Driver Code
// Given array arr[]
var arr = [1, 1, 1, 1];
var N = arr.length;
// Function Call
maximumsubsequences(arr, N);
// This code is contributed by itsok.
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Similar Reads
Maximum length subsequence such that adjacent elements in the subsequence have a common factor Given an array arr[], the task is to find the maximum length of a subsequence such that the adjacent elements in the subsequence have a common factor. Examples: Input: arr[] = { 13, 2, 8, 6, 3, 1, 9 } Output: 5Max length subsequence with satisfied conditions: { 2, 8, 6, 3, 9 } Input: arr[] = { 12, 2
9 min read
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence Given an array arr[], the task is to find the maximum sum of all the elements which are not a part of the longest increasing subsequence. Examples: Input: arr[] = {4, 6, 1, 2, 3, 8} Output: 10 Explanation: Elements are 4 and 6 Input: arr[] = {5, 4, 3, 2, 1} Output: 14 Explanation: Elements are 5, 4,
8 min read
Maximize count of Decreasing Subsequences from the given Array Given an array arr[], the task is to rearrange the array to generate maximum decreasing subsequences and print the count of the maximum number of subsequences possible such that each array element can be part of a single subsequence and the length of the subsequences needs to be maximized. Example:
5 min read
Length of the longest increasing subsequence such that no two adjacent elements are coprime Given an array arr[]. The task is to find the length of the longest Subsequence from the given array such that the sequence is strictly increasing and no two adjacent elements are coprime. Note: The elements in the given array are strictly increasing in order (1 <= a[i] <= 105)Examples: Input
9 min read
Length of the longest increasing subsequence which does not contain a given sequence as Subarray Given two arrays arr[] and arr1[] of lengths N and M respectively, the task is to find the longest increasing subsequence of array arr[] such that it does not contain array arr1[] as subarray. Examples: Input: arr[] = {5, 3, 9, 3, 4, 7}, arr1[] = {3, 3, 7}Output: 4Explanation: Required longest incre
14 min read