# 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 10^{5}. 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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

**Output:**

4

## Recommended Posts:

- Maximum sum of all elements of array after performing given operations
- Number of array elements derivable from D after performing certain operations
- Minimum steps to reach end of array under constraints
- Minimum steps to make all the elements of the array divisible by 4
- Minimum steps to reach a destination
- Minimum steps to reach target by a Knight | Set 1
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Maximum Possible Product in Array after performing given Operations
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum operations required to make all the elements distinct in an array
- Maximum count of equal numbers in an array after performing given operations
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Find the minimum number of operations required to make all array elements equal
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Minimum operations to make counts of remainders same in an array

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.