Check if frequency of each digit is less than the digit - GeeksforGeeks
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.
Read full article from Check if frequency of each digit is less than the digit - GeeksforGeeks
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.
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.
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
;
}