Problem Statement

You are given an integer array arr[]. Your task is to find the minimum number of elements to delete so that the remaining elements form a strictly increasing sequence while maintaining their original order.

Example 1

Input

Code
arr = [5, 6, 1, 7, 4]

Output

Code
2

Explanation

Remove 1 and 4.

Remaining array:

Code
[5, 6, 7]

This is strictly increasing.

Example 2

Input

Code
arr = [1, 1, 1]

Output

Code
2

Explanation

A strictly increasing sequence cannot contain duplicate values.

Remove any two elements.

Remaining:

Code
[1]

Observation

Instead of thinking about which elements to delete, think about:

Which elements should we keep?

The longest sequence that is already strictly increasing is called the Longest Increasing Subsequence (LIS).

If the LIS length is L, then:

Code
Minimum Deletions = Total Elements - LIS Length

Why?

Suppose:

Code
Array = [5, 6, 1, 7, 4]

Longest Increasing Subsequence:

Code
[5, 6, 7]

Length:

Code
3

Total elements:

Code
5

Minimum deletions:

Code
5 - 3 = 2

Exactly what we need.

Efficient Approach (O(n log n))

A normal Dynamic Programming solution takes O(n²).

Since n can be 100000, O(n²) will cause a Time Limit Exceeded error.

Instead, we use the Binary Search LIS Algorithm.

Idea

Maintain an ArrayList called:

Java
lis

This does not always store the actual LIS.

Instead:

Java
lis[i]

stores the smallest possible ending value of an increasing subsequence of length (i + 1).

Smaller ending values help us build longer increasing subsequences later.

Dry Run

Array:

Code
[5, 6, 1, 7, 4]

Initially:

Code
lis = []

Step 1

Current number:

Code
5

No elements exist.

Add it.

Code
lis = [5]

Step 2

Current number:

Code
6

Greater than the last element.

Append.

Code
lis = [5, 6]

Step 3

Current number:

Code
1

Find the first element ≥ 1.

That is:

Code
5

Replace it.

Code
lis = [1, 6]

Notice:

We are not saying the LIS became [1, 6].

Instead, we found a better ending value.

Step 4

Current number:

Code
7

Greater than all elements.

Append.

Code
lis = [1, 6, 7]

Step 5

Current number:

Code
4

First element ≥ 4 is:

Code
6

Replace it.

Code
lis = [1, 4, 7]

Finished.

Length:

Code
3

Minimum deletions:

Code
5 - 3 = 2

Understanding the Code

class Solution

Java
class Solution {

Solution class required by GeeksForGeeks.

public int minDeletions(int[] arr)

Java
public int minDeletions(int[] arr) {

Function returns the minimum number of deletions.

Store Array Size

Java
int n = arr.length;

Store the array size.

Create the lis ArrayList

Java
ArrayList<Integer> lis = new ArrayList<>();

Stores the smallest possible tail values.

Initially empty.

Loop Through Every Element

Java
for (int num : arr)

Take one element at a time.

Java
int idx = Collections.binarySearch(lis, num);

Search the current number inside lis.

Two cases are possible.

Case 1: Element Exists

Example:

Code
lis = [1, 4, 7]

Search:

Code
4

Returns:

Code
1

Case 2: Element Doesn't Exist

Example:

Code
lis = [1, 4, 7]

Search:

Code
5

Java returns:

Code
-3

Why?

Insertion position:

Code
2

Java returns:

Code
-(position + 1)

So:

Code
-(2 + 1)
= -3

Therefore:

Java
if (idx < 0) {
    idx = -(idx + 1);
}

Converts:

Code
-3

back into:

Code
2

which is the correct insertion position.

If the Element Is Larger Than All

Java
if (idx == lis.size())

Example:

Code
lis = [1, 4, 7]
num = 9

Insertion position:

Code
3

which equals:

Code
lis.size()

Append:

Java
lis.add(num);

Now:

Code
[1, 4, 7, 9]

Otherwise Replace

Java
else {
    lis.set(idx, num);
}

Example:

Code
lis = [1, 6, 7]
num = 4

Replace:

Code
6

Result:

Code
[1, 4, 7]

A smaller tail increases the chance of extending the sequence later.

Return the Answer

Java
return n - lis.size();

Example:

Code
n = 5
LIS = 3

Answer:

Code
5 - 3
= 2

Complete Code

Java
import java.util.*;

class Solution {
    public int minDeletions(int[] arr) {

        int n = arr.length;

        ArrayList<Integer> lis = new ArrayList<>();

        for (int num : arr) {

            int idx = Collections.binarySearch(lis, num);

            if (idx < 0) {
                idx = -(idx + 1);
            }

            if (idx == lis.size()) {
                lis.add(num);
            } else {
                lis.set(idx, num);
            }
        }

        return n - lis.size();
    }
}

Complexity Analysis

Time Complexity

  • Loop through all n elements → O(n)

  • Binary search for each element → O(log n)

Overall:

Code
O(n log n)

Space Complexity

The lis list stores at most n elements.

Code
O(n)

Key Takeaways

  • We don't directly find which elements to delete.

  • We first compute the Longest Increasing Subsequence (LIS).

  • The minimum deletions required are:

Code
Minimum Deletions = n - LIS Length
  • The lis ArrayList does not always represent the actual LIS. It stores the smallest possible tail values for increasing subsequences of different lengths, allowing us to achieve an efficient O(n log n) solution using binary search.

Summary

To minimize deletions while preserving the original order, compute the Longest Increasing Subsequence (LIS) of the array. The answer is simply the total number of elements minus the LIS length. Using the binary search-based LIS algorithm reduces the time complexity from O(n²) to O(n log n), making it suitable for large input sizes.