Minimum concatenation required to get strictly LIS for the given array
Given an array A[] of size n where there are only unique elements in the array. We have to find the minimum concatenation required for sequence A to get strictly Longest Increasing Subsequence. For array A[] we follow 1 based indexing.
Examples:
Input: A = {1, 3, 2}
Output: 2
Explanation:
We can concatenate A two times as [1, 3, 2, 1, 3, 2] and then output for index 1, 3, 5 which gives sub-sequence as 1->2->3.
Input: A = {2, 1, 4, 3, 5}
Output: 3
Explanation:
The given array has to be concatenated 3 times to generate the Longest Increasing Subsequence.
Approach:
To solve the problem mentioned above the very first observation is that a strictly increasing sub-sequence will always have its length equal to the number of unique elements present in sequence A[]. Hence, we have to figure out a way to keep the maximum consecutive numbers in order are together. We can achieve this by taking as many strictly consecutive numbers in a sequence before opting for concatenation and handle others in the next concatenation.
- Form pairs of elements and its index and store them in sorted order as per value. Initialize a variable to count the required concatenation operations.
- Run a loop from index 2 to n and if index of pair[i-1] > pair[i] then increment the variable by 1 which was counting the required concatenation operations, otherwise continue the process.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int increasingSubseq( int a[], int n)
{
map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp.insert(make_pair(a[i], i));
}
int ans = 1;
auto itr = ++mp.begin();
for ( auto it = mp.begin(); it != mp.end(); ++it) {
if (itr != mp.end()) {
if (itr->second <= it->second)
ans += 1;
++itr;
}
}
cout << ans << endl;
}
int main()
{
int a[] = { 2, 1, 4, 3, 5 };
int n = sizeof (a) / sizeof (a[0]);
increasingSubseq(a, n);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG {
private static void increasingSubseq( int a[], int n)
{
TreeMap<Integer, Integer> mp
= new TreeMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
mp.put(a[i], i);
}
int ans = 1 ;
Map.Entry<Integer, Integer> itr
= mp.pollFirstEntry();
for (Map.Entry<Integer, Integer> it :
mp.entrySet())
{
if (itr.getValue() >= it.getValue())
ans++;
itr = it;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int a[] = { 2 , 1 , 4 , 3 , 5 };
int n = a.length;
increasingSubseq(a, n);
}
}
|
Python3
def increasingSubseq(a, n):
mp = {}
for i in range (n):
mp[a[i]] = i
ans = 1
itr = 1
li = list (mp.values())
for key, value in mp.items():
if (itr < len (mp)):
if (li[itr] < = key):
ans + = 1
itr + = 1
print (ans)
if __name__ = = '__main__' :
a = [ 2 , 1 , 4 , 3 , 5 ]
n = len (a)
increasingSubseq(a, n)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG
{
private static void increasingSubseq( int [] a, int n)
{
SortedDictionary< int , int > mp = new SortedDictionary< int , int >();
for ( int i = 0; i < n; i++)
{
mp[a[i]] = i;
}
int ans = 1;
KeyValuePair< int , int > itr = mp.First();
foreach (KeyValuePair< int , int > it in mp)
{
if (itr.Value > it.Value)
{
ans++;
}
itr = it;
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int [] a = { 2, 1, 4, 3, 5 };
int n = a.Length;
increasingSubseq(a, n);
}
}
|
Javascript
<script>
function increasingSubseq(a, n){
let mp = new Map()
for (let i=0;i<n;i++){
mp.set(a[i],i)
}
let ans = 1
let itr = 1
let li= Array.from(mp.values())
for ([key, value] of mp){
if (itr < mp.size){
if (li[itr] <= key)
ans += 1
itr+=1
}
}
document.write(ans)
}
let a = [2, 1, 4, 3, 5]
let n = a.length
increasingSubseq(a, n)
</script>
|
Time complexity: O(N * log N)
Space complexity: O(N)
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...