# 2 Keys Keyboard in C++

C++Server Side ProgrammingProgramming

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
• return ret

Let us see the following implementation to get better understanding −

## Example

Live Demo

#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
Updated on 04-May-2020 13:17:10