Minimum number of deletions to make a sorted sequence

2.7

Given an array of n integers. The task is to remove or delete minimum number of elements from the array so that when the remaining elements are placed in the same sequence order form a sorted sequence.

Examples:

Input : {5, 6, 1, 7, 4}
Output : 2
Removing 1 and 4
leaves the remaining sequence order as
5 6 7 which is a sorted sequence.

Input : {30, 40, 2, 5, 1, 7, 45, 50, 8}
Output : 4

A simple solution is to remove all subsequences one by one and check if remaining set of elements are in sorted order or not. Time complexity of this solution is exponential.

An efficient approach uses the concept of finding the length of the longest increasing subsequence of a given sequence.

Algorithm:

-->arr be the given array.
-->n number of elements in arr.
-->len be the length of longest
   increasing subsequence in arr.
-->// minimum number of deletions
   min = n - len

C++

// C++ implementation to find minimum number
// of deletions to make a sorted sequence
#include <bits/stdc++.h>
using namespace std;

/* lis() returns the length of the longest
    increasing subsequence in arr[] of size n */
int lis( int arr[], int n )
{
    int result = 0;
    int lis[n];

    /* Initialize LIS values for all indexes */
    for (int i = 0; i < n; i++ )
        lis[i] = 1;

    /* Compute optimized LIS values in bottom
        up manner */
    for (int i = 1; i < n; i++ )
        for (int j = 0; j < i; j++ )
            if ( arr[i] > arr[j] &&
                 lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;

    /* Pick resultimum of all LIS values */
    for (int i = 0; i < n; i++ )
        if (result < lis[i])
            result = lis[i];

    return result;
}

// function to calculate minimum
// number of deletions
int minimumNumberOfDeletions(int arr[], int n)
{
    // Find longest increasing subsequence
    int len = lis(arr, n);

    // After removing elements other than
    // the lis, we get sorted sequence.
    return (n - len);
}

// Driver program to test above
int main()
{
    int arr[] = {30, 40, 2, 5, 1, 7, 45, 50, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Minimum number of deletions = "
         << minimumNumberOfDeletions(arr, n);
    return 0;
}

Java

// Java implementation to find minimum number
// of deletions to make a sorted sequence

class Main
{
    /* lis() returns the length of the longest
    increasing subsequence in arr[] of size n */
    static int lis( int arr[], int n )
    {
        int result = 0;
        int[] lis = new int[n];
     
        /* Initialize LIS values for all indexes */
        for (int i = 0; i < n; i++ )
            lis[i] = 1;
     
        /* Compute optimized LIS values in bottom
            up manner */
        for (int i = 1; i < n; i++ )
            for (int j = 0; j < i; j++ )
                if ( arr[i] > arr[j] &&
                     lis[i] < lis[j] + 1)
                    lis[i] = lis[j] + 1;
     
        /* Pick resultimum of all LIS values */
        for (int i = 0; i < n; i++ )
            if (result < lis[i])
                result = lis[i];
     
        return result;
    }
    
    // function to calculate minimum
    // number of deletions
    static int minimumNumberOfDeletions(int arr[], int n)
    {
        // Find longest increasing subsequence
        int len = lis(arr, n);
     
        // After removing elements other than
        // the lis, we get sorted sequence.
        return (n - len);
    }

    // main function
    public static void main (String[] args) 
    {
        int arr[] = {30, 40, 2, 5, 1, 7, 45, 50, 8};
        int n = arr.length;
        System.out.println("Minimum number of deletions = " +
             minimumNumberOfDeletions(arr, n));
    }
}

/* This code is contributed by Harsh Agarwal */


Output:

Minimum number of deletions = 4 

Time Complexity: O(n2)

Time Complexity can be decreased to O(nlogn) by finding the Longest Increasing Subsequence Size(N Log N)

This article is contributed by Ayush Jauhari. 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.

GATE CS Corner    Company Wise Coding Practice

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

Recommended Posts:



2.7 Average Difficulty : 2.7/5.0
Based on 13 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.