4 Keys Keyboard - Problem
4 Keys Keyboard - Maximize Character Output

Imagine you have a special keyboard with only four keys that can help you print the maximum number of 'A' characters on screen:

Key A: Print one 'A' on the screen
Ctrl-A: Select all text currently on screen
Ctrl-C: Copy the selected text to clipboard
Ctrl-V: Paste clipboard content, appending it to existing text

Given an integer n representing the maximum number of key presses allowed, your goal is to find the maximum number of 'A' characters you can display on screen.

Key Strategy: At some point, it becomes more efficient to use the copy-paste sequence (Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-V, ...) rather than just pressing 'A' repeatedly. The challenge is determining the optimal switching point!

Input & Output

example_1.py β€” Small Input
$ Input: n = 3
β€Ί Output: 3
πŸ’‘ Note: We can only press A 3 times: A A A. Copy-paste would require at least 4 operations (A, Ctrl-A, Ctrl-C, Ctrl-V), so it's not beneficial.
example_2.py β€” Optimal Copy-Paste
$ Input: n = 7
β€Ί Output: 9
πŸ’‘ Note: Optimal strategy: A A A (3 A's), then Ctrl-A Ctrl-C V V. This gives us 3 + 3 + 3 = 9 A's total. The sequence uses exactly 7 operations: A A A Ctrl-A Ctrl-C V V.
example_3.py β€” Larger Input
$ Input: n = 9
β€Ί Output: 12
πŸ’‘ Note: Best strategy: A A A A (4 A's), then Ctrl-A Ctrl-C V V V. This gives us 4 Γ— 3 = 12 A's total, using all 9 operations optimally.

Visualization

Tap to expand
4 Keys Keyboard Strategy (n=7 β†’ 9 A's)Operation Sequence:AAACtrl-ACtrl-CVVScreen State After Each Operation:Op 1: A β†’ Screen: "A" (1 A)Op 2: A β†’ Screen: "AA" (2 A's)Op 3: A β†’ Screen: "AAA" (3 A's)Op 4: Ctrl-A β†’ All text selectedOp 5: Ctrl-C β†’ "AAA" copied to clipboardOp 6: Ctrl-V β†’ Screen: "AAAAAA" (6 A's)Op 7: Ctrl-V β†’ Screen: "AAAAAAAAA" (9 A's) βœ“Why This Is Optimal:β€’ Alternative 1: Press A seven times β†’ 7 A's (suboptimal)β€’ Alternative 2: A, A, copy+paste β†’ 2Γ—4 = 8 A's (worse)β€’ Alternative 3: AΓ—4, copy+paste β†’ 4Γ—2 = 8 A's (worse)β€’ Our choice: AΓ—3, copy+pasteΓ—2 β†’ 3Γ—3 = 9 A's (optimal!)Key insight: Copy source of 3 A's with 2 paste operations maximizes result
Understanding the Visualization
1
Start Simple
For small n (≀3), just press A repeatedly
2
Consider Copy-Paste
At n=4+, evaluate: continue pressing A vs. copy-paste strategy
3
Find Break-Even Point
Copy-paste needs 3 operations minimum (Ctrl-A, Ctrl-C, Ctrl-V)
4
Optimal Strategy
Use DP to find best copy source for each position
5
Maximum Result
Choose strategy that gives maximum A's for given operations
Key Takeaway
🎯 Key Insight: The optimal strategy balances between building a good copy source and having enough operations left to paste multiple times. Usually copying when you have around n/3 to n/2 A's works best.

Time & Space Complexity

Time Complexity
⏱️
O(nΒ²)

For each of n positions, we check all previous positions as potential copy sources

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

DP array to store results for all positions from 1 to n

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ n ≀ 50
  • You can only use the four specified keys
  • All operations take exactly one key press
  • The screen starts empty
Asked in
Google 45 Amazon 35 Microsoft 28 Meta 22
38.0K Views
Medium Frequency
~18 min Avg. Time
950 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen