Beautiful Number

Revision en2, by sajeed66999, 2021-08-25 16:33:42

You are given a number N. A number is called beautiful if, for every digit x in the number, there are x occurrences of it in the number.

Example:

1 is beautiful because 1 has 1 occurrence. 3133 is beautiful because 1 has 1 occurrence and 3 has 3 occurrences. 224 is not beautiful because 4 does not have 4 occurrences. Find the smallest beautiful number which is greater than N

Example: N=299 Output: 333

def solve(N):
    n=list(str(N))
    d={}
    for i in n:
        if i in d:
            d[i]+=1
        else:
            d[i]=1
    for i in d:
        if int(i)!=d[i]:
            return False
    return True
def beautifulNumber (N):
    i=1
    while(True):
        if solve(N+i):
            return(N+i)
        i=i+1
N = int(input())
print(beautifulNumber(N))

constraints: 1<N<10^12

Code takes a lot of time for huge numbers like 10^10. need it optimized.

Tags #python 3, #integers, #competitive programming

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English sajeed66999 2021-08-25 16:33:42 2 Tiny change: 'traints:\nN<10^12\n\' -> 'traints:\n1<N<10^12\n\'
en1 English sajeed66999 2021-08-25 16:33:01 935 Initial revision (published)