
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
2 Keys Keyboard in C++
Suppose we have only one character 'A' in a text editor. We can perform two operations on this letter for each step −
- Copy All − We can copy all the characters present on the notepad
- Paste − We can paste the characters which are copied last time.
Now suppose we have a number n. We have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. We have to find the result in the minimum number of steps to get n 'A'. So if the given n is 3, then answer will be 3, so initially there is only one “A”, Now copy this and paste this, so there will be “AA” now. Now we can paste again, so one ‘A’ will be placed. Thus we will get “AAA”.
To solve this, we will follow these steps −
- ret := 0
- for k in range 2 to n
- while n mod k is not 0
- ret := ret + k and n := n / k
- while n mod k is not 0
- return ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int minSteps(int n) { int ret = 0; for(int k = 2; k <= n; k++){ for(; n % k == 0; ret += k, n /= k); } return ret; } }; main(){ Solution ob; cout << (ob.minSteps(10)); }
Input
10
Output
7
Advertisements