Minimum steps to reach end from start by performing multiplication and mod operations with array elements

Given start, end and an array of N numbers. At each step, start is multiplied with any number in the array and then mod operation with 100000 is done to get the new start. The task is to find the minimum steps in which end can be achieved starting from start.

Examples:

Input: start = 3 end = 30 a[] = {2, 5, 7}
Output: 2
Step 1: 3*2 = 6 % 100000 = 6
Step 2: 6*5 = 30 % 100000 = 30

Input: start = 7 end = 66175 a[] = {3, 4, 65}
Output: 4
Step 1: 7*3 = 21 % 100000 = 21
Step 2: 21*3 = 6 % 100000 = 63
Step 3: 63*65 = 4095 % 100000 = 4095
Step 4: 4095*65 = 266175 % 100000 = 66175



Approach: Since in the above problem the modulus given is 100000, therefore the maximum number of states will be 105. All the states can be checked using simple BFS. Initialize an ans[] array with -1 which marks that the state has not been visited. ans[i] stores the number of steps taken to reach i from start. Initially push the start to the queue, then apply BFS. Pop the top element and check if it is equal to the end, if it is then print the ans[end]. If the element is not equal to the topmost element, then multiply top element with every element in the array and perform a mod operation. If the multiplied element state has not been visited previously, then push it into the queue. Initialize ans[pushed_element] by ans[top_element] + 1. Once all the states are visited, and the state cannot be reached by performing every possible multiplication, then print -1.

Below is the implementation of the above approach:

// C++ program to find the minimum steps
// to reach end from start by performing
// multiplications and mod operations with array elements
#include <bits/stdc++.h>
using namespace std;

// Function that returns the minimum operations
int minimumMulitplications(int start, int end, int a[], int n)
{
    // array which stores the minimum steps
    // to reach i from start
    int ans[100001];

    // -1 indicated the state has not been visited
    memset(ans, -1, sizeof(ans));
    int mod = 100000;

    // queue to store all possible states
    queue<int> q;

    // initially push the start
    q.push(start % mod);

    // to reach start we require 0 steps
    ans[start] = 0;

    // till all states are visited
    while (!q.empty()) {

        // get the topmost element in the queue
        int top = q.front();

        // pop the topmost element
        q.pop();

        // if the topmost element is end
        if (top == end)
            return ans[end];

        // perform multiplication with all array elements
        for (int i = 0; i < n; i++) {
            int pushed = top * a[i];
            pushed = pushed % mod;

            // if not visited, then push it to queue
            if (ans[pushed] == -1) {
                ans[pushed] = ans[top] + 1;
                q.push(pushed);
            }
        }
    }
    return -1;
}

// Driver Code
int main()
{
    int start = 7, end = 66175;
    int a[] = { 3, 4, 65 };
    int n = sizeof(a) / sizeof(a[0]);

    // Calling function
    cout << minimumMulitplications(start, end, a, n);
    return 0;
}
Output:

4


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Recommended Posts:



3.3 Average Difficulty : 3.3/5.0
Based on 3 vote(s)






User Actions