Minimize the number of strictly increasing subsequences in an array | Set 2
Last Updated :
26 Apr, 2021
Given an array arr[] of size N, the task is to print the minimum possible count of strictly increasing subsequences present in the array.
Note: It is possible to swap the pairs of array elements.
Examples:
Input: arr[] = {2, 1, 2, 1, 4, 3}
Output: 2
Explanation: Sorting the array modifies the array to arr[] = {1, 1, 2, 2, 3, 4}. Two possible increasing subsequences are {1, 2, 3} and {1, 2, 4}, which involves all the array elements.
Input: arr[] = {3, 3, 3}
Output: 3
MultiSet-based Approach: Refer to the previous post to solve the problem using Multiset to find the longest decreasing subsequence in the array.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Space-Optimized Approach: The optimal idea is based on the following observation:
Two elements with the same value can’t be included in a single subsequence, as they won’t form a strictly increasing subsequence.
Therefore, for every distinct array element, count its frequency, say y. Therefore, at least y subsequences are required.
Hence, the frequency of the most occurring array element is the required answer.
Follow the steps below to solve the problem:
- Initialize a variable, say count, to store the final count of strictly increasing subsequences.
- Traverse the array arr[] and perform the following observations:
- Initialize two variables, say X, to store the current array element, and freqX to store the frequency of the current array element.
- Find and store all the occurrences of the current element in freqX.
- If the frequency of the current element is greater than the previous count, then update the count.
- Print the value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumIncreasingSubsequences(
int arr[], int N)
{
sort(arr, arr + N);
int count = 0;
int i = 0;
while (i < N) {
int x = arr[i];
int freqX = 0;
while (i < N && arr[i] == x) {
freqX++;
i++;
}
count = max(count, freqX);
}
cout << count;
}
int main()
{
int arr[] = { 2, 1, 2, 1, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
minimumIncreasingSubsequences(arr, N);
}
|
Java
import java.util.*;
class GFG
{
static void minimumIncreasingSubsequences(
int arr[], int N)
{
Arrays.sort(arr);
int count = 0 ;
int i = 0 ;
while (i < N)
{
int x = arr[i];
int freqX = 0 ;
while (i < N && arr[i] == x)
{
freqX++;
i++;
}
count = Math.max(count, freqX);
}
System.out.print(count);
}
public static void main(String args[])
{
int arr[] = { 2 , 1 , 2 , 1 , 4 , 3 };
int N = arr.length;
minimumIncreasingSubsequences(arr, N);
}
}
|
Python3
def minimumIncreasingSubsequences(arr, N) :
arr.sort()
count = 0
i = 0
while (i < N) :
x = arr[i]
freqX = 0
while (i < N and arr[i] = = x) :
freqX + = 1
i + = 1
count = max (count, freqX)
print (count)
arr = [ 2 , 1 , 2 , 1 , 4 , 3 ]
N = len (arr)
minimumIncreasingSubsequences(arr, N)
|
C#
using System;
public class GFG
{
static void minimumIncreasingSubsequences(
int []arr, int N)
{
Array.Sort(arr);
int count = 0;
int i = 0;
while (i < N)
{
int x = arr[i];
int freqX = 0;
while (i < N && arr[i] == x)
{
freqX++;
i++;
}
count = Math.Max(count, freqX);
}
Console.Write(count);
}
public static void Main(String []args)
{
int []arr = { 2, 1, 2, 1, 4, 3 };
int N = arr.Length;
minimumIncreasingSubsequences(arr, N);
}
}
|
Javascript
<script>
function minimumIncreasingSubsequences(arr, N)
{
arr.sort( function (a, b){ return a - b});
let count = 0;
let i = 0;
while (i < N)
{
let x = arr[i];
let freqX = 0;
while (i < N && arr[i] == x)
{
freqX++;
i++;
}
count = Math.max(count, freqX);
}
document.write(count);
}
let arr = [ 2, 1, 2, 1, 4, 3 ];
let N = arr.length;
minimumIncreasingSubsequences(arr, N);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...