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++
// 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; } |
Java
// Java program to find the minimum steps // to reach end from start by performing // multiplications and mod operations with array elements import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; class GFG { // Function that returns the minimum operations static int minimumMulitplications( int start, int end, int a[], int n) { // array which stores the minimum steps // to reach i from start int ans[] = new int [ 100001 ]; // -1 indicated the state has not been visited Arrays.fill(ans, - 1 ); int mod = 100000 ; // queue to store all possible states Queue<Integer> q = new LinkedList<>(); // initially push the start q.add(start % mod); // to reach start we require 0 steps ans[start] = 0 ; // till all states are visited while (!q.isEmpty()) { // get the topmost element in the queue int top = q.peek(); // pop the topmost element q.remove(); // 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.add(pushed); } } } return - 1 ; } // Driver Code public static void main(String args[]) { int start = 7 , end = 66175 ; int a[] = { 3 , 4 , 65 }; int n = a.length; // Calling function System.out.println(minimumMulitplications(start, end, a, n)); } } // This code is contributed by PrinciRaj19992 |
Python3
# Python3 program to find the minimum steps # to reach end from start by performing # multiplications and mod operations with # array elements from collections import deque # Function that returns the minimum operations def minimumMulitplications(start, end, a, n): # array which stores the minimum # steps to reach i from start ans = [ - 1 for i in range ( 100001 )] # -1 indicated the state has # not been visited mod = 100000 q = deque() # queue to store all possible states # initially push the start q.append(start % mod) # to reach start we require 0 steps ans[start] = 0 # till all states are visited while ( len (q) > 0 ): # get the topmost element in the # queue, pop the topmost element top = q.popleft() # if the topmost element is end if (top = = end): return ans[end] # perform multiplication with # all array elements for i in range (n): 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.append(pushed) return - 1 # Driver Code start = 7 end = 66175 a = [ 3 , 4 , 65 ] n = len (a) # Calling function print (minimumMulitplications(start, end, a, n)) # This code is contributed by mohit kumar |
C#
// C# program to find the minimum steps // to reach end from start by performing // multiplications and mod operations with array elements using System; using System.Collections.Generic; class GFG { // Function that returns the minimum operations static int minimumMulitplications( int start, int end, int []a, int n) { // array which stores the minimum steps // to reach i from start int []ans = new int [100001]; // -1 indicated the state has not been visited for ( int i = 0; i < ans.Length; i++) ans[i] = -1; int mod = 100000; // queue to store all possible states Queue< int > q = new Queue< int >(); // initially push the start q.Enqueue(start % mod); // to reach start we require 0 steps ans[start] = 0; // till all states are visited while (q.Count != 0) { // get the topmost element in the queue int top = q.Peek(); // pop the topmost element q.Dequeue(); // 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.Enqueue(pushed); } } } return -1; } // Driver Code public static void Main(String []args) { int start = 7, end = 66175; int []a = {3, 4, 65}; int n = a.Length; // Calling function Console.WriteLine(minimumMulitplications(start, end, a, n)); } } /* This code contributed by PrinciRaj1992 */ |
Javascript
<script> // Javascript program to find the minimum steps // to reach end from start by performing // multiplications and mod operations with array elements // Function that returns the minimum operations function minimumMulitplications(start,end,a,n) { // array which stores the minimum steps // to reach i from start let ans = new Array(100001); // -1 indicated the state has not been visited for (let i=0;i<ans.length;i++) { ans[i]=-1; } let mod = 100000; // queue to store all possible states let 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.length!=0) { // get the topmost element in the queue let top = q[0]; // pop the topmost element q.shift(); // if the topmost element is end if (top == end) { return ans[end]; } // perform multiplication with all array elements for (let i = 0; i < n; i++) { let 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 let start = 7, end = 66175; let a=[3, 4, 65]; let n = a.length; // Calling function document.write(minimumMulitplications(start, end, a, n)); // This code is contributed by unknown2108 </script> |
4
Time Complexity: O(n)
Auxiliary Space: O(n)