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
- Program to check if a number is divisible by any of its digits
- Program to check if a number is divisible by sum of its digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Check if two numbers have same number of digits
- Check if a number has digits in the given Order
- Check if the Xor of the frequency of all digits of a number N is zero or not
- Check if the sum of digits of a number N divides it
- Check if all digits of a number divide it
- Check if the given decimal number has 0 and 1 digits only
- Check if the frequency of all the digits in a number is same
- Check if a number with even number of digits is palindrome or not
- Check if the number is even or odd whose digits and base (radix) is given
- Check if a number is magic (Recursive sum of digits is 1)
- Check whether sum of digits at odd places of a number is divisible by K