Difference between the summation of numbers whose frequency of all digits are same and different
Last Updated :
14 Mar, 2023
Given an array of N integers, find the difference between the summation of numbers whose frequency of all digits are same and different. For e.g. 8844, 1001, 56, 77, 34764673 are the examples of numbers where all digits have the same frequency. Similarly, 545, 44199, 76672, 202 are the examples of numbers whose frequency of digits are not the same.
Examples:
Input: a[] = {24, 787, 2442, 101, 1212}
Output: 2790
(2442 + 24 + 1212) – (787 + 101) = 2790
Input: a[]= {12321, 786786, 110022, 47, 22895}
Output: 861639
Approach: Traverse for every element in the array. Keep a count of all digits in a map. Once all digits are traversed in the element, check if the map contains the same frequency for all digits. If it contains the same frequency, then add it to same else add it to diff. After the array has been traversed completely, return the difference of both the elements.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int difference( int a[], int n)
{
int same = 0;
int diff = 0;
for ( int i = 0; i < n; i++) {
int num = a[i];
unordered_map< int , int > mp;
while (num) {
mp[num % 10]++;
num = num / 10;
}
auto it = mp.begin();
int freqdigit = (*it).second;
int flag = 0;
for ( auto it = mp.begin(); it != mp.end(); it++) {
if ((*it).second != freqdigit) {
flag = 1;
break ;
}
}
if (flag)
diff += a[i];
else
same += a[i];
}
return same - diff;
}
int main()
{
int a[] = { 24, 787, 2442, 101, 1212 };
int n = sizeof (a) / sizeof (a[0]);
cout << difference(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int difference( int a[], int n)
{
int same = 0 ;
int diff = 0 ;
for ( int i = 0 ; i < n; i++)
{
int num = a[i];
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
while (num > 0 )
{
if (mp.containsKey(num % 10 ))
mp.put(num % 10 , (mp.get(num % 10 ) + 1 ));
else
mp.put(num % 10 , 1 );
num = num / 10 ;
}
Iterator<Map.Entry<Integer, Integer>> it = mp.entrySet().iterator();
int freqdigit = it.next().getValue();
int flag = 0 ;
for (Map.Entry<Integer,Integer> its : mp.entrySet())
{
if (its.getValue() != freqdigit)
{
flag = 1 ;
break ;
}
}
if (flag == 1 )
diff += a[i];
else
same += a[i];
}
return same - diff;
}
public static void main(String[] args)
{
int a[] = { 24 , 787 , 2442 , 101 , 1212 };
int n = a.length;
System.out.print(difference(a, n));
}
}
|
Python3
def difference(a, n):
same = 0
diff = 0
for i in range (n):
num = a[i]
mp = {}
while (num):
if num % 10 not in mp:
mp[num % 10 ] = 0
mp[num % 10 ] + = 1
num = num / / 10
it = list (mp.keys())
freqdigit = mp[it[ 0 ]]
flag = 0
for it in mp:
if mp[it] ! = freqdigit:
flag = 1
break
if (flag):
diff + = a[i]
else :
same + = a[i]
return same - diff
a = [ 24 , 787 , 2442 , 101 , 1212 ]
n = len (a)
print (difference(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int Difference( int [] a, int n)
{
int same = 0;
int diff = 0;
for ( int i = 0; i < n; i++) {
int num = a[i];
Dictionary< int , int > mp
= new Dictionary< int , int >();
while (num > 0) {
if (mp.ContainsKey(num % 10))
mp[num % 10] = mp[num % 10] + 1;
else
mp.Add(num % 10, 1);
num = num / 10;
}
IEnumerator<KeyValuePair< int , int > > it
= mp.GetEnumerator();
it.MoveNext();
int freqdigit = it.Current.Value;
int flag = 0;
while (it.MoveNext()) {
if (it.Current.Value != freqdigit) {
flag = 1;
break ;
}
}
if (flag == 1)
diff += a[i];
else
same += a[i];
}
return same - diff;
}
public static void Main( string [] args)
{
int [] a = { 24, 787, 2442, 101, 1212 };
int n = a.Length;
Console.WriteLine(Difference(a, n));
}
}
|
Javascript
function difference(a, n)
{
let same = 0;
let diff = 0;
for (let i = 0; i < n; i++) {
let num = a[i];
let mp = {};
while (num) {
if (mp[num%10]){
mp[num%10]++;
}
else {
mp[num%10] = 1;
}
num = Math.floor(num / 10);
}
let freqdigit = Object.values(mp)[0];;
let flag = 0;
for (let it in mp) {
if (mp[it] != freqdigit){
flag = 1;
break ;
}
}
if (flag)
diff += a[i];
else
same += a[i];
}
return same - diff;
}
let a = [ 24, 787, 2442, 101, 1212 ];
let n = a.length;
console.log(difference(a, n));
|
Share your thoughts in the comments
Please Login to comment...