Replace each element by the difference of the total size of the array and frequency of that element
Given an array of integers, the task is to replace every element by the difference of the total size of the array and its frequency.
Examples:
Input: arr[] = { 1, 2, 5, 2, 2, 5, 4 }
Output: 6 4 5 4 4 5 6
Explanation:
Size of the array is 7.
The frequency of 1 is 1. So replace it by 7-1 = 6
The frequency of 2 is 3. So replace it by 7-3 = 4
Input: arr[] = { 4, 5, 4, 5, 6, 6, 6 }
Output: 5 5 5 5 4 4 4
Approach:
- Take a hash map, which will store the frequency of all the elements in the array.
- Now, traverse once again.
- Now, replace all the elements by the difference of the total size of the array and its frequency.
- Print the modified array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ReplaceElements( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; ++i)
mp[arr[i]]++;
for ( int i = 0; i < n; ++i)
arr[i] = n - mp[arr[i]];
}
int main()
{
int arr[] = { 1, 2, 5, 2, 2, 5, 4 };
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 GFG
{
static void ReplaceElements( int arr[], int n)
{
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (!mp.containsKey(arr[i]))
{
mp.put(arr[i], 1 );
}
else
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
}
for ( int i = 0 ; i < n; ++i)
{
arr[i] = n - mp.get(arr[i]);
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 5 , 2 , 2 , 5 , 4 };
int n = arr.length;
ReplaceElements(arr, n);
for ( int i = 0 ; i < n; ++i)
{
System.out.print(arr[i] + " " );
}
}
}
|
Python3
def ReplaceElements(arr, n):
mp = dict ()
for i in range (n):
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
for i in range (n):
arr[i] = n - mp[arr[i]]
arr = [ 1 , 2 , 5 , 2 , 2 , 5 , 4 ]
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 void ReplaceElements( int []arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (!mp.ContainsKey(arr[i]))
{
mp.Add(arr[i], 1);
}
else
{
var a = mp[arr[i]] + 1;
mp.Remove(arr[i]);
mp.Add(arr[i], a);
}
}
for ( int i = 0; i < n; ++i)
{
arr[i] = n - mp[arr[i]];
}
}
public static void Main()
{
int []arr = {1, 2, 5, 2, 2, 5, 4};
int n = arr.Length;
ReplaceElements(arr, n);
for ( int i = 0; i < n; ++i)
{
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
<script>
function ReplaceElements(arr, n)
{
let mp = new Map();
for (let i = 0; i < n; i++)
{
if (!mp.has(arr[i]))
{
mp.set(arr[i], 1);
}
else
{
mp.set(arr[i], mp.get(arr[i]) + 1);
}
}
for (let i = 0; i < n; ++i)
{
arr[i] = n - mp.get(arr[i]);
}
}
let arr = [ 1, 2, 5, 2, 2, 5, 4 ];
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 traversing all elements in the array.
Auxiliary Space: O(N) where N is for map to store the frequency of array elements.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...