Skip to content
Related Articles

Related Articles

Length of smallest subarray required to be removed to make remaining elements consecutive
  • Last Updated : 04 May, 2021

Given an array arr[] consisting of N integers, the task is to find the length of the smallest subarray required to be removed to make the remaining array elements consecutive.

Examples:

Input: arr[] = {1, 2, 3, 7, 5, 4, 5}
Output: 2
Explanation:
Removing the subarray {7, 5} from the array arr[] modifies the array to {1, 2, 3, 4, 5}, which makes all array elements consecutive. Therefore, the length of the subarray removed is 2, which is minimum.

Input: arr[] = {4, 5, 6, 8, 9, 10}
Output: 3

Naive Approach: The simplest approach to solve the given problem is to remove generate all possible subarrays of the array arr[] and for each of them, check if their removal makes the remaining array elements consecutive or not. After checking for all the subarrays, print the length of the minimum subarray obtained that satisfies the condition.



Time Complexity: O(N3)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by storing the length of the longest prefix and suffix of consecutive elements and then find the minimum length of the subarray required to be removed such that the concatenation of the prefix and suffix forms a sequence of consecutive elements. 
Follow the below steps to solve the problem:

  • Initialize two variables, say L as 0 and R as (N – 1) to store the ending indices of the longest prefix and starting index of the longest suffix of consecutive elements respectively.
  • Update the value of L to the first index where arr[i] + 1 is not equal to arr[i + 1] such that arr[0, …, L] is a consecutive prefix array.
  • Update the value of R to the first index from the end where arr[i] is not equal to arr[i – 1] + 1 such that arr[R, …, N – 1] is a consecutive suffix array.
  • Initialize a variable, say ans, to store the minimum of (N – L – 1) and R to store the required result.
  • If the value of arr[R] ≤ arr[L] + 1, then store the right index, R1 as arr[0, …, L, R1, …, N – 1] is a consecutive array.
  • If the value of (R1 – L – 1) is less than the value of ans, then update the value of ans to (R1 – L – 1).
  • After completing the above steps, print the value of the ans as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the length of the
// smallest subarray to be removed to
// make remaining array elements consecutive
void shortestSubarray(int* A, int N)
{
    int i;
 
    // Store the ending index of the
    // longest prefix consecutive array
    int left_index;
 
    // Traverse the array to find the
    // longest prefix consecutive sequence
    for (i = 0; i < N - 1; i++) {
        if (A[i] + 1 != A[i + 1])
            break;
    }
 
    // A[0...left_index] is the
    // prefix consecutive sequence
    left_index = i;
 
    // Store the starting index of the
    // longest suffix consecutive sequence
    int right_index;
 
    // Traverse the array to find the
    // longest suffix consecutive sequence
    for (i = N - 1; i >= 1; i--) {
        if (A[i] != A[i - 1] + 1)
            break;
    }
 
    // A[right_index...N-1] is
    // the consecutive sequence
    right_index = i;
 
    int updated_right;
 
    // Store the smallest subarray
    // required to be removed
    int minLength = min(N - left_index - 1,
                        right_index);
 
    // Check if subarray from the
    // middle can be removed
    if (A[right_index]
        <= A[left_index] + 1) {
 
        // Update the right index s.t.
        // A[0, N-1] is consecutive
        updated_right = right_index
                        + A[left_index]
                        - A[right_index] + 1;
 
        // If updated_right < N, then
        // update the minimumLength
        if (updated_right < N)
            minLength = min(minLength,
                            updated_right
                                - left_index - 1);
    }
 
    // Print the required result
    cout << minLength;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 7, 4, 3, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    shortestSubarray(arr, N);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the length of the
// smallest subarray to be removed to
// make remaining array elements consecutive
static void shortestSubarray(int A[], int N)
{
    int i;
 
    // Store the ending index of the
    // longest prefix consecutive array
    int left_index;
 
    // Traverse the array to find the
    // longest prefix consecutive sequence
    for(i = 0; i < N - 1; i++)
    {
        if (A[i] + 1 != A[i + 1])
            break;
    }
 
    // A[0...left_index] is the
    // prefix consecutive sequence
    left_index = i;
 
    // Store the starting index of the
    // longest suffix consecutive sequence
    int right_index;
 
    // Traverse the array to find the
    // longest suffix consecutive sequence
    for(i = N - 1; i >= 1; i--)
    {
        if (A[i] != A[i - 1] + 1)
            break;
    }
 
    // A[right_index...N-1] is
    // the consecutive sequence
    right_index = i;
 
    int updated_right;
 
    // Store the smallest subarray
    // required to be removed
    int minLength = Math.min(N - left_index - 1,
                             right_index);
 
    // Check if subarray from the
    // middle can be removed
    if (A[right_index] <= A[left_index] + 1)
    {
         
        // Update the right index s.t.
        // A[0, N-1] is consecutive
        updated_right = right_index + A[left_index] -
                     A[right_index] + 1;
 
        // If updated_right < N, then
        // update the minimumLength
        if (updated_right < N)
            minLength = Math.min(minLength,
                                 updated_right -
                                 left_index - 1);
    }
 
    // Print the required result
    System.out.println(minLength);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 7, 4, 3, 5 };
    int N = arr.length;
     
    shortestSubarray(arr, N);
}
}
 
// This code is contributed by Kingash

Python3




# Python3 program for the above approach
 
# Function to find the length of the
# smallest subarray to be removed to
# make remaining array elements consecutive
def shortestSubarray(A, N):
     
    i = 0
 
    # Store the ending index of the
    # longest prefix consecutive array
    left_index = 0
 
    # Traverse the array to find the
    # longest prefix consecutive sequence
    for i in range(N - 1):
        if (A[i] + 1 != A[i + 1]):
            break
 
    # A[0...left_index] is the
    # prefix consecutive sequence
    left_index = i
 
    # Store the starting index of the
    # longest suffix consecutive sequence
    right_index = 0
 
    # Traverse the array to find the
    # longest suffix consecutive sequence
    i = N - 1
     
    while (i >= 1):
        if (A[i] != A[i - 1] + 1):
            break
         
        i -= 1
 
    # A[right_index...N-1] is
    # the consecutive sequence
    right_index = i
 
    updated_right = 0
 
    # Store the smallest subarray
    # required to be removed
    minLength = min(N - left_index - 1, right_index)
 
    # Check if subarray from the
    # middle can be removed
    if (A[right_index] <= A[left_index] + 1):
         
        # Update the right index s.t.
        # A[0, N-1] is consecutive
        updated_right = (right_index + A[left_index] -
                                       A[right_index] + 1)
 
        # If updated_right < N, then
        # update the minimumLength
        if (updated_right < N):
            minLength = min(minLength, updated_right -
                                       left_index - 1)
 
    # Print the required result
    print(minLength)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 7, 4, 3, 5 ]
    N = len(arr)
     
    shortestSubarray(arr, N)
 
# This code is contributed by SURENDRA_GANGWAR

C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the length of the
// smallest subarray to be removed to
// make remaining array elements consecutive
static void shortestSubarray(int[] A, int N)
{
    int i;
 
    // Store the ending index of the
    // longest prefix consecutive array
    int left_index;
 
    // Traverse the array to find the
    // longest prefix consecutive sequence
    for(i = 0; i < N - 1; i++)
    {
        if (A[i] + 1 != A[i + 1])
            break;
    }
 
    // A[0...left_index] is the
    // prefix consecutive sequence
    left_index = i;
 
    // Store the starting index of the
    // longest suffix consecutive sequence
    int right_index;
 
    // Traverse the array to find the
    // longest suffix consecutive sequence
    for(i = N - 1; i >= 1; i--)
    {
        if (A[i] != A[i - 1] + 1)
            break;
    }
 
    // A[right_index...N-1] is
    // the consecutive sequence
    right_index = i;
 
    int updated_right;
 
    // Store the smallest subarray
    // required to be removed
    int minLength = Math.Min(N - left_index - 1,
                             right_index);
 
    // Check if subarray from the
    // middle can be removed
    if (A[right_index] <= A[left_index] + 1)
    {
         
        // Update the right index s.t.
        // A[0, N-1] is consecutive
        updated_right = right_index + A[left_index] -
                     A[right_index] + 1;
 
        // If updated_right < N, then
        // update the minimumLength
        if (updated_right < N)
            minLength = Math.Min(minLength,
                                 updated_right -
                                 left_index - 1);
    }
 
    // Print the required result
    Console.WriteLine(minLength);
}
 
// Driver code
static public void Main()
{
    int[] arr = { 1, 2, 3, 7, 4, 3, 5 };
    int N = arr.Length;
     
    shortestSubarray(arr, N);
}
}
 
// This code is contributed by offbeat

Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find the length of the
// smallest subarray to be removed to
// make remaining array elements consecutive
function shortestSubarray(A, N)
{
    let i;
  
    // Store the ending index of the
    // longest prefix consecutive array
    let left_index;
  
    // Traverse the array to find the
    // longest prefix consecutive sequence
    for(i = 0; i < N - 1; i++)
    {
        if (A[i] + 1 != A[i + 1])
            break;
    }
  
    // A[0...left_index] is the
    // prefix consecutive sequence
    left_index = i;
  
    // Store the starting index of the
    // longest suffix consecutive sequence
    let right_index;
  
    // Traverse the array to find the
    // longest suffix consecutive sequence
    for(i = N - 1; i >= 1; i--)
    {
        if (A[i] != A[i - 1] + 1)
            break;
    }
  
    // A[right_index...N-1] is
    // the consecutive sequence
    right_index = i;
  
    let updated_right;
  
    // Store the smallest subarray
    // required to be removed
    let minLength = Math.min(N - left_index - 1,
                             right_index);
  
    // Check if subarray from the
    // middle can be removed
    if (A[right_index] <= A[left_index] + 1)
    {
          
        // Update the right index s.t.
        // A[0, N-1] is consecutive
        updated_right = right_index + A[left_index] -
                     A[right_index] + 1;
  
        // If updated_right < N, then
        // update the minimumLength
        if (updated_right < N)
            minLength = Math.min(minLength,
                                 updated_right -
                                 left_index - 1);
    }
  
    // Prlet the required result
    document.write(minLength);
}
 
// Driver code
    let arr = [ 1, 2, 3, 7, 4, 3, 5 ];
    let N = arr.length;
      
    shortestSubarray(arr, N);
 
// This code is contributed by susmitakundugoaldanga.
</script>
Output: 
4

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :