Maximum number of characters using keystrokes A, CtrlA, CtrlC and CtrlV
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
This classic optimization problem asks for the maximum number of A characters that can appear on screen after exactly N keystrokes, where the only allowed actions are A, Ctrl+A, Ctrl+C, and Ctrl+V. The key insight is that once N gets large enough, repeatedly typing A is no longer optimal and a copy-paste phase wins.
Why Greedy Intuition Is Not Enough
At first, typing A every time seems reasonable.
For small values of N, it is optimal:
- '
N = 1gives1' - '
N = 2gives2' - '
N = 3gives3' - '
N = 4gives4' - '
N = 5gives5' - '
N = 6gives6'
But at N = 7, a better plan appears:
AAACtrl+ACtrl+CCtrl+VCtrl+V
That produces 9 characters on screen, not 7. The reason is that after copying a block, each paste adds the entire copied block rather than a single character.
Dynamic Programming Formulation
Let dp[n] be the maximum number of characters possible with exactly n keystrokes.
There are two broad choices:
- press
Aand add one character - at some earlier point, perform
Ctrl+A,Ctrl+C, then use the remaining steps forCtrl+V
If you stop at step b to prepare the clipboard, then:
- steps
1..bproducedp[b]characters - step
b+1isCtrl+A - step
b+2isCtrl+C - the remaining
n - b - 2steps are pastes
That means the total becomes:
dp[b] * (n - b - 1)
The extra 1 comes from the original copied screen plus all pastes.
So the recurrence is:
A Simple Python Implementation
This prints the optimal answer for each step count. The algorithm is O(n^2), which is usually fine because interview and puzzle versions of the problem use modest values of N.
How to Think About the Best Breakpoint
The only interesting question is when to stop typing and switch to copy-paste. Every copy cycle consumes two setup keystrokes, so switching too early is wasteful. Switching too late misses the multiplication effect.
Dynamic programming works because it tests every possible breakpoint and remembers the best result for smaller step counts. You do not need a fragile rule like “always switch after four As.” The best breakpoint depends on N.
Common Pitfalls
- Forgetting that
Ctrl+AandCtrl+Ceach cost a keystroke and must be counted. - Assuming the best answer for
N = 7is8; the optimal result is9. - Writing a recurrence that counts only pastes and forgets the already copied screen content.
- Using a greedy shortcut without checking all possible breakpoints.
- Confusing “maximum after at most
Nkeystrokes” with “maximum after exactlyNkeystrokes.”
Summary
- For small
N, pressingArepeatedly is optimal. - After a threshold, copy-paste beats direct typing.
- Dynamic programming is the standard way to solve the problem correctly.
- The recurrence depends on choosing the best breakpoint before
Ctrl+AandCtrl+C. - A simple
O(n^2)solution is usually enough for this problem.
Related reading
- Maximum number of records in a MySQL database table
- Maximum number of threads in a .NET app?
- Maximum number of threads in a .NET app?
- Maximum product of coprime factors
- Maximum Product of Three Numbers
- Maximum product subsequence
- maximum sum of a subset of size K with sum less than M
- maximum sum subrectangle in a sparse matrix

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.