427. Find Number at Position
Asked in
Find Number at Position

A number game uses cards labeled from 1 to digitCount.

Form every possible number by arranging all the cards exactly once. Sort the numbers from smallest to largest.

Find the number at the given position in the sorted order.

Class

NumberGame

Method

findNumber

String findNumber( int digitCount, int position )

Parameters

  • digitCount: The number of cards used in the game.
  • position: The one-based position of the required number.

Returns

Return the number at the given position as a string.

Game Rules

  • The cards are labeled from 1 to digitCount.
  • Every card must be used exactly once.
  • The numbers are sorted from smallest to largest.
  • The smallest number is at position 1.

Constraints

  • 1 ≤ digitCount ≤ 9
  • 1 ≤ position ≤ digitCount!

Example 1

findNumber( digitCount = 4, position = 6 )

Output: "1432"

The sixth smallest number formed using cards 1 through 4 is "1432".

Example 2

findNumber( digitCount = 5, position = 73 )

Output: "41235"

The first 72 numbers begin with 1, 2, or 3. Therefore, the number at position 73 is the smallest number beginning with 4.

Example 3

findNumber( digitCount = 1, position = 1 )

Output: "1"

Only one number can be formed using a single card.



Please use Laptop/Desktop or any other large screen to add/edit code.