Problem · Dynamic Programming

Special Keyboard

Learn this problem
MediumZomato / EternalONSITE INTERVIEW

Problem statement

You have a special keyboard with four keys:

  • Key 1 prints A on the screen.
  • Key 2 performs Ctrl-A and selects the whole screen.
  • Key 3 performs Ctrl-C and copies the current selection into a buffer.
  • Key 4 performs Ctrl-V and appends the buffer to the screen.

Given an integer n, return the maximum number of A characters that can be produced using exactly n key presses.

Function

maxAWithSpecialKeyboard(n: int) → int

Examples

Example 1

n = 7return = 9

One optimal sequence is A, A, A, Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-V, producing 9 copies of A.

The source shared the rule but did not include this exact sample. FastPrep added this small example so the behavior can be checked directly.

More Zomato / Eternal problems

drafts saved locally
public int maxAWithSpecialKeyboard(int n) {
  // write your code here
}
n7
expected9
checking account