Replace repeating elements with greater that greatest values
Given an integer array, if an integer is repeating then replace it with a number greater than that number that has not been inserted yet in the array.
Examples:
Input : arr = {1, 3, 4, 5, 3}
Output : 1 3 4 5 6
Explanation: Here 3 is repeating so it is replaced with 6
Input : arr = {1, 3, 4, 4, 5, 3}
Output : 1 3 4 6 5 7
We need to replace repeating numbers with a number that does not appear in the array and is one greater than the largest present in the array.
Source: Paytm Interview Experience (Backend Developer).
Calculate the maximum element in the array and replace the repeated elements with the maxx+1 and update the maxx element as visited. In the implementation, basic concept of hashing is used.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceElements( int arr[], int n)
{
int maxx = *max_element(arr, arr+n);
unordered_set< int > s;
for ( int i = 0; i < n; i++) {
if (s.find(arr[i]) == s.end())
s.insert(arr[i]);
else {
arr[i] = maxx + 1;
maxx++;
s.insert(maxx);
}
}
}
int main()
{
int arr[] = { 1, 3, 4, 5, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
replaceElements(arr, n);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.util.*;
class Solution
{
static int max_element( int arr[])
{
int max=arr[ 0 ];
for ( int i= 1 ;i<arr.length;i++)
{
if (max<arr[i])
max=arr[i];
}
return max;
}
static void replaceElements( int arr[], int n)
{
int maxx = max_element(arr);
Vector<Integer> s= new Vector<Integer>();
for ( int i = 0 ; i < n; i++) {
if (!s.contains(arr[i]))
s.add(arr[i]);
else {
arr[i] = maxx + 1 ;
maxx++;
s.add(maxx);
}
}
}
public static void main(String args[])
{
int arr[] = { 1 , 3 , 4 , 5 , 3 };
int n = arr.length;
replaceElements(arr, n);
for ( int i = 0 ; i < n; i++)
System.out.print( arr[i] + " " );
}
}
|
Python3
def replaceElements( arr, n):
maxx = max (arr)
s = []
for i in range (n) :
if arr[i] not in s:
s.append(arr[i])
else :
arr[i] = maxx + 1
maxx + = 1
s.append(maxx)
if __name__ = = "__main__" :
arr = [ 1 , 3 , 4 , 5 , 3 ]
n = len (arr)
replaceElements(arr, n)
for i in range ( n):
print (arr[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int max_element( int []arr)
{
int max = arr[0];
for ( int i = 1; i < arr.Length; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
static void replaceElements( int []arr, int n)
{
int maxx = max_element(arr);
List< int > s = new List< int >();
for ( int i = 0; i < n; i++)
{
if (!s.Contains(arr[i]))
s.Add(arr[i]);
else
{
arr[i] = maxx + 1;
maxx++;
s.Add(maxx);
}
}
}
public static void Main()
{
int []arr = { 1, 3, 4, 5, 3 };
int n = arr.Length;
replaceElements(arr, n);
for ( int i = 0; i < n; i++)
Console.Write( arr[i] + " " );
}
}
|
Javascript
<script>
function max_element(arr)
{
let max=arr[0];
for (let i=1;i<arr.length;i++)
{
if (max<arr[i])
max=arr[i];
}
return max;
}
function replaceElements(arr,n)
{
let maxx = max_element(arr);
let s=[];
for (let i = 0; i < n; i++) {
if (!s.includes(arr[i]))
s.push(arr[i]);
else {
arr[i] = maxx + 1;
maxx++;
s.push(maxx);
}
}
}
let arr=[1, 3, 4, 5, 3];
let n = arr.length;
replaceElements(arr, n);
for (let i = 0; i < n; i++)
document.write( arr[i] + " " );
</script>
|
Time complexity: O(n) where n is the number of elements in the given array
Auxiliary space: O(n) for storing elements in the HashSet.
Last Updated :
20 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...