Maximum number of characters using keystrokes A, Ctrl+A, Ctrl+C and Ctrl+V

Posted by munda on Stack Overflow See other posts from Stack Overflow or by munda
Published on 2011-01-05T17:14:38Z Indexed on 2011/01/10 20:53 UTC
Read the original article Hit count: 268

This is an interview question from google. I am not able to solve it by myself. Can somebody shed some light?

Write a program to print the sequence of keystrokes such that it generates the maximum number of character 'A's. You are allowed to use only 4 keys: A, Ctrl+A, Ctrl+C and Ctrl+V. Only N keystrokes are allowed. All Ctrl+ characters are considered as one keystroke, so Ctrl+A is one keystroke.

For example, the sequence A, Ctrl+A, Ctrl+C, Ctrl+V generates two A's in 4 keystrokes.

  • Ctrl+A is Select All
  • Ctrl+C is Copy
  • Ctrl+V is Paste

I did some mathematics. For any N, using x numbers of A's , one Ctrl+A, one Ctrl+C and y Ctrl+V, we can generate max ((N-1)/2)2 number of A's. For some N > M, it is better to use as many Ctrl+A's, Ctrl+C and Ctrl+V sequences as it doubles the number of A's.

The sequence Ctrl+A, Ctrl+V, Ctrl+C will not overwrite the existing selection. It will append the copied selection to selected one.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about interview-questions