Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on DP
  • Practice DP
  • MCQs on DP
  • Tutorial on Dynamic Programming
  • Optimal Substructure
  • Overlapping Subproblem
  • Memoization
  • Tabulation
  • Tabulation vs Memoization
  • 0/1 Knapsack
  • Unbounded Knapsack
  • Subset Sum
  • LCS
  • LIS
  • Coin Change
  • Word Break
  • Egg Dropping Puzzle
  • Matrix Chain Multiplication
  • Palindrome Partitioning
  • DP on Arrays
  • DP with Bitmasking
  • Digit DP
  • DP on Trees
  • DP on Graph
Open In App
Next Article:
Minimum number of deletions to make a string palindrome | Set 2
Next article icon

Minimum number of deletions to make a sorted sequence

Last Updated : 07 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an array of n integers. The task is to remove or delete the minimum number of elements from the array so that when the remaining elements are placed in the same sequence order to form an increasing 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
Recommended Practice
Minimum number of deletions to make a sorted sequence
Try It!

A simple solution is to remove all subsequences one by one and check if remaining set of elements is in sorted order or not. The 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 Code
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 GFG
{
    /* 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);
    }
 
    // Driver Code
    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 */
 
 

Python3




# Python3 implementation to find
# minimum number of deletions to
# make a sorted sequence
 
# lis() returns the length
# of the longest increasing
# subsequence in arr[] of size n
def lis(arr, n):
 
    result = 0
    lis = [0 for i in range(n)]
 
    # Initialize LIS values
    # for all indexes
    for i in range(n):
        lis[i] = 1
 
    # Compute optimized LIS values
    # in bottom up manner
    for i in range(1, n):
        for j in range(i):
            if ( arr[i] > arr[j] and
                lis[i] < lis[j] + 1):
                lis[i] = lis[j] + 1
 
    # Pick resultimum
    # of all LIS values
    for i in range(n):
        if (result < lis[i]):
            result = lis[i]
 
    return result
 
# Function to calculate minimum
# number of deletions
def minimumNumberOfDeletions(arr, n):
 
    # Find longest increasing
    # subsequence
    len = lis(arr, n)
 
    # After removing elements
    # other than the lis, we
    # get sorted sequence.
    return (n - len)
 
 
# Driver Code
arr = [30, 40, 2, 5, 1,
          7, 45, 50, 8]
n = len(arr)
print("Minimum number of deletions = ",
      minimumNumberOfDeletions(arr, n))
         
# This code is contributed by Anant Agarwal.
 
 

C#




// C# implementation to find
// minimum number of deletions
// to make a sorted sequence
using System;
 
class GfG
{
     
    /* 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);
    }
 
    // Driver Code
    public static void Main (String[] args)
    {
        int []arr = {30, 40, 2, 5, 1,
                       7, 45, 50, 8};
        int n = arr.Length;
        Console.Write("Minimum number of" +
                          " deletions = " +
         minimumNumberOfDeletions(arr, n));
    }
}
 
// This code is contributed by parashar.
 
 

Javascript




<script>
// javascript implementation to find
// minimum number of deletions
// to make a sorted sequence
/* lis() returns the length
of the longest increasing
subsequence in arr[] of size n */
function lis(arr,n)
{
    let result = 0;
    let lis= new Array(n);
 
    /* Initialize LIS values
    for all indexes */
    for (let i = 0; i < n; i++ )
        lis[i] = 1;
 
    /* Compute optimized LIS
    values in bottom up manner */
    for (let i = 1; i < n; i++ )
        for (let 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 (let i = 0; i < n; i++ )
        if (result < lis[i])
            result = lis[i];
 
    return result;
}
 
// function to calculate minimum
// number of deletions
function minimumNumberOfDeletions(arr,n)
{
 
    // Find longest increasing
    // subsequence
    let len = lis(arr,n);
 
    // After removing elements
    // other than the lis, we
    // get sorted sequence.
    return (n - len);
}
 
    let arr = [30, 40, 2, 5, 1,7, 45, 50, 8];
    let n = arr.length;
    document.write("Minimum number of deletions = "
    + minimumNumberOfDeletions(arr,n));
 
// This code is contributed by vaibhavrabadiya117.
</script>
 
 

PHP




<?php
// PHP implementation to find
// minimum number of deletions
// to make a sorted sequence
 
 
/* lis() returns the length of
   the longest increasing subsequence
   in arr[] of size n */
function lis( $arr, $n )
{
    $result = 0;
    $lis[$n] = 0;
 
    /* Initialize LIS values
       for all indexes */
    for ($i = 0; $i < $n; $i++ )
        $lis[$i] = 1;
 
    /* Compute optimized LIS
       values in bottom up manner */
    for ($i = 1; $i < $n; $i++ )
        for ($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 ($i = 0; $i < $n; $i++ )
        if ($result < $lis[$i])
            $result = $lis[$i];
 
    return $result;
}
 
// function to calculate minimum
// number of deletions
function minimumNumberOfDeletions($arr, $n)
{
    // Find longest increasing
    // subsequence
    $len = lis($arr, $n);
 
    // After removing elements
    // other than the lis, we
    // get sorted sequence.
    return ($n - $len);
}
 
// Driver Code
$arr = array(30, 40, 2, 5, 1,
               7, 45, 50, 8);
$n = sizeof($arr) / sizeof($arr[0]);
echo "Minimum number of deletions = " ,
    minimumNumberOfDeletions($arr, $n);
 
// This code is contributed by nitin mittal.
?>
 
 
Output
Minimum number of deletions = 4             

Time Complexity : O(n2)
Auxiliary Space: O(n)

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.

Approach#2: Using longest increasing subsequence

One approach to solve this problem is to find the length of the longest increasing subsequence (LIS) of the given array and subtract it from the length of the array. The difference gives us the minimum number of deletions required to make the array sorted.

Algorithm

1. Calculate the length of the longest increasing subsequence (LIS) of the array.
2. Subtract the length of the LIS from the length of the array.
3. Return the difference obtained in step 2 as the output.

C++




#include <iostream>
#include <vector>
#include <algorithm> // Required for max_element
using namespace std;
 
// Function to find the minimum number of deletions
int minDeletions(vector<int> arr) {
    int n = arr.size();
    vector<int> lis(n, 1); // Initialize LIS array with 1
     
    // Calculate LIS values
    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            if (arr[i] > arr[j]) {
                lis[i] = max(lis[i], lis[j] + 1); // Update LIS value
            }
        }
    }
     
    // Find the maximum length of LIS
    int maxLength = *max_element(lis.begin(), lis.end());
     
    // Return the minimum number of deletions
    return n - maxLength;
}
//Driver code
int main() {
    vector<int> arr = {5, 6, 1, 7, 4};
     
    // Call the minDeletions function and print the result
    cout << minDeletions(arr) << endl;
     
    return 0;
}
 
 

Java




import java.util.Arrays;
 
public class Main {
 
    public static int minDeletions(int[] arr) {
        int n = arr.length;
        int[] lis = new int[n];
        Arrays.fill(lis, 1); // Initialize the LIS array with all 1's
         
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[i] > arr[j]) {
                    lis[i] = Math.max(lis[i], lis[j] + 1);
                }
            }
        }
         
        return n - Arrays.stream(lis).max().getAsInt(); // Return the number of elements to delete
    }
 
    public static void main(String[] args) {
        int[] arr = {5, 6, 1, 7, 4};
        System.out.println(minDeletions(arr)); // Output: 2
    }
}
 
 

Python3




def min_deletions(arr):
    n = len(arr)
    lis = [1] * n
    for i in range(1, n):
        for j in range(i):
            if arr[i] > arr[j]:
                lis[i] = max(lis[i], lis[j] + 1)
    return n - max(lis)
 
arr = [5, 6, 1, 7, 4]
print(min_deletions(arr))
 
 

C#




using System;
using System.Collections.Generic;
using System.Linq;
 
namespace MinDeletionsExample
{
    class Program
    {
        static int MinDeletions(List<int> arr)
        {
            int n = arr.Count;
            List<int> lis = Enumerable.Repeat(1, n).ToList(); // Initialize LIS array with 1
 
            // Calculate LIS values
            for (int i = 1; i < n; ++i)
            {
                for (int j = 0; j < i; ++j)
                {
                    if (arr[i] > arr[j])
                    {
                        lis[i] = Math.Max(lis[i], lis[j] + 1); // Update LIS value
                    }
                }
            }
 
            // Find the maximum length of LIS
            int maxLength = lis.Max();
 
            // Return the minimum number of deletions
            return n - maxLength;
        }
         // Driver Code
        static void Main(string[] args)
        {
            List<int> arr = new List<int> { 5, 6, 1, 7, 4 };
 
            // Call the MinDeletions function and print the result
            Console.WriteLine(MinDeletions(arr));
 
            // Keep console window open until a key is pressed
            Console.ReadKey();
        }
    }
}
 
 

