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
- Check if all digits of a number divide it
- Check if the sum of digits of a number N divides it
- Check if a number has digits in the given Order
- 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 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
- Check if a given number can be represented in given a no. of digits in any base
- Given a large number, check if a subsequence of digits is divisible by 8
- C Program for Sum the digits of a given number
- Program for Sum the digits of a given number
- Java Program for Sum the digits of a given number
- 8085 program to find sum of digits of 8 bit number
- Write a program to reverse digits of a number
- Program to find last two digits of Nth Fibonacci number
Improved By : Sam007