A number is lucky if all digits of the number are different. How to check if a given number is lucky or not.
Input: n = 983 Output: true All digits are different Input: n = 9838 Output: false 8 appears twice
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to traverse through every digit of given number and mark the traversed digit as visited. Since total number of digits is 10, we need a boolean array of size only 10 to mark visited digits.
Below is the implementation of above idea.
1291 is not Lucky 897 is Lucky 4566 is not Lucky 1232 is not Lucky 80 is Lucky 700 is not Lucky
Time Complexity: O(d) where d is number of digits in input number.
Auxiliary Space: O(1)
This article is contributed by Himanshu. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Improved By : Sam007
- Check if a given number can be represented in given a no. of digits in any base
- Lucky Numbers
- Build Lowest Number by Removing n digits from a given number
- Given a number, find the next smallest palindrome
- Find Last Digit of a^b for Large Numbers
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Check if the first and last digit of the smallest number forms a prime
- Print all substring of a number without any conversion
- Complement of a number with any base b
- Check if Decimal representation of an Octal number is divisible by 7