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.
Below is the implementation of the above approach:
// C++ code for above approach: #include <bits/stdc++.h> using namespace std;
// Function to check if the elements // in a subarray from index i to j // form a consecutive sequence or not bool isConsecutive( int arr[], int i, int j)
{ // Sort the elements in the subarray
sort(arr + i, arr + j + 1);
for ( int k = i + 1; k <= j; k++) {
// Check if the elements in the
// sorted subarray are consecutive
if (arr[k] != arr[k - 1] + 1)
return false ;
}
return true ;
} // Function to find the length of the // smallest subarray required to be // removed to make the remaining array // elements consecutive int shortestSubarray( int arr[], int n)
{ // Initialize the minimum length to
// the size of the input array
int minLen = n;
for ( int i = 0; i < n; i++) {
// Generate all possible subarrays
// of the input array
for ( int j = i; j < n; j++) {
// Create a temporary array to store
// the remaining elements after
// removing a subarray
int temp[n];
int k = 0;
for ( int l = 0; l < n; l++) {
// Copy the elements from the input
// array to the temporary array, except
// those in the current subarray
if (l < i || l > j) {
temp[k++] = arr[l];
}
}
// Check if the remaining elements in the
// temporary array form a consecutive sequence
if (isConsecutive(temp, 0, k - 1)) {
// Update the minimum length of the
// subarray removed so far that
// satisfies the condition
minLen = min(minLen, j - i + 1);
}
}
}
// Return the minimum length
// of the subarray removed
return minLen;
} // Driver code int main()
{ int arr[] = { 1, 2, 3, 7, 5, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << shortestSubarray(arr, n) << endl;
return 0;
} |
import java.util.Arrays;
public class ShortestSubarray {
// Function to check if the elements in a subarray from index i to j
// form a consecutive sequence or not
static boolean isConsecutive( int [] arr, int i, int j) {
// Sort the elements in the subarray
Arrays.sort(arr, i, j + 1 );
for ( int k = i + 1 ; k <= j; k++) {
// Check if the elements in the sorted subarray are consecutive
if (arr[k] != arr[k - 1 ] + 1 ) {
return false ;
}
}
return true ;
}
// Function to find the length of the smallest subarray required to be
// removed to make the remaining array elements consecutive
static int shortestSubarray( int [] arr) {
int n = arr.length;
// Initialize the minimum length to the size of the input array
int minLen = n;
for ( int i = 0 ; i < n; i++) {
// Generate all possible subarrays of the input array
for ( int j = i; j < n; j++) {
// Create a temporary array to store the remaining elements
// after removing a subarray
int [] temp = new int [n];
int k = 0 ;
for ( int l = 0 ; l < n; l++) {
// Copy the elements from the input array to the temporary
// array, except those in the current subarray
if (l < i || l > j) {
temp[k++] = arr[l];
}
}
// Check if the remaining elements in the temporary array form
// a consecutive sequence
if (isConsecutive(temp, 0 , k - 1 )) {
// Update the minimum length of the subarray removed so far
// that satisfies the condition
minLen = Math.min(minLen, j - i + 1 );
}
}
}
// Return the minimum length of the subarray removed
return minLen;
}
// Driver code
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 7 , 5 , 4 , 5 };
System.out.println(shortestSubarray(arr));
}
} |
# Function to check if the elements in a subarray from index i to j # form a consecutive sequence or not def is_consecutive(arr, i, j):
# Sort the elements in the subarray
arr[i:j + 1 ] = sorted (arr[i:j + 1 ])
for k in range (i + 1 , j + 1 ):
# Check if the elements in the sorted subarray are consecutive
if arr[k] ! = arr[k - 1 ] + 1 :
return False
return True
# Function to find the length of the smallest subarray required to be # removed to make the remaining array elements consecutive def shortest_subarray(arr):
n = len (arr)
# Initialize the minimum length to the size of the input array
min_len = n
for i in range (n):
# Generate all possible subarrays of the input array
for j in range (i, n):
# Create a temporary array to store the remaining elements after
# removing a subarray
temp = [arr[l] for l in range (n) if l < i or l > j]
k = len (temp)
# Check if the remaining elements in the temporary array form a consecutive sequence
if is_consecutive(temp, 0 , k - 1 ):
# Update the minimum length of the subarray removed so far that satisfies the condition
min_len = min (min_len, j - i + 1 )
# Return the minimum length of the subarray removed
return min_len
# Driver code if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 7 , 5 , 4 , 5 ]
result = shortest_subarray(arr)
print (result)
|
using System;
class SmallestSubarrayRemoval {
// Function to check if the elements in a subarray form
// a consecutive sequence or not
static bool IsConsecutive( int [] arr, int i, int j)
{
// Sort the elements in the subarray
Array.Sort(arr, i, j - i + 1);
for ( int k = i + 1; k <= j; k++) {
// Check if the elements in the sorted subarray
// are consecutive
if (arr[k] != arr[k - 1] + 1)
return false ;
}
return true ;
}
// Function to find the length of the smallest subarray
// required to be removed
static int ShortestSubarray( int [] arr)
{
int n = arr.Length;
// Initialize the minimum length to the size of the
// input array
int minLen = n;
for ( int i = 0; i < n; i++) {
// Generate all possible subarrays of the input
// array
for ( int j = i; j < n; j++) {
// Create a temporary array to store the
// remaining elements after removing a
// subarray
int [] temp = new int [n];
int k = 0;
for ( int l = 0; l < n; l++) {
// Copy the elements from the input
// array to the temporary array, except
// those in the current subarray
if (l < i || l > j) {
temp[k++] = arr[l];
}
}
// Check if the remaining elements in the
// temporary array form a consecutive
// sequence
if (IsConsecutive(temp, 0, k - 1)) {
// Update the minimum length of the
// subarray removed so far that
// satisfies the condition
minLen = Math.Min(minLen, j - i + 1);
}
}
}
// Return the minimum length of the subarray removed
return minLen;
}
static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 7, 5, 4, 5 };
int result = ShortestSubarray(arr);
Console.WriteLine(result);
}
} |
// Javascript code for above approach: // Function to check if the elements // in a subarray from index i to j // form a consecutive sequence or not function isConsecutive(arr, i, j) {
// Sort the elements in the subarray
arr.slice(i, j + 1).sort();
for (let k = i + 1; k <= j; k++) {
// Check if the elements in the
// sorted subarray are consecutive
if (arr[k] !== arr[k - 1] + 1)
return false ;
}
return true ;
} // Function to find the length of the // smallest subarray required to be // removed to make the remaining array // elements consecutive function shortestSubarray(arr, n) {
// Initialize the minimum length to
// the size of the input array
let minLen = n;
for (let i = 0; i < n; i++) {
// Generate all possible subarrays
// of the input array
for (let j = i; j < n; j++) {
// Create a temporary array to store
// the remaining elements after
// removing a subarray
let temp = [];
let k = 0;
for (let l = 0; l < n; l++) {
// Copy the elements from the input
// array to the temporary array, except
// those in the current subarray
if (l < i || l > j) {
temp[k++] = arr[l];
}
}
// Check if the remaining elements in the
// temporary array form a consecutive sequence
if (isConsecutive(temp, 0, k - 1)) {
// Update the minimum length of the
// subarray removed so far that
// satisfies the condition
minLen = Math.min(minLen, j - i + 1);
}
}
}
// Return the minimum length
// of the subarray removed
return minLen;
} // Driver code let arr = [1, 2, 3, 7, 5, 4, 5]; let n = arr.length; console.log(shortestSubarray(arr, n)); |
2
Time Complexity: O(N3), where N is the size of the input array.
Auxiliary Space: O(N), as we are using an extra array of size N to store the subarray.
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++ 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 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 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# 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 |
<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);
}
// Print 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> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)