Problem Statement

Given an integer, A. Find and Return first positive A integers in ascending order containing only digits 1, 2, and 3.

NOTE: All the A integers will fit in 32-bit integers.

Problem Constraints

1 <= A <= 29500

Input Format

The only argument given is integer A.

Output Format

Return an integer array denoting the first positive A integers in ascending order containing only digits 1, 2 and 3.

Example Input

Input 1:

 A = 3

Output 1:

 [1, 2, 3]

Explanation 1:

 Output denotes the first 3 integers that contains only digits 1, 2 and 3.

Input 2:

 A = 7

Output 2:

 [1, 2, 3, 11, 12, 13, 21]

Explanation 2:

 Output denotes the first 7 integers that contains only digits 1, 2 and 3.

Solution

  1. let our result be stored in a result array

  2. What are the numbers that should go into the result?

    1. Any number that contains 1 2 or 3

    2. the length of result should not exceed A

    3. What is the formula for a valid number?

      • 1 x 10 + 1 = 11
      • 1 x 10 + 2 = 12
      • 1 x 10 + 3 = 13
      • 2 x 10 + 1 = 21
      • 2 x 10 + 2 = 22
      • 2 x 10 + 3 = 23

      Therefore current x 10 + 1 or current x 10 + 2 or current x 10 + 3

  3. Loop till we get the desired length A

    1. current = remove first element of queue
    2. push current into result
    3. Calculate upcoming three elements and push in the queue

Code

solve : function(A){
		let queue = [1,2,3]
		let result = []

		while(result.length < A) {
			current = queue.shift()
			
			result.push(current)

			queue.push(current * 10 +1)
			queue.push(current * 10 +2)
			queue.push(current * 10 +3)

		}

		return result
	}