Replacing an element makes array elements consecutive
Last Updated :
11 Jul, 2025
Given an array of positive distinct integers. We need to find the only element whose replacement with any other value makes array elements distinct consecutive. If it is not possible to make array elements consecutive, return -1.
Examples :
Input : arr[] = {45, 42, 46, 48, 47}
Output : 42
Explanation: We can replace 42 with either
44 or 48 to make array consecutive.
Input : arr[] = {5, 6, 7, 9, 10}
Output : 5 [OR 10]
Explanation: We can either replace 5 with 8
or 10 with 8 to make array elements
consecutive.
Input : arr[] = {5, 6, 7, 9, 8}
Output : Array elements are already consecutive
A Naive Approach is to check each element of arr[], after replacing of which makes consecutive or not. Time complexity for this approach O(n2)
A Better Approach is based on an important observation that either the smallest or the largest element would be answer if answer exists. If answer exists, then there are two cases.
1) Series of consecutive elements starts with minimum element of array then continues by adding 1 to previous.
2) Series of consecutive elements start with maximum element of array, then continues by subtracting 1 from previous.
We make above two series and for every series, we search series elements in array. If for both series, number of mismatches are more than 1, then answer does not exist. If any series is found with one mismatch, then we have answer.
C++
// C++ program to find an element replacement
// of which makes the array elements consecutive.
#include <bits/stdc++.h>
using namespace std;
int findElement(int arr[], int n)
{
sort(arr, arr+n);
// Making a series starting from first element
// and adding 1 to every element.
int mismatch_count1 = 0, res;
int next_element = arr[n-1] - n + 1;
for (int i=0; i<n-1; i++) {
if (binary_search(arr, arr+n, next_element) == 0)
{
res = arr[0];
mismatch_count1++;
}
next_element++;
}
// If only one mismatch is found.
if (mismatch_count1 == 1)
return res;
// If no mismatch found, elements are
// already consecutive.
if (mismatch_count1 == 0)
return 0;
// Making a series starting from last element
// and subtracting 1 to every element.
int mismatch_count2 = 0;
next_element = arr[0] + n - 1;
for (int i=n-1; i>=1; i--) {
if (binary_search(arr, arr+n, next_element) == 0)
{
res = arr[n-1];
mismatch_count2++;
}
next_element--;
}
// If only one mismatch is found.
if (mismatch_count2 == 1)
return res;
return -1;
}
// Driver code
int main()
{
int arr[] = {7, 5, 12, 8} ;
int n = sizeof(arr)/sizeof(arr[0]);
int res = findElement(arr,n);
if (res == -1)
cout << "Answer does not exist";
else if (res == 0)
cout << "Elements are already consecutive";
else
cout << res;
return 0;
}
Java
// Java program to find an element
// replacement of which makes
// the array elements consecutive.
import java.io.*;
import java.util.Arrays;
class GFG
{
static int findElement(int []arr,
int n)
{
Arrays.sort(arr);
// Making a series starting
// from first element and
// adding 1 to every element.
int mismatch_count1 = 0,
res = 0;
int next_element = arr[n - 1] -
n + 1;
for (int i = 0; i < n - 1; i++)
{
if (Arrays.binarySearch(arr,
next_element) < 0)
{
res = arr[0];
mismatch_count1++;
}
next_element++;
}
// If only one mismatch is found.
if (mismatch_count1 == 1)
return res;
// If no mismatch found, elements
// are already consecutive.
if (mismatch_count1 == 0)
return 0;
// Making a series starting
// from last element and
// subtracting 1 to every element.
int mismatch_count2 = 0;
next_element = arr[0] + n - 1;
for (int i = n - 1; i >= 1; i--)
{
if (Arrays.binarySearch(arr,
next_element) < 0)
{
res = arr[n - 1];
mismatch_count2++;
}
next_element--;
}
// If only one mismatch is found.
if (mismatch_count2 == 1)
return res;
return -1;
}
// Driver code
public static void main(String args[])
{
int []arr = new int[]{7, 5, 12, 8} ;
int n = arr.length;
int res = findElement(arr,n);
if (res == -1)
System.out.print("Answer does not exist");
else if (res == 0)
System.out.print("Elements are " +
"already consecutive");
else
System.out.print(res);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Python3
# Python3 program to find an element
# replacement of which makes the
# array elements consecutive.
from bisect import bisect_left
def BinarySearch(a, x):
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
else:
return -1
def findElement(arr, n):
arr.sort()
# Making a series starting
# from first element and
# adding 1 to every element.
mismatch_count1 = 0
res = 0
next_element = arr[n - 1] - n + 1
for i in range(n - 1):
if (BinarySearch(arr, next_element) == -1):
res = arr[0]
mismatch_count1 += 1
next_element += 1
# If only one mismatch is found.
if (mismatch_count1 == 1):
return res
# If no mismatch found, elements are
# already consecutive.
if (mismatch_count1 == 0):
return 0
# Making a series starting from last element
# and subtracting 1 to every element.
mismatch_count2 = 0
next_element = arr[0] + n - 1
for i in range(n - 1, 0, -1):
if BinarySearch(arr, next_element) == -1:
res = arr[n - 1]
mismatch_count2 += 1
next_element -= 1
# If only one mismatch is found.
if(mismatch_count2 == 1):
return res
return -1
# Driver code
if __name__=="__main__":
arr = [ 7, 5, 12, 8 ]
n = len(arr)
res = findElement(arr, n)
if (res == -1):
print("Answer does not exist")
elif (res == 0):
print("Elements are already consecutive")
else:
print(res)
# This code is contributed by rutvik_56
C#
// C# program to find an element
// replacement of which makes
// the array elements consecutive.
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int findElement(int []arr,
int n)
{
Array.Sort(arr);
// Making a series starting
// from first element and
// adding 1 to every element.
int mismatch_count1 = 0, res = 0;
int next_element = arr[n - 1] - n + 1;
for (int i = 0; i < n - 1; i++)
{
if (Array.BinarySearch(arr,
next_element) < 0)
{
res = arr[0];
mismatch_count1++;
}
next_element++;
}
// If only one mismatch is found.
if (mismatch_count1 == 1)
return res;
// If no mismatch found, elements
// are already consecutive.
if (mismatch_count1 == 0)
return 0;
// Making a series starting
// from last element and
// subtracting 1 to every element.
int mismatch_count2 = 0;
next_element = arr[0] + n - 1;
for (int i = n - 1; i >= 1; i--)
{
if (Array.BinarySearch(arr,
next_element) < 0)
{
res = arr[n - 1];
mismatch_count2++;
}
next_element--;
}
// If only one mismatch is found.
if (mismatch_count2 == 1)
return res;
return -1;
}
// Driver code
static void Main()
{
int []arr = new int[]{7, 5, 12, 8} ;
int n = arr.Length;
int res = findElement(arr,n);
if (res == -1)
Console.Write("Answer does not exist");
else if (res == 0)
Console.Write("Elements are " +
"already consecutive");
else
Console.Write(res);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
JavaScript
<script>
// JavaScript program to find an element replacement
// of which makes the array elements consecutive.
function binary_search(arr, x) {
let start=0, end=arr.length-1;
// Iterate while start not meets end
while (start<=end){
// Find the mid index
let mid=Math.floor((start + end)/2);
// If element is present at mid, return True
if (arr[mid]===x) return mid;
// Else look in left or right half accordingly
else if (arr[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return -1;
}
function findElement( arr, n)
{
arr.sort(function(a,b){return a-b});
// Making a series starting from first element
// and adding 1 to every element.
let mismatch_count1 = 0, res;
let next_element = arr[n-1] - n + 1;
for (let i=0; i<n-1; i++) {
if (binary_search(arr, next_element) < 0)
{
res = arr[0];
mismatch_count1++;
}
next_element++;
}
// If only one mismatch is found.
if (mismatch_count1 == 1)
return res;
// If no mismatch found, elements are
// already consecutive.
if (mismatch_count1 == 0)
return 0;
// Making a series starting from last element
// and subtracting 1 to every element.
let mismatch_count2 = 0;
next_element = arr[0] + n - 1;
for (let i=n-1; i>=1; i--) {
if (binary_search(arr, next_element) < 0)
{
res = arr[n-1];
mismatch_count2++;
}
next_element--;
}
// If only one mismatch is found.
if (mismatch_count2 == 1)
return res;
return -1;
}
// Driver code
let a = [7, 5, 12, 8] ;
let N = a.length;
let res = findElement(a,N);
if (res == -1)
document.write( "Answer does not exist");
else if (res == 0)
document.write( "Elements are already consecutive");
else
document.write(res);
</script>
Time Complexity: O(n Log n)
Auxiliary Space: O(1)
Similar Reads
Make all Array elements equal by replacing it with adjacent elements Given an array A[] of size N, the task is to find the minimum number of moves required to make array elements equal where you can make adjacent elements equal in one move. Examples: Input: A = {1, 6, 5, 1, 7, 1}, N = 6Output: 3Explanation: Replace 6 with 1, 5 with 1, and then at last replace 7 with
5 min read
Make all Array elements equal by replacing consecutive occurrences of a number repeatedly Given an array arr[] of size N, the task is to find the minimum number of operations required to make all the array elements equal by following operation: Pick any number between 1 to N.Choose an element from the array,Replace all the consecutive equal elements with the picked number. Example: Input
8 min read
Find missing element in a sorted array of consecutive numbers Given an array arr[] of n distinct integers. Elements are placed sequentially in ascending order with one element missing. The task is to find the missing element.Examples: Input: arr[] = {1, 2, 4, 5, 6, 7, 8, 9} Output: 3Input: arr[] = {-4, -3, -1, 0, 1, 2} Output: -2Input: arr[] = {1, 2, 3, 4} Out
7 min read
Check if array elements are consecutive Given an unsorted array of numbers, the task is to check if the array consists of consecutive numbers. Examples: Input: arr[] = [5, 2, 3, 1, 4]Output: YesExplanation: Array has consecutive numbers from 1 to 5.Input: arr[] = [83, 78, 80, 81, 79, 82]Output: YesExplanation: Array has consecutive number
15+ min read
Find all ranges of consecutive numbers from Array Given a sorted array arr[] consisting of N integers without any duplicates, the task is to find the ranges of consecutive numbers from that array.Examples: Input: arr[] = {1, 2, 3, 6, 7} Output: 1->3, 6->7 Explanation: There are two ranges of consecutive number from that array. Range 1 = 1 -
10 min read
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements Given an array arr[] of size N, the task is to make all array elements equal to 0 by replacing all elements of a subsequences of equal elements by any integer, minimum number of times. Examples: Input: arr[] = {3, 7, 3}, N = 3Output: 2Explanation:Selecting a subsequence { 7 } and replacing all its e
5 min read