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)
Another Approach: using Bidirectional Search
- Initialize two queues, one for start and one for end. Push start and end to their respective queues.
- Initialize two ans[] arrays for both start and end. ans[i] stores the number of steps taken to reach i from start or end, depending on which queue it belongs to.
- Initialize a visited[] array which marks if the state has been visited or not. visited[i] stores true if the state i has been visited.
- Initialize a commonNode variable to -1, indicating no common node has been found yet.
- While both queues are not empty, do the following:
a. Choose the queue with the smaller size, and pop the front element. Let this element be x.
b. If x is already visited, continue to the next element in the queue.
c. For each element y in the array a[], calculate yx%mod. If this state is already visited by the other queue, then we have found a common node. Update commonNode to yx%mod and return ans[start] + ans[end].
d. If this state has not been visited before, add it to the queue, mark it as visited, and update ans[] for this state. - If no common node is found after visiting all possible states, return -1.
- Done
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 minimumMultiplications( int start, int end, int a[], int n) { // array which stores the minimum steps // to reach i from start or end int ans_start[100001], ans_end[100001]; // visited array to keep track of visited states bool visited_start[100001], visited_end[100001]; // -1 indicated the state has not been visited memset (ans_start, -1, sizeof (ans_start)); memset (ans_end, -1, sizeof (ans_end)); memset (visited_start, false , sizeof (visited_start)); memset (visited_end, false , sizeof (visited_end)); int mod = 100000; // queues to store all possible states queue< int > q_start, q_end; // initially push start and end q_start.push(start % mod); q_end.push(end % mod); // to reach start or end we require 0 steps ans_start[start] = 0; ans_end[end] = 0; // mark start and end as visited visited_start[start] = true ; visited_end[end] = true ; // variable to store common node int commonNode = -1; // till all states are visited or common node is found while (!q_start.empty() && !q_end.empty()) { // choose the queue with smaller size if (q_start.size() < q_end.size()) { int top = q_start.front(); q_start.pop(); // if the topmost element is end if (visited_end[top]) { commonNode = top; break ; } // 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 (!visited_start[pushed]) { ans_start[pushed] = ans_start[top] + 1; q_start.push(pushed); visited_start[pushed] = true ; } } } else { int top = q_end.front(); q_end.pop(); // if the topmost element is start if (visited_start[top]) { commonNode = top; break ; } // 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 (!visited_end[pushed]) { ans_end[pushed] = ans_end[top] + 1; q_end.push(pushed); visited_end[pushed] = true ; } } } } if (commonNode == -1) return -1; // return the sum of minimum steps from start and end to // common node return ans_start[commonNode] + ans_end[commonNode]; } // Driver Code int main() { int start = 7, end = 66175; int a[] = { 3, 4, 65 }; int n = sizeof (a) / sizeof (a[0]); // Calling function cout << minimumMultiplications(start, end, a, n); return 0; } |
Java
import java.util.*; public class Main { // Function that returns the minimum operations static int minimumMultiplications( int start, int end, int a[], int n) { // array which stores the minimum steps // to reach i from start or end int ans_start[] = new int [ 100001 ]; int ans_end[] = new int [ 100001 ]; Arrays.fill(ans_start, - 1 ); Arrays.fill(ans_end, - 1 ); // visited array to keep track of visited states boolean visited_start[] = new boolean [ 100001 ]; boolean visited_end[] = new boolean [ 100001 ]; int mod = 100000 ; // queues to store all possible states Queue<Integer> q_start = new LinkedList<>(); Queue<Integer> q_end = new LinkedList<>(); // initially push start and end q_start.add(start % mod); q_end.add(end % mod); // to reach start or end we require 0 steps ans_start[start] = 0 ; ans_end[end] = 0 ; // mark start and end as visited visited_start[start] = true ; visited_end[end] = true ; // variable to store common node int commonNode = - 1 ; // till all states are visited or common node is found while (!q_start.isEmpty() && !q_end.isEmpty()) { // choose the queue with smaller size if (q_start.size() < q_end.size()) { int top = q_start.poll(); // if the topmost element is end if (visited_end[top]) { commonNode = top; break ; } // 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 (!visited_start[pushed]) { ans_start[pushed] = ans_start[top] + 1 ; q_start.add(pushed); visited_start[pushed] = true ; } } } else { int top = q_end.poll(); // if the topmost element is start if (visited_start[top]) { commonNode = top; break ; } // 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 (!visited_end[pushed]) { ans_end[pushed] = ans_end[top] + 1 ; q_end.add(pushed); visited_end[pushed] = true ; } } } } if (commonNode == - 1 ) return - 1 ; // return the sum of minimum steps from start and end to common node return ans_start[commonNode] + ans_end[commonNode]; } // 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(minimumMultiplications(start, end, a, n)); } } |
Python3
from queue import Queue # Function that returns the minimum operations def minimumMultiplications(start, end, a, n): # array which stores the minimum steps # to reach i from start or end ans_start, ans_end = [ - 1 ] * 100001 , [ - 1 ] * 100001 # visited array to keep track of visited states visited_start, visited_end = [ False ] * 100001 , [ False ] * 100001 mod = 100000 # queues to store all possible states q_start, q_end = Queue(), Queue() # initially push start and end q_start.put(start % mod) q_end.put(end % mod) # to reach start or end we require 0 steps ans_start[start] = 0 ans_end[end] = 0 # mark start and end as visited visited_start[start] = True visited_end[end] = True # variable to store common node commonNode = - 1 # till all states are visited or common node is found while not q_start.empty() and not q_end.empty(): # choose the queue with smaller size if q_start.qsize() < q_end.qsize(): top = q_start.get() # if the topmost element is end if visited_end[top]: commonNode = top break # 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 not visited_start[pushed]: ans_start[pushed] = ans_start[top] + 1 q_start.put(pushed) visited_start[pushed] = True else : top = q_end.get() # if the topmost element is start if visited_start[top]: commonNode = top break # 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 not visited_end[pushed]: ans_end[pushed] = ans_end[top] + 1 q_end.put(pushed) visited_end[pushed] = True if commonNode = = - 1 : return - 1 # return the sum of minimum steps from start and end to common node return ans_start[commonNode] + ans_end[commonNode] # Driver Code if __name__ = = "__main__" : start, end = 7 , 66175 a = [ 3 , 4 , 65 ] n = len (a) # Calling function print (minimumMultiplications(start, end, a, n)) |
4
Time Complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...