Four Keys Keyboard Problem Statement

Imagine you have a special keyboard with four keys:

  • Key 1: (A) to print one ‘A’ on screen.
  • Key 2: (Ctrl-A) to select the entire screen.
  • Key 3: (Ctrl-C) to copy the selection to the buffer.
  • Key 4: (Ctrl-V) to paste the buffer's content, appending it after the current content.

Given a positive integer ‘N’, determine the maximum number of 'A's that can be printed on the screen by pressing the keys on this special keyboard ‘N’ times.

Input:

The first line of input contains an integer ‘T’ denoting the number of test cases.
The following ‘T’ lines each contain a positive integer ‘N’, representing the number of times the keys can be pressed on the keyboard.

Output:

For each test case, output a line with the maximum number of ‘A’s that can be printed on the screen.

Example:

Input:
2
7
3
Output:
9
3
Explanation:

In the first test case with N = 7, the optimal sequence is: A, A, A, Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-V. This results in 9 'A's on the screen.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 150
Follow-up:
Try solving the problem in O(N) complexity.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Be the first one to answer
Add answer anonymously...
Visa Fullstack Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter