VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-maximum-number-of-a-using-given-four-keys/

⇱ Print maximum number of ‘A’ using given four keys - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print maximum number of ‘A’ using given four keys

Last Updated : 23 Jul, 2025

Imagine you have a special keyboard with the following keys:

  • Key 1: Prints 'A' on screen
  • Key 2: (ctrl-A): Select screen
  • Key 3: (ctrl-C): Copy selection to buffer
  • Key 4: (ctrl-V): Print buffer on screen appending it after what has already been printed.

If you can only press the keyboard for N times (with the above four keys), write a program to produce maximum numbers of A’s. That is to say, the input parameter is N (No. of keys that you can press), the output is M (No. of As that you can produce).

Source: One97(Paytm) Interview Experience

Examples:

Input: N = 3
Output: 3
Explanation: We can at most get 3 A's on screen by pressing following key sequence. A, A, A

Input: N = 7
Output: 9
Explanation: We can at most get 9 A's on screen by pressing following key sequence. A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V

Input: N = 11
Output: 27
Explanation: We can at most get 27 A's on screen by pressing following key sequence. A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V, Ctrl A, Ctrl C, Ctrl V, Ctrl V

Approach:

Below are few important points to note.

  • For N < 7, the output is N itself.
  • Ctrl V can be used multiple times to print current buffer (See last two examples above). The idea to compute the optimal string length for N keystrokes by using a simple insight. The sequence of N keystrokes which produces an optimal string length will end with a suffix of Ctrl-A, a Ctrl-C, followed by only Ctrl-V's (For N > 6).

The task is to find out the breakpoint after which we get the above suffix of keystrokes. Definition of a breakpoint is that instance after which we need to only press Ctrl-A, Ctrl-C once and the only Ctrl-V’s afterwards to generate the optimal length. If we loop from N-3 to 1 and choose each of these values for the break-point, and compute that optimal string they would produce. Once the loop ends, we will have the maximum of the optimal lengths for various breakpoints, thereby giving us the optimal length for N keystrokes.

Below are the implementation of the above approach:


Output
Maximum Number of A's with 1 keystrokes is 1
Maximum Number of A's with 2 keystrokes is 2
Maximum Number of A's with 3 keystrokes is 3
Maximum Number of A's with 4 keystrokes is 4
Maximum Number of A'...

Approach (Dynamic Programming): The above function computes the same subproblems again and again. Recomputations of same subproblems can be avoided by storing the solutions to subproblems and solving problems in bottom up manner. where screen[n-1] is used to store result for N = n.

Below are the implementation of the above approach:


Output
Maximum Number of A's with 1 keystrokes is 1
Maximum Number of A's with 2 keystrokes is 2
Maximum Number of A's with 3 keystrokes is 3
Maximum Number of A's with 4 keystrokes is 4
Maximum Number of A'...
Comment
Article Tags: