Given a number system with only 3 and 4. Find the nth number in the number system. First few numbers in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 3433, 3434, 3443, 3444, …
Source: Zoho Interview
We can generate all numbers with i digits using the numbers with (i-1) digits. The idea is to first add a ‘3’ as prefix in all numbers with (i-1) digit, then add a ‘4’. For example, the numbers with 2 digits are 33, 34, 43 and 44. The numbers with 3 digits are 333, 334, 343, 344, 433, 434, 443 and 444 which can be generated by first adding a 3 as prefix, then 4.
Following are detailed steps.
1) Create an array 'arr' of strings size n+1. 2) Initialize arr as empty string. (Number with 0 digits) 3) Do following while array size is smaller than or equal to n .....a) Generate numbers by adding a 3 as prefix to the numbers generated in previous iteration. Add these numbers to arr .....a) Generate numbers by adding a 4 as prefix to the numbers generated in previous iteration. Add these numbers to arr
Thanks to kaushik Lele for suggesting this idea in a comment here. Following is C++ implementation for the same.
3 4 33 34 43 44 333 334 343 344 433 434 443 444 3333
This article is contributed by Raman. 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.
- Convert the number from Indian system to International system
- Convert the number from International system to Indian system
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum number with digits as 4 and 7 only and given sum
- Find n-th element in a series with only 2 digits (4 and 7) allowed
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Find the n-th number made of even digits only
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Convert to number with digits as 3 and 8 only
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count number of binary strings of length N having only 0's and 1's
- Length of the smallest number which is divisible by K and formed by using 1's only
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Find whether only two parallel lines contain all coordinates points or not
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Implement *, - and / operations using only + arithmetic operator