Size of array after repeated deletion of LIS
Last Updated :
13 Sep, 2023
Given an array arr[0..n-1] of the positive element. The task is to print the remaining elements of arr[] after repeated deletion of LIS (of size greater than 1). If there are multiple LIS with the same length, we need to choose the LIS that ends first.
Examples:
Input : arr[] = {1, 2, 5, 3, 6, 4, 1}
Output : 1
Explanation :
{1, 2, 5, 3, 6, 4, 1} - {1, 2, 5, 6} = {3, 4, 1}
{3, 4, 1} - {3, 4} = {1}
Input : arr[] = {1, 2, 3, 1, 5, 2}
Output : -1
Explanation :
{1, 2, 3, 1, 5, 2} - {1, 2, 3, 5} = {1, 2}
{1, 2} - {1, 2} = {}
Input : arr[] = {5, 3, 2}
Output : 3
We repeatedly find LIS and remove its elements from an array.
// input vector array = arr[]
// max-sum LIS vector array = maxArray[]
while (arr.size())
{
// find LIS
lis = findLIS(arr, arr.size());
if (lis.size() < 2)
break;
// Remove lis elements from current array. Note
// that both lis[] and arr[] are sorted in
// increasing order.
for (i=0; i<arr.size() && lis.size()>0; i++)
{
if (arr[i] == lis[0])
{
// Remove lis element from arr[]
arr.erase(arr.begin()+i) ;
i--;
// erase the element from lis[]. This is
// needed to make sure that next element
// to be removed is first element of lis[]
lis.erase(lis.begin()) ;
}
}
}
// print remaining element of array
for (i=0; i<arr.size(); i++)
cout << arr[i] << " ";
if (i == 0)
cout << "-1";
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findLIS(vector< int > arr, int n)
{
vector <vector< int > > L(n);
L[0].push_back(arr[0]);
for ( int i = 1; i < n; i++)
{
for ( int j = 0; j < i; j++)
{
if (arr[i] > arr[j] && (L[i].size() < L[j].size()))
L[i] = L[j];
}
L[i].push_back(arr[i]);
}
int maxSize = 1;
vector< int > lis;
for (vector< int > x : L)
{
if (x.size() > maxSize)
{
lis = x;
maxSize = x.size();
}
}
return lis;
}
void minimize( int input[], int n)
{
vector< int > arr(input, input + n);
while (arr.size())
{
vector< int > lis = findLIS(arr, arr.size());
if (lis.size() < 2)
break ;
for ( int i=0; i<arr.size() && lis.size()>0; i++)
{
if (arr[i] == lis[0])
{
arr.erase(arr.begin()+i) ;
i--;
lis.erase(lis.begin()) ;
}
}
}
int i;
for (i=0; i < arr.size(); i++)
cout << arr[i] << " " ;
if (i == 0)
cout << "-1" ;
}
int main()
{
int input[] = { 3, 2, 6, 4, 5, 1 };
int n = sizeof (input) / sizeof (input[0]);
minimize(input, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> findLIS(Vector<Integer> arr,
int n)
{
Vector<Integer> []L = new Vector[n];
for ( int i = 0 ; i < L.length; i++)
L[i] = new Vector<Integer>();
L[ 0 ].add(arr.elementAt( 0 ));
for ( int i = 1 ; i < n; i++)
{
for ( int j = 0 ; j < i; j++)
{
if (arr.elementAt(i) > arr.elementAt(j) &&
(L[i].size() < L[j].size()))
L[i] = L[j];
}
L[i].add(arr.elementAt(i));
}
int maxSize = 1 ;
Vector<Integer> lis = new Vector<>();
for (Vector<Integer> x : L)
{
if (x.size() > maxSize)
{
lis = x;
maxSize = x.size();
}
}
return lis;
}
static void minimize( int input[],
int n)
{
Vector<Integer> arr = new Vector<>();
for ( int i = 0 ; i < n; i++)
arr.add(input[i]);
while (arr.size() != 0 )
{
Vector<Integer> lis = findLIS(arr,
arr.size());
if (lis.size() < 2 )
break ;
for ( int i = 0 ; i < arr.size() &&
lis.size() > 0 ; i++)
{
if (arr.elementAt(i) == lis.elementAt( 0 ))
{
arr.removeAll(lis);
i--;
lis.remove( 0 );
}
}
}
int i;
for (i = 1 ; i < arr.size(); i++)
System.out.print(arr.elementAt(i) + " " );
if (i == 0 )
System.out.print( "-1" );
}
public static void main(String[] args)
{
int input[] = { 3 , 2 , 6 , 4 , 5 , 1 };
int n = input.length;
minimize(input, n);
}
}
|
Python3
from typing import List
def findLIS(arr: List [ int ], n: int ) - > List [ int ]:
L = [ 0 ] * n
for i in range (n):
L[i] = []
L[ 0 ].append(arr[ 0 ])
for i in range ( 1 , n):
for j in range (i):
if (arr[i] > arr[j] and
( len (L[i]) < len (L[j]))):
L[i] = L[j].copy()
L[i].append(arr[i])
maxSize = 1
lis: List [ int ] = []
for x in L:
if ( len (x) > maxSize):
lis = x.copy()
maxSize = len (x)
return lis
def minimize( input : List [ int ], n: int ) - > None :
arr = input .copy()
while len (arr):
lis = findLIS(arr, len (arr))
if ( len (lis) < 2 ):
break
i = 0
while i < len (arr) and len (lis) > 0 :
if (arr[i] = = lis[ 0 ]):
arr.remove(arr[i])
i - = 1
lis.remove(lis[ 0 ])
i + = 1
i = 0
while i < len (arr):
print (arr[i], end = " " )
i + = 1
if (i = = 0 ):
print ( "-1" )
if __name__ = = "__main__" :
input = [ 3 , 2 , 6 , 4 , 5 , 1 ]
n = len ( input )
minimize( input , n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< int > findLIS(List< int > arr, int n)
{
List<List< int > > L = new List<List< int > >();
for ( int i = 0; i < n; i++) {
L.Add( new List< int >());
}
L[0].Add(arr[0]);
for ( int i = 1; i < n; i++)
{
for ( int j = 0; j < i; j++)
{
if (arr[i] > arr[j]
&& (L[i].Count < L[j].Count))
L[i] = new List< int >(L[j]);
}
L[i].Add(arr[i]);
}
int maxSize = 1;
List< int > lis = new List< int >();
foreach (List< int > x in L)
{
if (x.Count > maxSize) {
lis = x;
maxSize = x.Count;
}
}
return lis;
}
static void minimize( int [] input, int n)
{
List< int > arr = new List< int >(input);
while (arr.Count > 0)
{
List< int > lis = findLIS(arr, arr.Count);
if (lis.Count < 2)
break ;
for ( int i = 0; i < arr.Count; i++) {
if (lis.Contains(arr[i])) {
arr.RemoveAt(i);
i--;
}
}
}
int j = 0;
for (j = 0; j < arr.Count; j++)
Console.Write(arr[j] + " " );
if (j == 0)
Console.Write( "-1" );
}
static void Main()
{
int [] input = { 3, 2, 6, 4, 5, 1 };
int n = input.Length;
minimize(input, n);
}
}
|
Javascript
function findLIS(arr) {
let n = arr.length;
let L = Array(n).fill().map(() => []);
L[0].push(arr[0]);
for (let i = 1; i < n; i++) {
for (let j = 0; j < i; j++) {
if (arr[i] > arr[j] && (L[i].length < L[j].length))
L[i] = L[j];
}
L[i].push(arr[i]);
}
let maxSize = 1;
let lis = [];
for (let x of L) {
if (x.length > maxSize) {
lis = x;
maxSize = x.length;
}
}
return lis;
}
function minimize(input) {
let arr = input.slice();
while (arr.length) {
let lis = findLIS(arr);
if (lis.length < 2)
break ;
for (let i = 0; i < arr.length && lis.length > 0; i++) {
if (arr[i] === lis[0]) {
arr.splice(i, 1);
i--;
lis.shift();
}
}
}
if (arr.length) {
console.log(arr[1]);
} else {
console.log(-1);
}
}
let input = [3, 2, 6, 4, 5, 1];
minimize(input);
|
Output:
1
Time Complexity:O(n^2)
Space Complexity:O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...