2 Keys Keyboard

There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step:

  • Copy All: You can copy all the characters present on the screen; a partial copy is not allowed.
  • Paste: You can paste the characters which were copied last time.

Given an integer n, return the minimum number of operations needed to get the character 'A' exactly n times on the screen.

Example 1
Inputn = 3
Output3
Starting with one 'A', copy all once and paste twice to get exactly three 'A' characters in 3 operations.
Example 2
Inputn = 1
Output0
The screen already has exactly one 'A', so no operations are needed.

Constraints

  • 1 <= n <= 1000

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate