
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
How to print maximum number of A’s using given four keys
Let us consider, we will try to write the letter ‘A’, using the keyboard. Our goal is to use only four keys and try to write maximum ‘A’s on the text field. The keys are ‘A’, ‘C’, ‘V’ and ‘Ctrl’.
To write the maximum number of A, we will use Ctrl + A to select All, Ctrl + C to copy and Ctrl + V to paste.
Input and Output
Input: Number of keystrokes, say 7 Output: Maximum Number of A's with 7 keystrokes is: 9 Press A three times. Then Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+V
Algorithm
keyNumbers(keyStrokes)
Input: number of keystrokes.
Output: Maximum number of letters using these keystrokes.
Begin if keyStrokes <= 6, then return keyStrokes for n := 1 to 6, do result[n-1] := n done for n := 7 to keyStrokes, do result[n-1] := 0 for breakpoint := n-3 down to 1, do curr := (n – breakpoint - 1)*result[breakpoint - 1] if curr > result[n-1], then result[n - 1] := curr done done result[keyStrokes - 1] End
Example
#include<iostream> using namespace std; int keyNumbers(int keystrokes) { //find number of 'A's using 4 types of keys if (keystrokes <= 6) //if keystrokes are less than 7 return keystrokes; int result[keystrokes]; //store intermediate results for (int n=1; n<=6; n++) //upto 6 keystrokes, we need that number of keystrokes for max result[n-1] = n; for (int n=7; n<=keystrokes; n++) { //for 7th to higher result[n-1] = 0; //initially store 0 as result for (int breakPoint=n-3; breakPoint>=1; breakPoint--) { //find breakpoint to select, copy and paste int curr = (n-breakPoint-1)*result[breakPoint-1]; if (curr > result[n-1]) result[n-1] = curr; } } return result[keystrokes-1]; } int main() { int keystrokes; cout << "Enter Number of keystrokes: "; cin >> keystrokes; cout << "Maximum Number of A's with "<<keystrokes << " keystrokes is: "<< keyNumbers(keystrokes)<<endl; }
Output
Enter Number of keystrokes: 7 Maximum Number of A's with 7 keystrokes is: 9
- Related Articles
- How to print number of islands in a given matrix using C#?
- Print BST keys in the given range in C++
- How to print all the keys of a dictionary in Python
- Java program to print the fibonacci series of a given number using while loop
- Print BST keys in given Range - O(1) Space in C++
- Find maximum number that can be formed using digits of a given number in C++
- Python Program to print a specific number of rows with Maximum Sum
- Java program to print the reverse of the given number
- Java program to print the factorial of the given number
- Java program to print Fibonacci series of a given number.
- How to count number of keys in a MongoDB document?
- How to print the days in a given month using Python?
- How to print array elements within a given range using Numpy?
- How to Print Lines Containing Given String in File using Python?
- Golang Program to Print the Multiplication Table of a Given Number

Advertisements