Replacing an element makes array elements consecutive

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++

// CPP 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;
}
Output:

12

Time Complexity: O(n Log n)





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 11 vote(s)