Minimum Operations to Build n A's

You are given an integer n.

Initially, a notepad contains exactly one character 'A'. You can perform the following two operations:

  • Copy All: Copy all characters currently present on the screen. You cannot copy only a part of the text.
  • Paste: Paste the characters that were copied most recently.

Return the minimum number of operations required to make the screen contain exactly n characters 'A'.

Notes

  • Initially, the screen contains exactly one 'A'.
  • The clipboard is empty at the beginning.
  • A Paste operation can only paste the content copied by the most recent Copy All.
  • You must end with exactly n 'A' characters on the screen.

Example 1:

Input: n = 4

Output: 4

Explanation:

Start with "A".

  • Step 1: Copy All → clipboard = "A"
  • Step 2: Paste → screen = "AA"
  • Step 3: Paste → screen = "AAA"
  • Step 4: Paste → screen = "AAAA"

So, the minimum number of operations is 4.

Example 2:

Input: n = 6

Output: 5

Explanation:

One optimal sequence is:

Start with "A"

  • Step 1: Copy All
  • Step 2: Paste → "AA"
  • Step 3: Copy All
  • Step 4: Paste → "AAAA"
  • Step 5: Paste → "AAAAAA"

So, the minimum number of operations is 5.

Now Your Turn!

Pick the correct output for the given input

Input: n = 9

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int minSteps(int n) {
// Your code goes here
}
};
Test Case

Input:

N