Check if frequency of each digit is less than the digit

Given an integer n, the task is to check if frequency of each digit of the number is less than or equal to digit itself.

Examples:

Input : 51241
Output : False

Input : 1425243
Output : True

Naive Approach: Start from 0 and count the frequency for every digit upto 9, if at any place frequency is more than the digit value then return false, else return true.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C++ program to validate a number
#include<bits/stdc++.h>
using namespace std;
  
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
bool validate(long long int n)
{
    for (int i=0; i<10; i++)
    {
        long long int temp = n; 
        int count = 0;
        while (temp)
        {
            // If current digit of temp is
            // same as i
            if (temp % 10 == i)
                count++;
  
            // if frequency is greater than 
            // digit value, return false
            if (count > i)
                return false;
  
            temp /= 10;
        }
    }
    return true;
}
  
// driver program
int main()
{
    long long int n = 1552793;
    if (validate(n))
        cout << "True";
    else
        cout << "False";
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to validate a number
import java .io.*;
  
public class GFG {
  
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
static boolean validate(long n)
{
    for (int i = 0; i < 10; i++)
    {
        long temp = n; 
        int count = 0;
        while (temp > 0)
        {
            // If current digit of 
            // temp is same as i
            if (temp % 10 == i)
                count++;
  
            // if frequency is greater than 
            // digit value, return false
            if (count > i)
                return false;
  
            temp /= 10;
        }
    }
    return true;
}
  
    // Driver Code
    static public void main (String[] args)
    {
            long n = 1552793;
        if (validate(n))
            System.out.println("True");
        else
            System.out.println("False");
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to validate a number
  
# Function to validate number (Check if 
# frequency of a digit is less than the
# digit itself or not)
def validate(n):
  
    for i in range(10):
        temp = n; 
        count = 0;
        while (temp):
              
            # If current digit of temp is 
            # same as i
            if (temp % 10 == i):
                count+=1;
  
            # if frequency is greater than 
            # digit value, return false
            if (count > i):
                return -1;
  
            temp //= 10;
  
    return 1;
  
# Driver Code
n = 1552793;
geek = "True" if validate(n) else "False";
print(geek);
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to validate a number
using System;
  
public class GFG {
  
// Function to validate number (Check if
// frequency of a digit is less than the
// digit itself or not)
static bool validate(long n)
{
    for (int i = 0; i < 10; i++)
    {
        long temp = n; 
        int count = 0;
        while (temp > 0)
        {
            // If current digit of 
            // temp is same as i
            if (temp % 10 == i)
                count++;
  
            // if frequency is greater than 
            // digit value, return false
            if (count > i)
                return false;
  
            temp /= 10;
        }
    }
    return true;
}
  
    // Driver Code
    static public void Main(String[] args)
    {
            long n = 1552793;
        if (validate(n))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to validate a number
  
// Function to validate number
// (Check if frequency of a
// digit is less than the
// digit itself or not)
function validate($n)
{
    for ($i = 0; $i < 10; $i++)
    {
        $temp = $n
        $count = 0;
        while ($temp)
        {
              
            // If current digit 
            // of temp is same 
            // as i
            if ($temp % 10 == $i)
                $count++;
  
            // if frequency is
            // greater than digit 
            // value, return false
            if ($count > $i)
                return -1;
  
            $temp /= 10;
        }
    }
    return 1;
}
  
    // Driver Code
    $n = 1552793;
    $geek = validate($n) ?"True" :"False";
    echo($geek);
  
// This code is contributed by Ajit
?>

chevron_right



Output:

True

 

Efficient Approach: is to store the frequency of each digit and if at any place frequency is more than the digit value then return false, else return true.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C++ program to validate a number
#include<bits/stdc++.h>
using namespace std;
  
// Function to validate number (Check if 
// frequency of a digit is less than the 
// digit itself or not)
bool validate(long long int n)
{
    int count[10] = {0};  
    while (n)
    {
        // calculate frequency of each digit
        int r = n % 10;
  
        // If count is already r, then
        // incrementing it would invalidate, 
        // hence we return false.
        if (count[r] == r)
           return false;  
  
        count[r]++;
        n /= 10;
    }
  
    return true;
}
  
// driver program
int main()
{
    long long int n = 1552793;
    if (validate(n))
        cout << "True";
    else
        cout << "False";
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to
// validate a number
import java.io.*;
  
class GFG 
{
      
// Function to validate
// number (Check if frequency 
// of a digit is less than 
// the digit itself or not)
static boolean validate(long n)
{
    int count[] = new int[10] ; 
    while (n > 0)
    {
        // calculate frequency 
        // of each digit
        int r = (int)n % 10;
  
        // If count is already r, 
        // then incrementing it 
        // would invalidate, 
        // hence we return false.
        if (count[r] == r)
        return false
  
        count[r]++;
        n /= 10;
    }
  
    return true;
}
  
// Driver Code
public static void main (String[] args)
{
    long n = 1552793;
    if (validate(n))
        System.out.println("True");
    else
        System.out.println("False");
}
}
  
// This code is contributed by ajit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A Python3 program to validate a number
import math as mt
  
# Function to validate number (Check if 
# frequency of a digit is less than the 
# digit itself or not)
def validate(n):
  
    count = [0 for i in range(10)] 
    while (n > 0):
      
        # calculate frequency of each digit
        r = n % 10
  
        # If count is already r, then
        # incrementing it would invalidate, 
        # hence we return false.
        if (count[r] == r):
            return False
  
        count[r] += 1
        n = n // 10
      
    return True
  
# Driver Code
n = 1552793
if (validate(n)):
    print("True")
else:
    print("False")
  
# This code is contributed by 
# Mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C# program to validate a number 
using System;
  
class GFG
{
// Function to validate number 
// (Check if frequency of a digit is 
// less than the digit itself or not) 
static bool validate(long n) 
    int []count = new int[10] ; 
    while (n > 0) 
    
        // calculate frequency 
        // of each digit 
        int r = (int)n % 10; 
  
        // If count is already r, then  
        // incrementing it would invalidate, 
        // hence we return false. 
        if (count[r] == r) 
        return false
  
        count[r]++; 
        n /= 10; 
    
  
    return true
  
// Driver Code 
static public void Main ()
{
    long n = 1552793; 
    if (validate(n)) 
        Console.WriteLine("True"); 
    else
        Console.WriteLine("False"); 
  
// This code is contributed by jit_t

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A PHP program to validate a number
  
// Function to validate number (Check if 
// frequency of a digit is less than the 
// digit itself or not)
function validate($n)
{
    $count=array(10);
    while ($n)
    {
        // calculate frequency of each digit
        $r = $n % 10;
  
        // If count is already r, then
        // incrementing it would invalidate, 
        // hence we return false.
        if (($count[$r] == $r))
        {
            return false; 
        }
  
        $count[$r] = $count[$r] + 1;
        $n = $n / 10;
    }
  
    return true;
}
  
// Driver Code
    $n = 1552793;
    $geek = validate($n) ?"True" :"False";
    echo($geek);
?>

chevron_right



Output:

True

This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.