Javascript




function minDeletions(arr) {
  let n = arr.length;
  let lis = new Array(n).fill(1);
  for (let i = 1; i < n; i++) {
    for (let j = 0; j < i; j++) {
      if (arr[i] > arr[j]) {
        lis[i] = Math.max(lis[i], lis[j] + 1);
      }
    }
  }
  return n - Math.max(...lis);
}
 
let arr = [5, 6, 1, 7, 4];
console.log(minDeletions(arr));
 
 
Output
2             

Time complexity: O(n^2), where n is length of array
Space complexity: O(n), where n is length of array

Approach#3: Using binary search

This approach uses binary search to find the correct position to insert a given element into the subsequence.

Algorithm

1. Initialize a list ‘sub’ with the first element of the input list.
2. For each subsequent element in the input list, if it is greater than the last element in ‘sub’, append it to ‘sub’.
3. Otherwise, use binary search to find the correct position to insert the element into ‘sub’.
4. The minimum number of deletions required is equal to the length of the input list minus the length of ‘sub’.

C++




#include <iostream>
#include <vector>
 
using namespace std;
 
// Function to find the minimum number of deletions to make a strictly increasing subsequence
int minDeletions(vector<int>& arr) {
    int n = arr.size();
    vector<int> sub; // Stores the longest increasing subsequence
     
    sub.push_back(arr[0]); // Initialize the subsequence with the first element of the array
     
    for (int i = 1; i < n; i++) {
        if (arr[i] > sub.back()) {
            // If the current element is greater than the last element of the subsequence,
            // it can be added to the subsequence to make it longer.
            sub.push_back(arr[i]);
        } else {
            int index = -1; // Initialize index to -1
            int val = arr[i]; // Current element value
            int l = 0, r = sub.size() - 1; // Initialize left and right pointers for binary search
             
            // Binary search to find the index where the current element can be placed in the subsequence
            while (l <= r) {
                int mid = (l + r) / 2; // Calculate the middle index
                 
                if (sub[mid] >= val) {
                    index = mid; // Update the index if the middle element is greater or equal to the current element
                    r = mid - 1; // Move the right pointer to mid - 1
                } else {
                    l = mid + 1; // Move the left pointer to mid + 1
                }
            }
             
            if (index != -1) {
                sub[index] = val; // Replace the element at the found index with the current element
            }
        }
    }
 
    // The minimum number of deletions is equal to the difference between the input array size and the size of the longest increasing subsequence
    return n - sub.size();
}
 
int main() {
    vector<int> arr = {30, 40, 2, 5, 1, 7, 45, 50, 8};
    int output = minDeletions(arr);
    cout << output << endl;
     
    return 0;
}
 
 

Java




import java.util.ArrayList;
 
public class Main {
 
    // Function to find the minimum number of deletions to make a strictly increasing subsequence
    static int minDeletions(ArrayList<Integer> arr) {
        int n = arr.size();
        ArrayList<Integer> sub = new ArrayList<>(); // Stores the longest increasing subsequence
 
        sub.add(arr.get(0)); // Initialize the subsequence with the first element of the array
 
        for (int i = 1; i < n; i++) {
            if (arr.get(i) > sub.get(sub.size() - 1)) {
                // If the current element is greater than the last element of the subsequence,
                // it can be added to the subsequence to make it longer.
                sub.add(arr.get(i));
            } else {
                int index = -1; // Initialize index to -1
                int val = arr.get(i); // Current element value
                int l = 0, r = sub.size() - 1; // Initialize left and right pointers for binary search
 
                // Binary search to find the index where the current element can be placed in the subsequence
                while (l <= r) {
                    int mid = (l + r) / 2; // Calculate the middle index
 
                    if (sub.get(mid) >= val) {
                        index = mid; // Update the index if the middle element is greater or equal to the current element
                        r = mid - 1; // Move the right pointer to mid - 1
                    } else {
                        l = mid + 1; // Move the left pointer to mid + 1
                    }
                }
 
                if (index != -1) {
                    sub.set(index, val); // Replace the element at the found index with the current element
                }
            }
        }
 
        // The minimum number of deletions is equal to the difference between the input array size and the size of the longest increasing subsequence
        return n - sub.size();
    }
 
    public static void main(String[] args) {
        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(30);
        arr.add(40);
        arr.add(2);
        arr.add(5);
        arr.add(1);
        arr.add(7);
        arr.add(45);
        arr.add(50);
        arr.add(8);
 
        int output = minDeletions(arr);
        System.out.println(output);
    }
}
 
 

Python3




def min_deletions(arr):
    def ceil_index(sub, val):
        l, r = 0, len(sub)-1
        while l <= r:
            mid = (l + r) // 2
            if sub[mid] >= val:
                r = mid - 1
            else:
                l = mid + 1
        return l
 
    sub = [arr[0]]
    for i in range(1, len(arr)):
        if arr[i] > sub[-1]:
            sub.append(arr[i])
        else:
            sub[ceil_index(sub, arr[i])] = arr[i]
 
    return len(arr) - len(sub)
 
arr = [30, 40, 2, 5, 1, 7, 45, 50, 8]
output = min_deletions(arr)
print(output)
 
 

C#




using System;
using System.Collections.Generic;
 
class Program
{
    // Function to find the minimum number of deletions to make a strictly increasing subsequence
    static int MinDeletions(List<int> arr)
    {
        int n = arr.Count;
        List<int> sub = new List<int>(); // Stores the longest increasing subsequence
 
        sub.Add(arr[0]); // Initialize the subsequence with the first element of the array
 
        for (int i = 1; i < n; i++)
        {
            if (arr[i] > sub[sub.Count - 1])
            {
                // If the current element is greater than the last element of the subsequence,
                // it can be added to the subsequence to make it longer.
                sub.Add(arr[i]);
            }
            else
            {
                int index = -1; // Initialize index to -1
                int val = arr[i]; // Current element value
                int l = 0, r = sub.Count - 1; // Initialize left and right
                                              // pointers for binary search
 
                // Binary search to find the index where the current element
                // can be placed in the subsequence
                while (l <= r)
                {
                    int mid = (l + r) / 2; // Calculate the middle index
 
                    if (sub[mid] >= val)
                    {
                        index = mid; // Update the index if the middle element is
                                     // greater or equal to the current element
                        r = mid - 1; // Move the right pointer to mid - 1
                    }
                    else
                    {
                        l = mid + 1; // Move the left pointer to mid + 1
                    }
                }
 
                if (index != -1)
                {
                    sub[index] = val; // Replace the element at the found index
                                      // with the current element
                }
            }
        }
 
        // The minimum number of deletions is equal to the difference
        // between the input list size and the size of the
        // longest increasing subsequence
        return n - sub.Count;
    }
// Driver code
    static void Main()
    {
        List<int> arr = new List<int> { 30, 40, 2, 5, 1, 7, 45, 50, 8 };
        int output = MinDeletions(arr);
        Console.WriteLine(output);
 
        Console.ReadLine();
    }
}
 
 

Javascript




// Function to find the minimum number of deletions to make a strictly increasing subsequence
function minDeletions(arr) {
    let n = arr.length;
    let sub = []; // Stores the longest increasing subsequence
 
    sub.push(arr[0]); // Initialize the subsequence with the first element of the array
 
    for (let i = 1; i < n; i++) {
        if (arr[i] > sub[sub.length - 1]) {
            // If the current element is greater than the last element of the subsequence,
            // it can be added to the subsequence to make it longer.
            sub.push(arr[i]);
        } else {
            let index = -1; // Initialize index to -1
            let val = arr[i]; // Current element value
            let l = 0, r = sub.length - 1; // Initialize left and right pointers for binary search
 
            // Binary search to find the index where the current element can be placed
            // in the subsequence
            while (l <= r) {
                let mid = Math.floor((l + r) / 2); // Calculate the middle index
 
                if (sub[mid] >= val) {
                    index = mid; // Update the index if the middle element is greater
                                 //or equal to the current element
                    r = mid - 1; // Move the right pointer to mid - 1
                } else {
                    l = mid + 1; // Move the left pointer to mid + 1
                }
            }
 
            if (index !== -1) {
                sub[index] = val; // Replace the element at the found index with the current element
            }
        }
    }
 
    // The minimum number of deletions is equal to the difference
    //between the input array size and the size of the longest increasing subsequence
    return n - sub.length;
}
 
