Minimum Cost to Convert 1 to N by Multiplying X or Right Rotation of Digits


We can use the following technique to find the cheapest way to multiply X or right−rotate its digits from 1 to N. To monitor the initial lowest cost, create a cost variable. Check to see if N is evenly divided by X at each stage as you progress from N to 1. If so, divide N by X to update it and carry on with the process. Rotate N's digits to the right to increase its value if it is not divisible by X. Increase the cost variable in this situation. The ultimate cost variable value will be the least amount necessary to change 1 into N. This algorithm effectively determines the fewest operations required to do the desired transformation using digit rotation or multiplication.

Methods Used

  • Naive Approach: Right Rotation of Digits

  • Efficient Approach: Multiplication by X

Naive Approach: Right Rotation of Digits

The naive approach involves starting with the number 1 and repeatedly rotating its digits to the right until reaching the target number N. In each rotation, the last digit becomes the first digit. Although conceptually simple, this strategy can be inefficient for larger values of N as it may require numerous steps to reach the target number. As N increases, the number of rotations also increases rapidly, making it a less effective method for determining the least cost to convert 1 to N. Due to its inefficiency, this approach is not recommended for large N values, and alternative methods, such as dividing N by X, prove to be more efficient in finding the lowest cost for the conversion.

Algorithm

  • Create the variable "cost" to track the steps required to get to N, initialising it to 1 to represent the current value.

  • Follow these instructions several times until the current number equals N:

    Rotate the current number's digits to the right, making the last digit the first digit.

    Increase the "cost" variable by 1 to keep track of the necessary number of rotations.

  • The "cost" variable will store the minimal steps required to rotate the original integer (1) using right rotations to reach N once the current number equals N.

Example

#include <iostream>
#include <cmath>

int rotateDigits(int num, int numDigits) {
    return (num / 10) + (num % 10) * std::pow(10, numDigits - 1);
}

int main() {
    int N = 123; // Replace this with your desired N value

    int current = 1;
    int cost = 0;
    bool found = false;

    while (current != N) {
        int numDigits = std::to_string(current).length();
        current = rotateDigits(current, numDigits);
        cost++;

        if (cost > N) {
            std::cout << "N cannot be reached from 1 using right rotations." << std::endl;
            found = true;
            break;
        }
    }

    if (!found) {
        std::cout << "Minimum steps to reach N: " << cost << std::endl;
    }
    return 0;
}

Output

N cannot be reached from 1 using right rotations.

Efficient Approach: Multiplication by X

The best method for minimising the cost of multiplying 1 by N is to divide N by X periodically until the result is 1. A cost variable is initialised to monitor the lowest cost in order to accomplish this. We determine if N is divisible by X by starting with the value of N. If N and X are both divisible, the cost is increased and the division is done. You keep doing this until N equals 1. This methodology is more efficient than "Right Rotation of Digits" since it involves less steps in total to arrive at a result of 1. It is the favoured method for figuring out the lowest conversion cost feasible due to its quicker and more effective nature.

Algorithm

  • To keep track of the lowest cost, initialise the variable "cost" to 0.

  • Start with a fixed multiplier X and the supplied target number N.

  • For as long as N exceeds 1, repeat steps 4 through 6.

  • Assuming N% X == 0, determine whether N is divisible by X.

  • Divide N by X if it is divisible (N = N / X), and then add one to the "cost" variable.

  • If not divisible, rotate N's digits to the right (by shifting the last digit to the first) and increase "cost" by one.

  • Up until N becomes 1, repeat steps 3 through 6.

  • The last "cost" denotes the bare minimum required to multiply by X or turn the digits right to change 1 to N.

Example

#include <iostream>
#include <cmath>

int main() {
    int X = 3;
    int N = 100;
    int cost = 0;

    while (N > 1) {
        if (N % X == 0) {
            N /= X;
            cost++;
        } else {
            int lastDigit = N % 10;
            N = (N / 10) + (lastDigit * std::pow(10, std::floor(std::log10(N))));
            cost++;
        }
    }

    std::cout << "Final cost: " << cost << std::endl;

    return 0;
}

Output

Final cost: 2

Conclusion

In conclusion, the Efficient Approach of Multiplication by X surpasses the Naive Approach of Right Rotation of Digits when determining the least cost to convert 1 to N by either multiplying X or right−rotating digits. The more simplified approach provided by the Efficient Approach requires fewer steps to arrive at the desired number N. The Naive Approach, on the other hand, might be ineffective and time−consuming, particularly for higher values of N. We can reduce the amount of processes needed and determine the most economical approach to convert 1 into N by using the Efficient Approach. This strategy solves the issue of determining the least cost for this conversion process and is shown to be a more useful and effective algorithm.

Updated on: 02-Aug-2023

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements