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
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.
6 4 3 6
- Replace repeating elements with greater that greatest values
- 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
- Replace duplicates with greater than previous duplicate value
- Python | Check if all the values in a list that are greater than a given value
- Longest subsequence having greater corner values
- Longest Subarray with Sum greater than Equal to Zero
- Replace array elements by sum of next two consecutive elements
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Permute two arrays such that sum of every pair is greater or equal to K
- Longest subarray having average greater than or equal to x | Set-2
- Longest subarray having average greater than or equal to x
- Numbers with prime frequencies greater than or equal to k
- Adding elements of an array until every element becomes greater than or equal to k
- Sum of elements in an array with frequencies greater than or equal to that element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Sam007