- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ program to replace an element makes array elements consecutive

We are given an array of elements. We need to find if it is possible to change the value of any one element to make the array elements consecutive. If not possible, return -1 ; otherwise, the element needs to be changed.

Let's suppose we have an array {4, 3, 9, 5, 6} and we have to sort this given array. Then start from the smallest and largest element checking the number of mismatches. If the number of mismatches is more than 1 on both sides of the array, the answer is -1. Otherwise, it is possible to get the element that needs to change.

## Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Get the required values in the form of an array (input array).

A series of subsequent elements begins with the array's smallest element and moves forward by adding 1 to the previous element.

A series of subsequent elements begins with maximum element of array, then continues by subtracting 1 from previous.

Create the two series mentioned above, and then find each series' elements in the array. If there are more than one mismatches between the two series, then impossible. If one mismatch can be detected in any sequence, we have the answer.

## Example

Implementing a C++ program below, that replaces an array element such that all the array elements become consecutive −

#include <iostream> #include <vector> #include <algorithm> using namespace std; int solve(vector<int> arr) { sort(arr.begin(), arr.end()); int n = arr.size(); int mismatch = 0, ans; int nextElement = arr[n-1] - n + 1; for (int i=0; i<n-1; i++, nextElement++) { if (binary_search(arr.begin(), arr.end(), nextElement) == 0) { ans = arr[0]; mismatch++; } } if (mismatch == 1) return ans; if (mismatch == 0) return 0; mismatch = 0; nextElement = arr[0] + n - 1; for (int i=n-1;i>=1;i--, nextElement--) { if (binary_search(arr.begin(), arr.end(), nextElement) == 0) { ans = arr[n-1]; mismatch++; } } if (mismatch == 1) return ans; return -1; } int main() { vector<int> arr = {4, 3, 9, 5, 6}; int ans = solve(arr); if (ans == -1) cout << "Impossible"; else if (ans == 0) cout << "Already consecutive"; else cout << ans; return 0; }

Changing the element 9 to 2 or 7 makes the entire set of elements consecutive. The arraybecomes = {4, 3, 7, 5, 6} and the output will be −

## Output

9

## Conclusion

The algorithm's time complexity is **O(nlog(n)).** We can also solve the exact problem by using a different array and finding if the difference changes at more than one place in the array and several more approaches. You may give it a shot after understanding this basic approach.

- Related Questions & Answers
- Compress array to group consecutive elements JavaScript
- Consecutive elements sum array in JavaScript
- 8085 program to add two consecutive bytes of an array
- 8085 program to subtract two consecutive bytes of an array
- Construct an array from GCDs of consecutive elements in given array in C++
- Absolute Difference of all pairwise consecutive elements in an array (C++)?
- How do I recursively remove consecutive duplicate elements from an array?
- C program to reverse an array elements
- Python program to count pairs for consecutive elements
- How to replace elements in array with elements of another array in JavaScript?
- Check if three consecutive elements in an array is identical in JavaScript
- Count of only repeated element in a sorted array of consecutive elements in C++
- How to replace an element of an ArrayList in Java?
- Rearrange an array to minimize sum of product of consecutive pair elements in C++
- Recursive program to find an element in an array linearly.
- Java Program to Replace Elements in a List