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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Lucky Numbers
- Minimum digits to be removed to make either all digits or alternating digits same
- Check if the sum of digits of number is divisible by all of its digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count different numbers possible using all the digits their frequency times
- Program to count digits in an integer (4 Different Methods)
- Smallest number with given sum of digits and sum of square of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Sum of the digits of square of the given number which has only 1's as its digits
- Check if given number contains a digit which is the average of all other digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- C Program to Print all digits of a given number
- Numbers with sum of digits equal to the sum of digits of its all prime factor