let arr = [30, 40, 2, 5, 1, 7, 45, 50, 8];
let output = minDeletions(arr);
console.log(output);
 
 
Output
4             

Time Complexity: O(n log n)

Auxiliary Space: O(n)



Next Article
Minimum number of deletions to make a string palindrome | Set 2

A

Ayush Jauhari
Improve
Article Tags :
  • DSA
  • Dynamic Programming
  • LIS
Practice Tags :
  • Dynamic Programming

Similar Reads

  • Minimum number of deques required to make the array sorted
    Given an array arr containing N unique integers. The task is to calculate the minimum number of deques required to make the array sorted. Example: Input: arr[] = {3, 6, 0, 9, 5, 4}Output: 2Explanation: Create a new deque d1 = {3}.Create a new deque d2 = {6}.Push 0 onto the front of d1; d1 = {0, 3}Pu
    9 min read
  • Minimum number of deletions to make a string palindrome | Set 2
    Given a string A, compute the minimum number of characters you need to delete to make resulting string a palindrome. Examples: Input : baca Output : 1 Input : geek Output : 2 We have discussed one approach in below post. Minimum number of deletions to make a string palindrome Below approach will use
    9 min read
  • Minimum Deletions to Make a String Palindrome
    Given a string s of length n, the task is to remove or delete the minimum number of characters from the string so that the resultant string is a palindrome. Note: The order of characters should be maintained. Examples : Input : s = "aebcbda"Output : 2Explanation: Remove characters 'e' and 'd'. Resul
    15+ min read
  • Minimum number of subtract operation to make an array decreasing
    You are given a sequence of numbers arr[0], arr[1], ..., arr[N - 1] and a positive integer K. In each operation, you may subtract K from any element of the array. You are required to find the minimum number of operations to make the given array decreasing. An array [Tex]arr[0], arr[1], ....., arr[N-
    7 min read
  • Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
    Given an array arr containing N integer elements, the task is to count the minimum number of elements that need to be changed such that all the elements (after proper rearrangement) make first N terms of Fibonacci Series. Examples: Input: arr[] = {4, 1, 2, 1, 3, 7} Output: 2 4 and 7 must be changed
    6 min read
  • Minimum number of deletions so that no two consecutive are same
    Given a string, find minimum number of deletions required so that there will be no two consecutive repeating characters in the string. Examples: Input : AAABBB Output : 4 Explanation : New string should be AB Input : ABABABAB Output : 0 Explanation : There are no consecutive repeating characters. If
    4 min read
  • Sort the elements by minimum number of operations
    Given two positive integer arrays X[] and Y[] of size N, Where all elements of X[] are distinct. Considering all the elements of X[] are lying side by side initially on a line, the task is to find the minimum number of operations required such that elements in X[] becomes in increasing order where i
    8 min read
  • Minimum array elements to be changed to make Recaman's sequence
    Given an array arr[] of N elements. The task is to find the minimum number of elements to be changed in the array such that the array contains first N Recaman's Sequence terms. Note that Recaman terms may be present in any order in the array. First few terms of Recaman's Sequence are: 0, 1, 3, 6, 2,
    7 min read
  • Minimum deletions to make String S the Subsequence of any String in the Set D
    Given a string S and a set of strings D, the task is to find the minimum number of deletions required to make the string S a subsequence of any string in the set D, such that the deletion can only be performed on the substring of S. Examples: Input: string S = "abcdefg", vector<string> D = {"a
    13 min read
  • Minimum number of steps required to place all 1s at a single index
    Given a binary array A[] of size N, where all 1s can be moved to its adjacent position, the task is to print an array res[] of size N, where res[i] contains the minimum number of steps required to move all the 1s at the ith index. Examples: Input: A[] = {1, 0, 1, 0}Output: {2, 2, 2, 4}Explanation: F
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences