Replace two consecutive equal values with one greater
Last Updated :
17 Aug, 2022
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.
Example:
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void replace_elements( int arr[], int n)
{
int pos = 0;
for ( int i = 0; i < n; i++) {
arr[pos++] = arr[i];
while (pos > 1 && arr[pos - 2] ==
arr[pos - 1]) {
pos--;
arr[pos - 1]++;
}
}
for ( int i = 0; i < pos; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 6, 4, 3, 4, 3, 3, 5 };
int n = sizeof (arr) / sizeof ( int );
replace_elements(arr, n);
return 0;
}
|
Java
public class GFG {
static void replace_elements( int arr[], int n)
{
int pos = 0 ;
for ( int i = 0 ; i < n; i++) {
arr[pos++] = arr[i];
while (pos > 1 && arr[pos - 2 ] ==
arr[pos - 1 ])
{
pos--;
arr[pos - 1 ]++;
}
}
for ( int i = 0 ; i < pos; i++)
System.out.print( arr[i] + " " );
}
public static void main(String args[])
{
int arr[] = { 6 , 4 , 3 , 4 , 3 , 3 , 5 };
int n = arr.length;
replace_elements(arr, n);
}
}
|
Python3
from __future__ import print_function
def replace_elements(arr, n):
pos = 0
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
for i in range ( 0 , pos):
print (arr[i], end = " " )
arr = [ 6 , 4 , 3 , 4 , 3 , 3 , 5 ]
n = len (arr)
replace_elements(arr, n)
|
C#
using System;
class GFG {
static void replace_elements( int []arr, int n)
{
int pos = 0;
for ( int i = 0; i < n; i++) {
arr[pos++] = arr[i];
while (pos > 1 && arr[pos - 2] ==
arr[pos - 1])
{
pos--;
arr[pos - 1]++;
}
}
for ( int i = 0; i < pos; i++)
Console.Write( arr[i] + " " );
}
static void Main()
{
int []arr = { 6, 4, 3, 4, 3, 3, 5 };
int n = arr.Length;
replace_elements(arr, n);
}
}
|
PHP
<?php
function replace_elements( $arr , $n )
{
$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]++;
}
}
for ( $i = 0; $i < $pos ; $i ++)
echo $arr [ $i ] . " " ;
}
$arr = array (6, 4, 3, 4, 3, 3, 5);
$n = count ( $arr );
replace_elements( $arr , $n );
?>
|
Javascript
<script>
function replace_elements(arr, n) {
let pos = 0;
for (let i = 0; i < n; i++) {
arr[pos++] = arr[i];
while (pos > 1 && arr[pos - 2] ==
arr[pos - 1]) {
pos--;
arr[pos - 1]++;
}
}
for (let i = 0; i < pos; i++)
document.write(arr[i] + " " );
}
let arr = [6, 4, 3, 4, 3, 3, 5];
let n = arr.length
replace_elements(arr, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...