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++
// C++ program for
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the number of strictly
// increasing subsequences in an array
int minimumIncreasingSubsequences(
int arr[], int N)
{
// Sort the array
sort(arr, arr + N);
// Stores final count
// of subsequences
int count = 0;
int i = 0;
// Traverse the array
while (i < N) {
// Stores current element
int x = arr[i];
// Stores frequency of
// the current element
int freqX = 0;
// Count frequency of
// the current element
while (i < N && arr[i] == x) {
freqX++;
i++;
}
// If current element frequency
// is greater than count
count = max(count, freqX);
}
// Print the final count
cout << count;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 1, 2, 1, 4, 3 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to find
// the number of strictly
// increasing subsequences
minimumIncreasingSubsequences(arr, N);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the number of strictly
// increasing subsequences in an array
static void minimumIncreasingSubsequences(
int arr[], int N)
{
// Sort the array
Arrays.sort(arr);
// Stores final count
// of subsequences
int count = 0;
int i = 0;
// Traverse the array
while (i < N)
{
// Stores current element
int x = arr[i];
// Stores frequency of
// the current element
int freqX = 0;
// Count frequency of
// the current element
while (i < N && arr[i] == x)
{
freqX++;
i++;
}
// If current element frequency
// is greater than count
count = Math.max(count, freqX);
}
// Print the final count
System.out.print(count);
}
// Driver Code
public static void main(String args[])
{
// Given array
int arr[] = { 2, 1, 2, 1, 4, 3 };
// Size of the array
int N = arr.length;
// Function call to find
// the number of strictly
// increasing subsequences
minimumIncreasingSubsequences(arr, N);
}
}
// This code is contributed by splevel62.
Python3
# Python3 program to implement
# the above approach
# Function to find the number of strictly
# increasing subsequences in an array
def minimumIncreasingSubsequences(arr, N) :
# Sort the array
arr.sort()
# Stores final count
# of subsequences
count = 0
i = 0
# Traverse the array
while (i < N) :
# Stores current element
x = arr[i]
# Stores frequency of
# the current element
freqX = 0
# Count frequency of
# the current element
while (i < N and arr[i] == x) :
freqX += 1
i += 1
# If current element frequency
# is greater than count
count = max(count, freqX)
# Print the final count
print(count)
# Given array
arr = [ 2, 1, 2, 1, 4, 3 ]
# Size of the array
N = len(arr)
# Function call to find
# the number of strictly
# increasing subsequences
minimumIncreasingSubsequences(arr, N)
# This code is contributed by divyesh072019.
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function to find the number of strictly
// increasing subsequences in an array
static void minimumIncreasingSubsequences(
int []arr, int N)
{
// Sort the array
Array.Sort(arr);
// Stores readonly count
// of subsequences
int count = 0;
int i = 0;
// Traverse the array
while (i < N)
{
// Stores current element
int x = arr[i];
// Stores frequency of
// the current element
int freqX = 0;
// Count frequency of
// the current element
while (i < N && arr[i] == x)
{
freqX++;
i++;
}
// If current element frequency
// is greater than count
count = Math.Max(count, freqX);
}
// Print the readonly count
Console.Write(count);
}
// Driver Code
public static void Main(String []args)
{
// Given array
int []arr = { 2, 1, 2, 1, 4, 3 };
// Size of the array
int N = arr.Length;
// Function call to find
// the number of strictly
// increasing subsequences
minimumIncreasingSubsequences(arr, N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to implement the above approach
// Function to find the number of strictly
// increasing subsequences in an array
function minimumIncreasingSubsequences(arr, N)
{
// Sort the array
arr.sort(function(a, b){return a - b});
// Stores readonly count
// of subsequences
let count = 0;
let i = 0;
// Traverse the array
while (i < N)
{
// Stores current element
let x = arr[i];
// Stores frequency of
// the current element
let freqX = 0;
// Count frequency of
// the current element
while (i < N && arr[i] == x)
{
freqX++;
i++;
}
// If current element frequency
// is greater than count
count = Math.max(count, freqX);
}
// Print the readonly count
document.write(count);
}
// Given array
let arr = [ 2, 1, 2, 1, 4, 3 ];
// Size of the array
let N = arr.length;
// Function call to find
// the number of strictly
// increasing subsequences
minimumIncreasingSubsequences(arr, N);
// This code is contributed by suresh07.
</script>
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Similar Reads
Minimum number of strictly decreasing subsequences Given an array arr[] of size N. The task is to split the array into minimum number of strictly decreasing subsequences. Calculate the minimum number of subsequences we can get by splitting. Examples: Input: N = 4, arr[] = {3, 5, 1, 2}Output: 2Explanation: We can split the array into two subsequences
7 min read
Minimum number of increasing subsequences Given an array of integers of size N, you have to divide it into the minimum number of "strictly increasing subsequences" For example: let the sequence be {1, 3, 2, 4}, then the answer would be 2. In this case, the first increasing sequence would be {1, 3, 4} and the second would be {2}.Examples: In
10 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
Minimum number of elements which are not part of Increasing or decreasing subsequence in array Given an array of n elements. Make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements which are not
12 min read
Count number of increasing subsequences of size k Given an array arr[] containing n integers. The problem is to count number of increasing subsequences in the array of size k. Examples: Input : arr[] = {2, 6, 4, 5, 7}, k = 3Output : 5The subsequences of size '3' are:{2, 6, 7}, {2, 4, 5}, {2, 4, 7},{2, 5, 7} and {4, 5, 7}.Input : arr[] = {12, 8, 11,
15+ min read