Length of smallest subarray required to be removed to make remaining elements consecutive
Last Updated :
09 Nov, 2023
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++14
// 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;
}
Java
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));
}
}
Python3
# 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)
C#
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
// 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));
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++
// 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);
}
// 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Length of smallest subarray to be removed to make sum of remaining elements divisible by K
Given an array arr[] of integers and an integer K, the task is to find the length of the smallest subarray that needs to be removed such that the sum of remaining array elements is divisible by K. Removal of the entire array is not allowed. If it is impossible, then print "-1". Examples: Input: arr[
11 min read
Length of smallest subarray to be removed such that the remaining array is sorted
Given an array arr[] consisting of N integers, the task is to print the length of the smallest subarray to be removed from arr[] such that the remaining array is sorted. Examples: Input: arr[] = {1, 2, 3, 10, 4, 2, 3, 5}Output: 3Explanation:The smallest subarray to be remove is {10, 4, 2} of length
8 min read
Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
Given an integer K and an array arr[] consisting of N integers, the task is to find the length of the subarray of smallest possible length to be removed such that the count of array elements smaller than and greater than K in the remaining array are equal. Examples: Input: arr[] = {5, 7, 2, 8, 7, 4,
10 min read
Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
Given a binary array arr[] consisting of N elements, the task is to find the maximum possible length of a subarray of only 1âs, after deleting a single pair of consecutive array elements. If no such subarray exists, print -1. Examples: Input: arr[] = {1, 1, 1, 0, 0, 1} Output: 4 Explanation: Removal
15+ min read
Minimum cost to make parity of elements same by removing Subarray
Given an arr[] of length N, the task is to make parity of arr[] the same by using the below-provided operation: Select a subarray containing elements of the same parity.Remove the subarray.The cost to remove that subarray is (absolute adjacent difference of all elements present in sub-array)*(length
15+ min read
Length of the Smallest Subarray that must be removed in order to Maximise the GCD
Given an array arr[] of N elements, the task is to find the length of the smallest subarray such that when this subarray is removed from the array, the GCD of the resultant array is maximum. Note: The resulting array should be non-empty.Examples: Input: N = 4, arr[] = {3, 6, 1, 2} Output: 2 Explanat
7 min read
Length of smallest Subarray with at least one element repeated K times
Given an array arr[] of length N and an integer K. The task is to find the minimum length of subarray such that at least one element of the subarray is repeated exactly K times in that subarray. If no such subarray exists, print -1. Examples: Input: arr[] = {1, 2, 1, 2, 1}, K = 2Output: 3Explanation
9 min read
Minimum array elements required to be subtracted from either end to reduce K to 0
Given an array arr[] consisting of N integers and an integer K, the task is to reduce K to 0 by removing an array element from either end of the array and subtracting it from K. If it is impossible to reduce K to 0, then print "-1". Otherwise, print the minimum number of such operations required. Ex
9 min read
Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M
Given an array arr[] of size N consisting of only the first M natural numbers, the task is to find the minimum length of the subarray that is required to be replaced such that the frequency of array elements is N / M. Note: N is a multiple of M. Examples: Input: M = 3, arr[] = {1, 1, 1, 1, 2, 3}Outp
10 min read
Minimum removals required to make frequency of all remaining array elements equal
Given an array arr[] of size N, the task is to find the minimum number of array elements required to be removed such that the frequency of the remaining array elements become equal. Examples : Input: arr[] = {2, 4, 3, 2, 5, 3}Output: 2Explanation: Following two possibilities exists:1) Either remove
13 min read