Horje
Check if the frequency of all the digits in a number is same

Given a positive number ‘N’, the task is to find whether ‘N’ is balanced or not. Output ‘YES’ if ‘N’ is a balanced number else ‘NO’.

A number is balanced if the frequency of all the digits in it is same i.e. all the digits appear the same number of times.

Examples: 

Input: N = 1234567890
Output: YES
The frequencies of all the digits are same.
i.e. every digit appears same number of times.

Input: N = 1337
Output: NO

Approach: 

  • Create an array freq[] of size 10 which will store the frequency of each digit in ‘N’.
  • Then, check if all the digits of ‘N’ have the same frequency or not.
  • If yes then print ‘YES’ or ‘NO’ otherwise.

Below is the implementation of the above approach: 

C++

<?php
// PHP implementation of the approach
 
// returns true if the number
// passed as the argument
// is a balanced number.
function isNumBalanced($N)
{
    $st = "" . strval($N);
    $isBalanced = true;
 
    // frequency array to store
    // the frequencies of all
    // the digits of the number
    $freq = array_fill(0, 10, 0);
    $i = 0;
    $n = strlen($st);
 
    for ($i = 0; $i < $n; $i++)
 
        // store the frequency of
        // the current digit
        $freq[ord($st[$i]) - ord('0')]++;
 
    for ($i = 0; $i < 9; $i++)
    {
 
        // if freq[i] is not
        // equal to freq[i + 1] at
        // any index 'i' then set
        // isBalanced to false
        if ($freq[$i] != $freq[$i + 1])
            $isBalanced = false;
    }
 
    // return true if
    // the string is balanced
    if ($isBalanced)
        return true;
    else
        return false;
}
 
// Driver code
$N = 1234567890;
 
// Function call
$flag = isNumBalanced($N);
 
if ($flag)
    echo "YES\n";
else
    echo "NO\n";
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript implementation of
// the above approach
 
// Returns true if the number
// passed as the argument
// is a balanced number.
function isNumBalanced(N)
{
    var st = N;
    var isBalanced = true;
      
    // Frequency array to store
    // the frequencies of all
    // the digits of the number
    var freq = new Array(10);
    var i = 0;
    var n = st.length;
     
    for(i = 0; i < n; i++)
     
        // Store the frequency of
        // the current digit
        freq[st[i] - 0]++;
         
    for(i = 0; i < 9; i++)
    {
         
        // If freq[i] is not
        // equal to freq[i + 1] at
        // any index ā€˜i’ then set
        // isBalanced to false
        if (freq[i] != freq[i + 1])
            isBalanced = false;
    }
      
    // Return true if
    // the string is balanced
    if (isBalanced)
        return true;
    else
        return false;
}
 
// Driver code
var N = 1234567890;
 
// Function call
var flag = isNumBalanced(N);
 
if (flag)
    document.write("YES");
else
    document.write("NO");
 
// This code is contributed by bunnyram19
 
</script>

Output

NO

Complexity Analysis:

  • Time Complexity: O(NlogN), since the complexity for the sort function is n logn
  • Auxiliary Complexity: O(1), though there a frequency array but it takes only 10 space which is constant no of spaces hence the overall space complexity is constant



Reffered: https://www.geeksforgeeks.org


Mathematical

Related
Maximum difference elements that can added to a set Maximum difference elements that can added to a set
Largest number divisible by 90 that can be made using 0 and 5 Largest number divisible by 90 that can be made using 0 and 5
Count number of integers less than or equal to N which has exactly 9 divisors Count number of integers less than or equal to N which has exactly 9 divisors
Count of common multiples of two numbers in a range Count of common multiples of two numbers in a range
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays Sum of nth terms of Modified Fibonacci series made by every pair of two arrays

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9