How to print maximum number of A 's using given four keys | A recursive function that returns the optimal length string for N keystrokes ; The optimal string length is N when N is smaller than 7 ; Initialize result ; TRY ALL POSSIBLE BREAK - POINTS For any keystroke N , we need to loop from N - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have Ctrl - A , Ctrl - C and then only Ctrl - V all the way . ; If the breakpoint is s at b 'th keystroke then  the optimal string would have length  (n-b-1)*screen[b-1]; ; Driver code ; for the rest of the array we will rely on the previous entries to compute new ones
< ? 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 ) . " STRNEWLINE " ) ; ? >