# Difference between the summation of numbers whose frequency of all digits are same and different

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:

`// C++ Difference between the ` `// summation of numbers ` `// in which the frequency of ` `// all digits are same and different ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the difference ` `int` `difference(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// Stores the sum of same ` ` ` `// and different frequency digits ` ` ` `int` `same = 0; ` ` ` `int` `diff = 0; ` ` ` ` ` `// traverse in the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// duplicate of array element ` ` ` `int` `num = a[i]; ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// traverse for every digit ` ` ` `while` `(num) { ` ` ` `mp[num % 10]++; ` ` ` `num = num / 10; ` ` ` `} ` ` ` ` ` `// iterator pointing to the ` ` ` `// first element in the array ` ` ` `auto` `it = mp.begin(); ` ` ` ` ` `// count of the smallest digit ` ` ` `int` `freqdigit = (*it).second; ` ` ` `int` `flag = 0; ` ` ` ` ` `// check if all digits have same frequency or not ` ` ` `for` `(` `auto` `it = mp.begin(); it != mp.end(); it++) { ` ` ` `if` `((*it).second != freqdigit) { ` ` ` `flag = 1; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// add to diff if not same ` ` ` `if` `(flag) ` ` ` `diff += a[i]; ` ` ` `else` ` ` `same += a[i]; ` ` ` `} ` ` ` ` ` `return` `same - diff; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 24, 787, 2442, 101, 1212 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << difference(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2790

## Recommended Posts:

- Check if the Xor of the frequency of all digits of a number N is zero or not
- Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Replace each element by the difference of the total size of the array and frequency of that element
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Print numbers with digits 0 and 1 only such that their sum is N
- Print all numbers less than N with at-most 2 unique digits
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers in range such that digits in it and it's product with q are unequal
- Pair of prime numbers with a given sum and minimum absolute difference
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Generate a number such that the frequency of each digit is digit times the frequency in given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.