# Size of array after repeated deletion of LIS

Given an array arr[0..n-1] of positive element. The task is to print remaining elements of arr[] after repeated deletion of LIS (of size greater than 1). If there are multiple LIS with 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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

We repeatedly find LIS and remove its elements from 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)
{
// 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++ program to find size of array after repeated ` `  ``deletion of LIS */` `#include ` `using` `namespace` `std; ` ` `  `// Function to construct Maximum Sum LIS ` `vector<``int``> findLIS(vector<``int``> arr, ``int` `n) ` `{ ` `    ``// L[i] - The Maximum Sum Increasing ` `    ``// Subsequence that ends with arr[i] ` `    ``vector > L(n); ` ` `  `    ``// L is equal to arr ` `    ``L.push_back(arr); ` ` `  `    ``// start from index 1 ` `    ``for` `(``int` `i = 1; i < n; i++) ` `    ``{ ` `        ``// for every j less than i ` `        ``for` `(``int` `j = 0; j < i; j++) ` `        ``{ ` `            ``/* L[i] = {MaxSum(L[j])} + arr[i] ` `            ``where j < i and arr[j] < arr[i] */` `            ``if` `(arr[i] > arr[j]) ` `                ``L[i] = L[j]; ` `        ``} ` ` `  `        ``// L[i] ends with arr[i] ` `        ``L[i].push_back(arr[i]); ` `    ``} ` ` `  `    ``// set lis =  LIS ` `    ``// whose size is max among all ` `    ``int` `maxSize = 1; ` `    ``vector<``int``> lis; ` `    ``for` `(vector<``int``> x : L) ` `    ``{ ` `        ``// The > sign makes sure that the LIS ` `        ``// ending first is chose.     ` `        ``if` `(x.size() > maxSize) ` `        ``{ ` `            ``lis = x; ` `            ``maxSize = x.size(); ` `        ``} ` `    ``} ` ` `  `    ``return` `lis; ` `} ` ` `  `// Function to minimize array ` `void` `minimize(``int` `input[], ``int` `n) ` `{ ` `    ``vector<``int``> arr(input, input + n); ` ` `  `    ``while` `(arr.size()) ` `    ``{ ` `        ``// Find LIS of current array ` `        ``vector<``int``> lis = findLIS(arr, arr.size()); ` ` `  `        ``// If all elements are in decreasing order ` `        ``if` `(lis.size() < 2) ` `            ``break``; ` ` `  `        ``// Remove lis elements from current array. Note ` `        ``// that both lis[] and arr[] are sorted in ` `        ``// increasing order. ` `        ``for` `(``int` `i=0; i0; i++) ` `        ``{ ` `            ``// If first element of lis[] is found ` `            ``if` `(arr[i] == lis) ` `            ``{ ` `                ``// Remove lis element from arr[] ` `                ``arr.erase(arr.begin()+i) ; ` `                ``i--; ` ` `  `                ``// Erase first element of lis[] ` `                ``lis.erase(lis.begin()) ; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// print remaining element of array ` `    ``int` `i; ` `    ``for` `(i=0; i < arr.size(); i++) ` `        ``cout  << arr[i] << ``" "``; ` ` `  `    ``// print -1 for empty array ` `    ``if` `(i == 0) ` `        ``cout << ``"-1"``; ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``int` `input[] = { 3, 2, 6, 4, 5, 1 }; ` `    ``int` `n = ``sizeof``(input) / ``sizeof``(input); ` ` `  `    ``// minimize array after deleting LIS ` `    ``minimize(input, n); ` ` `  `    ``return` `0; ` `} `

Output:

```1
```

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.