You are given an array of size ‘n’. You have to replace every pair of consecutive values ‘x’ by a single value ‘x+1’ every time until there is no such repetition left and then print the new array.

Input : 5, 2, 1, 1, 2, 2

Output : 5 4

Explanation:

step 1: While traversing, encountered pair of 1(gets replaced by 2. We get 5, 2, 2, 2, 2

step 2: The first encountered pair of 2 gets replaced by 3. We get 5, 3, 2, 2

step 3: Again pair of 2 gets replaced by 3. We get 5, 3, 3

step 4: Recently formed pair of 3 gets replaced by 4. We get 5, 4

This is our required answer.Input : 4, 5, 11, 2, 5, 7, 2

Output : 4 5 11 2 5 7 2

**Approach :** In this problem you have to traverse the array of integers and check if any two consecutive integers are of a same value X. Then you have to replace that pair of integers with a single integer X+1. After that you have to begin with a new step by re-traversing the array and performing the same operation.

## C++

`// C++ program to replace two elements with equal ` `// values with one greater. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to replace consecutive equal ` `// elements ` `void` `replace_elements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `pos = 0; ` `// Index in result ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `arr[pos++] = arr[i]; ` ` ` ` ` `while` `(pos > 1 && arr[pos - 2] == ` ` ` `arr[pos - 1]) { ` ` ` `pos--; ` ` ` `arr[pos - 1]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// to print new array ` ` ` `for` `(` `int` `i = 0; i < pos; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 6, 4, 3, 4, 3, 3, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `replace_elements(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to replace two elements ` `// with equal values with one greater. ` `public` `class` `GFG { ` ` ` ` ` `// Function to replace consecutive equal ` ` ` `// elements ` ` ` `static` `void` `replace_elements(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `pos = ` `0` `; ` `// Index in result ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `arr[pos++] = arr[i]; ` ` ` ` ` `while` `(pos > ` `1` `&& arr[pos - ` `2` `] == ` ` ` `arr[pos - ` `1` `]) ` ` ` `{ ` ` ` `pos--; ` ` ` `arr[pos - ` `1` `]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// to print new array ` ` ` `for` `(` `int` `i = ` `0` `; i < pos; i++) ` ` ` `System.out.print( arr[i] + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `arr[] = { ` `6` `, ` `4` `, ` `3` `, ` `4` `, ` `3` `, ` `3` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` `replace_elements(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## Python3

`# python program to replace two elements ` `# with equal values with one greater. ` `from` `__future__ ` `import` `print_function ` ` ` `# Function to replace consecutive equal ` `# elements ` `def` `replace_elements(arr, n): ` ` ` ` ` `pos ` `=` `0` `# Index in result ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `arr[pos] ` `=` `arr[i] ` ` ` `pos ` `=` `pos ` `+` `1` ` ` `while` `(pos > ` `1` `and` `arr[pos ` `-` `2` `] ` ` ` `=` `=` `arr[pos ` `-` `1` `]): ` ` ` `pos ` `-` `=` `1` ` ` `arr[pos ` `-` `1` `] ` `+` `=` `1` ` ` ` ` `# to print new array ` ` ` `for` `i ` `in` `range` `(` `0` `, pos): ` ` ` `print` `(arr[i], end` `=` `" "` `) ` ` ` `# Driver Code ` `arr ` `=` `[ ` `6` `, ` `4` `, ` `3` `, ` `4` `, ` `3` `, ` `3` `, ` `5` `] ` `n ` `=` `len` `(arr) ` `replace_elements(arr, n) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to replace two elements ` `// with equal values with one greater. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to replace consecutive equal ` ` ` `// elements ` ` ` `static` `void` `replace_elements(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `pos = 0; ` `// Index in result ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `arr[pos++] = arr[i]; ` ` ` ` ` `while` `(pos > 1 && arr[pos - 2] == ` ` ` `arr[pos - 1]) ` ` ` `{ ` ` ` `pos--; ` ` ` `arr[pos - 1]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// to print new array ` ` ` `for` `(` `int` `i = 0; i < pos; i++) ` ` ` `Console.Write( arr[i] + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = { 6, 4, 3, 4, 3, 3, 5 }; ` ` ` `int` `n = arr.Length; ` ` ` `replace_elements(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to replace two ` `// elements with equal ` `// values with one greater. ` ` ` `// Function to replace consecutive ` `// equal elements ` `function` `replace_elements(` `$arr` `,` `$n` `) ` `{ ` ` ` `// Index in result ` ` ` `$pos` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$arr` `[` `$pos` `++] = ` `$arr` `[` `$i` `]; ` ` ` ` ` `while` `(` `$pos` `> 1 && ` `$arr` `[` `$pos` `- 2] == ` ` ` `$arr` `[` `$pos` `- 1]) ` ` ` `{ ` ` ` `$pos` `--; ` ` ` `$arr` `[` `$pos` `- 1]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// to print new array ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$pos` `; ` `$i` `++) ` ` ` `echo` `$arr` `[` `$i` `] . ` `" "` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(6, 4, 3, 4, 3, 3, 5); ` ` ` `$n` `= ` `count` `(` `$arr` `); ` ` ` `replace_elements(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by Sam007. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6 4 3 6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Replace repeating elements with greater that greatest values
- Count of strings that become equal to one of the two strings after one removal
- Replace array elements by sum of next two consecutive elements
- Counting values greater than equal to x after increments
- Count elements such that there are exactly X elements with values greater than or equal to X
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Replace every element with the least greater element on its right
- Replace duplicates with greater than previous duplicate value
- Permute two arrays such that sum of every pair is greater or equal to K
- Find next greater element with no consecutive 1 in it's binary representation
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Python | Check if all the values in a list that are greater than a given value
- Longest subsequence having greater corner values
- Count the values greater than X in the modified array
- Sort first k values in ascending order and remaining n-k values in descending order
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Find minimum sum such that one of every three consecutive elements is taken
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.