2 Keys Keyboard Problem - GeeksforGeeks

09 Jul, 2021
Given a positive integer N and a string S initially it is “A”, the task is to minimize the number of operations required to form a string consisting of N numbers of A’s by performing one of the following operations in each step:
Copy all the characters present in the string S.
Append all the characters to the string S which are copied last time.
Examples:
Input: N = 3Output: 3Explanation:Below are the operations performed:Operation 1: Copy the initial string S once i.e., “A”.Operation 2: Appending the copied string i.e., “A”, to the string S modifies the string S to “AA”.Operation 3: Appending the copied string i.e., “A”, to the string S modifies the string S to “AAA”.Therefore, the minimum number of operations required to get 3 A’s is 3.
Input: N = 7Output: 7
Approach: The given problem can be solved based on the following observations: 
If N = P1*P2*Pm where {P1, P2, ..., Pm} are the prime numbers then one can perform the following moves:First, copy the string and then paste it (P1 – 1) times.Similarly, again copying the string and pasting it for (P2 – 1) times.Performing M times with the remaining prime number, one will get the string with N number of A’s.Therefore, the total number of minimum moves needed is given by (P1 + P2 + ... + Pm).
If N = P1*P2*Pm where {P1, P2, ..., Pm} are the prime numbers then one can perform the following moves:First, copy the string and then paste it (P1 – 1) times.Similarly, again copying the string and pasting it for (P2 – 1) times.Performing M times with the remaining prime number, one will get the string with N number of A’s.
First, copy the string and then paste it (P1 – 1) times.Similarly, again copying the string and pasting it for (P2 – 1) times.Performing M times with the remaining prime number, one will get the string with N number of A’s.
First, copy the string and then paste it (P1 – 1) times.
Similarly, again copying the string and pasting it for (P2 – 1) times.
Performing M times with the remaining prime number, one will get the string with N number of A’s.
Therefore, the total number of minimum moves needed is given by (P1 + P2 + ... + Pm).
Follow the steps below to solve the problem:
Initialize a variable, say ans as 0, that stores the resultant number of operations.
Find the prime factors and its power of the given integer N.
Now, iterate through all the prime factors of N and increment the value of ans by the product of the prime factor and its power.
Finally, after completing the above steps, print the value of ans as the resultant minimum number of moves.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the minimum number// of steps required to form N number// of A'sint minSteps(int N){    // Stores the count of steps needed    int ans = 0;     // Traverse over the range [2, N]    for (int d = 2; d * d <= N; d++) {         // Iterate while N is divisible        // by d        while (N % d == 0) {             // Increment the value of            // ans by d            ans += d;             // Divide N by d            N /= d;        }    }     // If N is not 1    if (N != 1) {        ans += N;    }     // Return the ans    return ans;} // Driver Codeint main(){    int N = 3;    cout << minSteps(N);     return 0;}
// Java Program for the above approachimport java.io.*; class GFG{       // Function to find the minimum number    // of steps required to form N number    // of A's    static int minSteps(int N)    {        // Stores the count of steps needed        int ans = 0;         // Traverse over the range [2, N]        for (int d = 2; d * d <= N; d++) {             // Iterate while N is divisible            // by d            while (N % d == 0) {                 // Increment the value of                // ans by d                ans += d;                 // Divide N by d                N /= d;            }        }         // If N is not 1        if (N != 1) {            ans += N;        }         // Return the ans        return ans;    }     // Driver Code    public static void main(String[] args)    {        int N = 3;        minSteps(N);        System.out.println(minSteps(N));    }} // This code is contributed by lokesh potta.
# Python3 program for the above approach# Function to find the minimum number# of steps required to form N number# of A'sdef minSteps( N):     # Stores the count of steps needed    ans = 0     # Traverse over the range [2, N]    d = 2    while(d * d <= N):         # Iterate while N is divisible        # by d        while (N % d == 0):             # Increment the value of            # ans by d            ans += d             # Divide N by d            N /= d        d += 1         # If N is not 1    if (N != 1):        ans += N         # Return the ans    return ans # Driver CodeN = 3print(minSteps(N)) # This code is contributed by shivanisinghss2110   
// C# program for the above approachusing System; class GFG{     // Function to find the minimum number// of steps required to form N number// of A'sstatic int minSteps(int N){    // Stores the count of steps needed    int ans = 0;     // Traverse over the range [2, N]    for (int d = 2; d * d <= N; d++) {         // Iterate while N is divisible        // by d        while (N % d == 0) {             // Increment the value of            // ans by d            ans += d;             // Divide N by d            N /= d;        }    }     // If N is not 1    if (N != 1) {        ans += N;    }     // Return the ans    return ans;} // Driver Codestatic public void Main (){         int N = 3;    Console.Write(minSteps(N));}} // This code is contributed by sanjoy_62.
<script> // JavaScript Program for the above approach// Function to find the minimum number    // of steps required to form N number    // of A's    function minSteps(N)    {        // Stores the count of steps needed        var ans = 0;         // Traverse over the range [2, N]        for (var d = 2; d * d <= N; d++) {             // Iterate while N is divisible            // by d            while (N % d == 0) {                 // Increment the value of                // ans by d                ans += d;                 // Divide N by d                N /= d;            }        }         // If N is not 1        if (N != 1) {            ans += N;        }         // Return the ans        return ans;    }     // Driver Code        var N = 3;        minSteps(N);        document.write(minSteps(N));   // This code is contributed by shivanisinghss2110 </script>
3
Time Complexity: O(√N)Auxiliary Space: O(1)
sanjoy_62
lokeshpotta20
shivanisinghss2110
Prime Number
prime-factor
Combinatorial
Mathematical
Mathematical
Prime Number
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Largest number by rearranging digits of a given positive or negative number
Combinations with repetitions
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Given number of matches played, find number of teams in tournament
Generate all possible combinations of at most X characters from a given array
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Modulo Operator (%) in C/C++ with Examples