How to print maximum number of A’s using given four keys
Last Updated :
16 Dec, 2022
This is a famous interview question asked in Google, Paytm and many other company interviews.
Below is the problem statement.
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).
Examples:
Input: N = 3
Output: 3
We can at most get 3 A's on screen by pressing
following key sequence.
A, A, A
Input: N = 7
Output: 9
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
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
Below are few important points to note.
a) For N < 7, the output is N itself.
b) Ctrl V can be used multiple times to print current buffer (See last two examples above). The idea is 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 break=point 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 afterward 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 is implementation based on above idea.
C++14
#include <bits/stdc++.h>
using namespace std;
int findoptimal( int N)
{
if (N <= 6)
return N;
int max = 0;
int b;
for (b = N - 3; b >= 1; b--) {
int curr = (N - b - 1) * findoptimal(b);
if (curr > max)
max = curr;
}
return max;
}
int main()
{
int N;
for (N = 1; N <= 20; N++)
cout << "Maximum Number of A's with " << N <<
" keystrokes is " << findoptimal(N) << endl;
}
|
C
#include <stdio.h>
int findoptimal( int N)
{
if (N <= 6)
return N;
int max = 0;
int b;
for (b = N - 3; b >= 1; b--) {
int curr = (N - b - 1) * findoptimal(b);
if (curr > max)
max = curr;
}
return max;
}
int main()
{
int N;
for (N = 1; N <= 20; N++)
printf ( "Maximum Number of A's with %d keystrokes is %d\n" ,
N, findoptimal(N));
}
|
Java
import java.io.*;
class GFG {
static int findoptimal( int N)
{
if (N <= 6 )
return N;
int max = 0 ;
int b;
for (b = N - 3 ; b >= 1 ; b--) {
int curr = (N - b - 1 ) * findoptimal(b);
if (curr > max)
max = curr;
}
return max;
}
public static void main(String[] args)
{
int N;
for (N = 1 ; N <= 20 ; N++)
System.out.println( "Maximum Number of A's with keystrokes is " + N + findoptimal(N));
}
}
|
Python3
def findoptimal(N):
if N< = 6 :
return N
maxi = 0
for b in range (N - 3 , 0 , - 1 ):
curr = (N - b - 1 ) * findoptimal(b)
if curr>maxi:
maxi = curr
return maxi
if __name__ = = '__main__' :
for n in range ( 1 , 21 ):
print ( 'Maximum Number of As with ' , n, 'keystrokes is ' , findoptimal(n))
|
C#
using System;
class GFG {
static int findoptimal( int N)
{
if (N <= 6)
return N;
int max = 0;
int b;
for (b = N - 3; b >= 1; b--) {
int curr = (N - b - 1) * findoptimal(b);
if (curr > max)
max = curr;
}
return max;
}
static void Main()
{
int N;
for (N = 1; N <= 20; N++)
Console.WriteLine( "Maximum Number of A's with " + N + " keystrokes is " + findoptimal(N));
}
}
|
PHP
<?php
function findoptimal( $N )
{
if ( $N <= 6)
return $N ;
$max = 0;
$b ;
for ( $b = $N - 3; $b >= 1; $b -= 1)
{
$curr = ( $N - $b - 1) * findoptimal( $b );
if ( $curr > $max )
$max = $curr ;
}
return $max ;
}
$N ;
for ( $N = 1; $N <= 20; $N += 1)
echo ( "Maximum Number of A's with"
. $N . "keystrokes is " .findoptimal( $N ). "\n" );
?>
|
Javascript
<script>
function findoptimal(N)
{
if (N <= 6)
return N;
let max = 0;
let b;
for (b = N - 3; b >= 1; b--)
{
let curr = (N - b - 1) * findoptimal(b);
if (curr > max)
max = curr;
}
return max;
}
let N;
for (N = 1; N <= 20; N++)
document.write( "Maximum Number of A's with " +
N + " keystrokes is " +
findoptimal(N) + "<br>" );
</script>
|
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's with 5 keystrokes is 5
Maximum Number of A's with 6 keystrokes is 6
Maximum Number of A's with 7 keystrokes is 9
Maximum Number of A's with 8 keystrokes is 12
Maximum Number of A's with 9 keystrokes is 16
Maximum Number of A's with 10 keystrokes is 20
Maximum Number of A's with 11 keystrokes is 27
Maximum Number of A's with 12 keystrokes is 36
Maximum Number of A's with 13 keystrokes is 48
Maximum Number of A's with 14 keystrokes is 64
Maximum Number of A's with 15 keystrokes is 81
Maximum Number of A's with 16 keystrokes is 108
Maximum Number of A's with 17 keystrokes is 144
Maximum Number of A's with 18 keystrokes is 192
Maximum Number of A's with 19 keystrokes is 256
Maximum Number of A's with 20 keystrokes is 324
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 a bottom-up manner.
Below is Dynamic Programming based C implementation where an auxiliary array screen[N] is used to store result of subproblems.
C++
#include <iostream>
using namespace std;
int findoptimal( int N)
{
if (N <= 6)
return N;
int screen[N];
int b;
int n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++) {
screen[n - 1] = 0;
for (b = n - 3; b >= 1; b--) {
int curr = (n - b - 1) * screen[b - 1];
if (curr > screen[n - 1])
screen[n - 1] = curr;
}
}
return screen[N - 1];
}
int main()
{
int N;
for (N = 1; N <= 20; N++)
cout << "Maximum Number of A's with " <<N<< " keystrokes is "
<< findoptimal(N) << endl;
}
|
C
#include <stdio.h>
int findoptimal( int N)
{
if (N <= 6)
return N;
int screen[N];
int b;
int n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++) {
screen[n - 1] = 0;
for (b = n - 3; b >= 1; b--) {
int curr = (n - b - 1) * screen[b - 1];
if (curr > screen[n - 1])
screen[n - 1] = curr;
}
}
return screen[N - 1];
}
int main()
{
int N;
for (N = 1; N <= 20; N++)
printf ( "Maximum Number of A's with %d keystrokes is %d\n" ,
N, findoptimal(N));
}
|
Java
import java.io.*;
class GFG {
static int findoptimal( int N)
{
if (N <= 6 )
return N;
int screen[] = new int [N];
int b;
int n;
for (n = 1 ; n <= 6 ; n++)
screen[n - 1 ] = n;
for (n = 7 ; n <= N; n++) {
screen[n - 1 ] = 0 ;
for (b = n - 3 ; b >= 1 ; b--) {
int curr = (n - b - 1 ) * screen[b - 1 ];
if (curr > screen[n - 1 ])
screen[n - 1 ] = curr;
}
}
return screen[N - 1 ];
}
public static void main(String[] args)
{
int N;
for (N = 1 ; N <= 20 ; N++)
System.out.println( "Maximum Number of A's with keystrokes is " + N + findoptimal(N));
}
}
|
Python3
def findoptimal(N):
if (N < = 6 ):
return N
screen = [ 0 ] * N
for n in range ( 1 , 7 ):
screen[n - 1 ] = n
for n in range ( 7 , N + 1 ):
screen[n - 1 ] = 0
for b in range (n - 3 , 0 , - 1 ):
curr = (n - b - 1 ) * screen[b - 1 ]
if (curr > screen[n - 1 ]):
screen[n - 1 ] = curr
return screen[N - 1 ]
if __name__ = = "__main__" :
for N in range ( 1 , 21 ):
print ( "Maximum Number of A's with " , N, " keystrokes is " ,
findoptimal(N))
|
C#
using System;
public class GFG {
static int findoptimal( int N)
{
if (N <= 6)
return N;
int [] screen = new int [N];
int b;
int n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++) {
screen[n - 1] = 0;
for (b = n - 3; b >= 1; b--) {
int curr = (n - b - 1) * screen[b - 1];
if (curr > screen[n - 1])
screen[n - 1] = curr;
}
}
return screen[N - 1];
}
public static void Main(String[] args)
{
int N;
for (N = 1; N <= 20; N++)
Console.WriteLine( "Maximum Number of A's with {0} keystrokes is {1}\n" ,
N, findoptimal(N));
}
}
|
Javascript
<script>
function findoptimal(N)
{
if (N <= 6)
return N;
let screen = new Array(N);
for (let i = 0; i < N; i++)
{
screen[i] = 0;
}
let b;
let n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++)
{
screen[n - 1] = 0;
for (b = n - 3; b >= 1; b--)
{
let curr = (n - b - 1) * screen[b - 1];
if (curr > screen[n - 1])
screen[n - 1] = curr;
}
}
return screen[N - 1];
}
let N;
for (N = 1; N <= 20; N++)
document.write( "Maximum Number of A's with " +
N + " keystrokes is " +
findoptimal(N) + "<br>" );
</script>
|
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's with 5 keystrokes is 5
Maximum Number of A's with 6 keystrokes is 6
Maximum Number of A's with 7 keystrokes is 9
Maximum Number of A's with 8 keystrokes is 12
Maximum Number of A's with 9 keystrokes is 16
Maximum Number of A's with 10 keystrokes is 20
Maximum Number of A's with 11 keystrokes is 27
Maximum Number of A's with 12 keystrokes is 36
Maximum Number of A's with 13 keystrokes is 48
Maximum Number of A's with 14 keystrokes is 64
Maximum Number of A's with 15 keystrokes is 81
Maximum Number of A's with 16 keystrokes is 108
Maximum Number of A's with 17 keystrokes is 144
Maximum Number of A's with 18 keystrokes is 192
Maximum Number of A's with 19 keystrokes is 256
Maximum Number of A's with 20 keystrokes is 324
Thanks to Gaurav Saxena for providing the above approach to solve this problem.
As the number of A’s become large, the effect of pressing Ctrl-V more than 3 times starts to become insubstantial as compared to just pressing Ctrl-A, Ctrl-C and Ctrl-V again. So, the above code can be made more efficient by checking the effect of pressing Ctrl-V for 1, 2, and 3 times only.
C++
#include <bits/stdc++.h>
using namespace std;
int findoptimal( int N)
{
if (N <= 6)
return N;
int screen[N];
int b;
int n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++) {
screen[n - 1] = max(2 * screen[n - 4],
max(3 * screen[n - 5],
4 * screen[n - 6]));
}
return screen[N - 1];
}
int main()
{
int N;
for (N = 1; N <= 20; N++)
printf ( "Maximum Number of A's with %d keystrokes is %d\n" ,
N, findoptimal(N));
}
|
Java
class GFG
{
static int findoptimal( int N)
{
if (N <= 6 )
return N;
int []screen = new int [N];
int b;
int n;
for (n = 1 ; n <= 6 ; n++)
screen[n - 1 ] = n;
for (n = 7 ; n <= N; n++)
{
screen[n - 1 ] = Math.max( 2 * screen[n - 4 ],
Math.max( 3 * screen[n - 5 ],
4 * screen[n - 6 ]));
}
return screen[N - 1 ];
}
public static void main(String[] args)
{
int N;
for (N = 1 ; N <= 20 ; N++)
System.out.printf( "Maximum Number of A's with" +
" %d keystrokes is %d\n" ,
N, findoptimal(N));
}
}
|
Python3
def findoptimal(N):
if (N < = 6 ):
return N
screen = [ 0 ] * N
for n in range ( 1 , 7 ):
screen[n - 1 ] = n
for n in range ( 7 , N + 1 ):
screen[n - 1 ] = max ( 2 * screen[n - 4 ],
max ( 3 * screen[n - 5 ],
4 * screen[n - 6 ]));
return screen[N - 1 ]
if __name__ = = "__main__" :
for N in range ( 1 , 21 ):
print ( "Maximum Number of A's with " , N,
" keystrokes is " , findoptimal(N))
|
C#
using System;
class GFG
{
static int findoptimal( int N)
{
if (N <= 6)
return N;
int []screen = new int [N];
int n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++)
{
screen[n - 1] = Math.Max(2 * screen[n - 4],
Math.Max(3 * screen[n - 5],
4 * screen[n - 6]));
}
return screen[N - 1];
}
public static void Main(String[] args)
{
int N;
for (N = 1; N <= 20; N++)
Console.Write( "Maximum Number of A's with" +
" {0} keystrokes is {1}\n" ,
N, findoptimal(N));
}
}
|
Javascript
<script>
function findoptimal(N){
if (N <= 6)
return N;
let screen = [];
let b;
let n;
for (n = 1; n <= 6; n++)
screen[n - 1] = n;
for (n = 7; n <= N; n++) {
screen[n - 1] = Math.max(2 * screen[n - 4],
Math.max(3 * screen[n - 5],
4 * screen[n - 6]));
}
return screen[N - 1];
}
let N;
for (N = 1; N <= 20; N++)
document.write(
"Maximum Number of A's with " + N + " keystrokes is "
+findoptimal(N)+ "<br>"
);
</script>
|
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's with 5 keystrokes is 5
Maximum Number of A's with 6 keystrokes is 6
Maximum Number of A's with 7 keystrokes is 9
Maximum Number of A's with 8 keystrokes is 12
Maximum Number of A's with 9 keystrokes is 16
Maximum Number of A's with 10 keystrokes is 20
Maximum Number of A's with 11 keystrokes is 27
Maximum Number of A's with 12 keystrokes is 36
Maximum Number of A's with 13 keystrokes is 48
Maximum Number of A's with 14 keystrokes is 64
Maximum Number of A's with 15 keystrokes is 81
Maximum Number of A's with 16 keystrokes is 108
Maximum Number of A's with 17 keystrokes is 144
Maximum Number of A's with 18 keystrokes is 192
Maximum Number of A's with 19 keystrokes is 256
Maximum Number of A's with 20 keystrokes is 324
Time Complexity: O(N)
Auxiliary Space: O(N)
Thanks to Sahil for providing the above approach to solve this problem.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...