Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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;
}


Java




// Java Difference between the
// summation of numbers
// in which the frequency of
// all digits are same and different
import java.util.*;
 
class GFG
{
 
// Function that returns the difference
static 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];
        HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
 
        // traverse for every digit
        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 pointing to the
        // first element in the array
        Iterator<Map.Entry<Integer, Integer>> it = mp.entrySet().iterator();
         
        // count of the smallest digit
        int freqdigit = it.next().getValue();
        int flag = 0;
 
        // check if all digits have same frequency or not
        for (Map.Entry<Integer,Integer> its : mp.entrySet())
        {
            if (its.getValue() != freqdigit)
            {
                flag = 1;
                break;
            }
        }
 
        // add to diff if not same
        if (flag == 1)
            diff += a[i];
        else
            same += a[i];
    }
 
    return same - diff;
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = { 24, 787, 2442, 101, 1212 };
    int n = a.length;
    System.out.print(difference(a, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 Difference between the
# summation of numbers
# in which the frequency of
# all digits are same and different
 
# Function that returns the difference
def difference(a, n):
     
    # Stores the sum of same
    # and different frequency digits
    same = 0
    diff = 0
     
    # traverse in the array
    for i in range(n):
         
        # duplicate of array element
        num = a[i]
        mp={}
         
        # traverse for every digit
        while (num):
            if num % 10 not in mp:
                mp[num % 10] = 0
            mp[num % 10] += 1
            num = num // 10
             
        # iterator pointing to the
        # first element in the array
        it = list(mp.keys())
         
        # count of the smallest digit
        freqdigit = mp[it[0]]
        flag = 0
         
        # check if all digits have same frequency or not
        for it in mp:
            if mp[it] != freqdigit:
                flag = 1
                break
             
        # add to diff if not same
        if (flag):
            diff += a[i]
        else:
            same += a[i]
     
    return same - diff
 
# Driver Code
a = [24, 787, 2442, 101, 1212]
n = len(a)
print(difference(a, n))
 
# This code is contributed by SHUBHAMSINGH10


C#




// C# implementation of the above Java code
using System;
using System.Collections.Generic;
 
class GFG {
  // Function that returns the difference
  static 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];
      Dictionary<int, int> mp
        = new Dictionary<int, int>();
 
      // traverse for every digit
      while (num > 0) {
        if (mp.ContainsKey(num % 10))
          mp[num % 10] = mp[num % 10] + 1;
        else
          mp.Add(num % 10, 1);
        num = num / 10;
      }
 
      // iterator pointing to the
      // first element in the array
      IEnumerator<KeyValuePair<int, int> > it
        = mp.GetEnumerator();
      it.MoveNext();
 
      // count of the smallest digit
      int freqdigit = it.Current.Value;
      int flag = 0;
 
      // check if all digits have same frequency or
      // not
      while (it.MoveNext()) {
        if (it.Current.Value != freqdigit) {
          flag = 1;
          break;
        }
      }
 
      // add to diff if not same
      if (flag == 1)
        diff += a[i];
      else
        same += a[i];
    }
 
    return same - diff;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] a = { 24, 787, 2442, 101, 1212 };
    int n = a.Length;
    Console.WriteLine(Difference(a, n));
  }
}
 
// This code is contributed by phasing17


Javascript




// Javascript Difference between the
// summation of numbers
// in which the frequency of
// all digits are same and different
 
 
// Function that returns the difference
function difference(a, n)
{
 
    // Stores the sum of same
    // and different frequency digits
    let same = 0;
    let diff = 0;
 
    // traverse in the array
    for (let i = 0; i < n; i++) {
        // duplicate of array element
        let num = a[i];
        let mp = {};
 
        // traverse for every digit
        while (num) {
             
            if(mp[num%10]){
                mp[num%10]++;
            }
            else{
                mp[num%10] = 1;
            }
            num = Math.floor(num / 10);
        }
 
        // count of the smallest digit
        let freqdigit = Object.values(mp)[0];;
        let flag = 0;
 
        // check if all digits have same frequency or not
        for (let it in mp) {
             
            if(mp[it] != freqdigit){
                flag = 1;
                break;
            }
        }
         
        // add to diff if not same
        if (flag)
            diff += a[i];
        else
            same += a[i];
    }
 
    return same - diff;
}
 
// Driver Code
let a = [ 24, 787, 2442, 101, 1212 ];
let n = a.length;
console.log(difference(a, n));
 
// The code is contributed by Nidhi goel.


Output: 

2790

 



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads