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++
#include<bits/stdc++.h>
using namespace std;
bool validate( long long int n)
{
for ( int i=0; i<10; i++)
{
long long int temp = n;
int count = 0;
while (temp)
{
if (temp % 10 == i)
count++;
if (count > i)
return false ;
temp /= 10;
}
}
return true ;
}
int main()
{
long long int n = 1552793;
if (validate(n))
cout << "True" ;
else
cout << "False" ;
return 0;
}
|
Java
import java .io.*;
public class GFG {
static boolean validate( long n)
{
for ( int i = 0 ; i < 10 ; i++)
{
long temp = n;
int count = 0 ;
while (temp > 0 )
{
if (temp % 10 == i)
count++;
if (count > i)
return false ;
temp /= 10 ;
}
}
return true ;
}
static public void main (String[] args)
{
long n = 1552793 ;
if (validate(n))
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python3
def validate(n):
for i in range ( 10 ):
temp = n;
count = 0 ;
while (temp):
if (temp % 10 = = i):
count + = 1 ;
if (count > i):
return - 1 ;
temp / / = 10 ;
return 1 ;
n = 1552793 ;
geek = "True" if validate(n) else "False" ;
print (geek);
|
C#
using System;
public class GFG {
static bool validate( long n)
{
for ( int i = 0; i < 10; i++)
{
long temp = n;
int count = 0;
while (temp > 0)
{
if (temp % 10 == i)
count++;
if (count > i)
return false ;
temp /= 10;
}
}
return true ;
}
static public void Main(String[] args)
{
long n = 1552793;
if (validate(n))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
PHP
<?php
function validate( $n )
{
for ( $i = 0; $i < 10; $i ++)
{
$temp = $n ;
$count = 0;
while ( $temp )
{
if ( $temp % 10 == $i )
$count ++;
if ( $count > $i )
return -1;
$temp /= 10;
}
}
return 1;
}
$n = 1552793;
$geek = validate( $n ) ? "True" : "False" ;
echo ( $geek );
?>
|
Javascript
<script>
function validate(n)
{
for (let i = 0; i < 10; i++)
{
let temp = n;
let count = 0;
while (temp > 0)
{
if (temp % 10 == i)
count++;
if (count > i)
return false ;
temp /= 10;
}
}
return true ;
}
let n = 1552793;
if (validate(n))
document.write( "True" );
else
document.write( "False" );
</script>
|
Output:
True
Time Complexity: O(10 * log10n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
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++
#include<bits/stdc++.h>
using namespace std;
bool validate( long long int n)
{
int count[10] = {0};
while (n)
{
int r = n % 10;
if (count[r] == r)
return false ;
count[r]++;
n /= 10;
}
return true ;
}
int main()
{
long long int n = 1552793;
if (validate(n))
cout << "True" ;
else
cout << "False" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean validate( long n)
{
int count[] = new int [ 10 ] ;
while (n > 0 )
{
int r = ( int )n % 10 ;
if (count[r] == r)
return false ;
count[r]++;
n /= 10 ;
}
return true ;
}
public static void main (String[] args)
{
long n = 1552793 ;
if (validate(n))
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python3
import math as mt
def validate(n):
count = [ 0 for i in range ( 10 )]
while (n > 0 ):
r = n % 10
if (count[r] = = r):
return False
count[r] + = 1
n = n / / 10
return True
n = 1552793
if (validate(n)):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
class GFG
{
static bool validate( long n)
{
int []count = new int [10] ;
while (n > 0)
{
int r = ( int )n % 10;
if (count[r] == r)
return false ;
count[r]++;
n /= 10;
}
return true ;
}
static public void Main ()
{
long n = 1552793;
if (validate(n))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
PHP
<?php
function validate( $n )
{
$count = array (10);
while ( $n )
{
$r = $n % 10;
if (( $count [ $r ] == $r ))
{
return false;
}
$count [ $r ] = $count [ $r ] + 1;
$n = $n / 10;
}
return true;
}
$n = 1552793;
$geek = validate( $n ) ? "True" : "False" ;
echo ( $geek );
?>
|
Javascript
<script>
function validate(n)
{
let count = new Uint8Array(10);
while (n)
{
let r = n % 10;
if (count[r] == r)
return false ;
count[r]++;
n = Math.floor(n / 10);
}
return true ;
}
let n = 1552793;
if (validate(n))
document.write( "True" );
else
document.write( "False" );
</script>
|
Output:
True
Time Complexity: O(log10n), where n represents the given integer.
Auxiliary Space: O(10), no extra space is required, so it is a constant.
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...