Minimum multiplications to reach end

Graphs Shortest Path Algorithms Hard

Given start, end, and an array arr of n numbers. At each step, the start is multiplied by any number in the array and then a mod operation with 100000 is done to get the new start.


Find the minimum steps in which the end can be achieved starting from the start. If it is not possible to reach the end, then return -1.

Examples:

Input: arr = [2, 5, 7], start = 3, end = 30

Output: 2

Explanation: 

Step 1: 3*2 = 6 % 100000 = 6 

Step 2: 6*5 = 30 % 100000 = 30

Therefore, in minimum 2 multiplications, we reach the end number which is treated as a destination node of a graph here.

Input: arr = [3, 4, 65], start = 7, end = 66175

Output: 4

Explanation: 

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

Therefore, in minimum 4 multiplications we reach the end number which is treated as a destination node of a graph here.

Input: arr = [3, 4, 65], start = 7, end = 21

Constraints

  •   1 <= n <= 104
  •   1 <= arr[i] <= 104
  •   1 <= start, end < 105

Hints

  • Use a queue where each entry contains (current_value, steps_taken). Maintain a visited set (or boolean array of size 100000) to avoid cycles.
  • Process each number, generate new numbers (current * num) % 100000 for each num in arr, and enqueue them if they haven't been visited. If end is reached, return the number of steps. If the queue is exhausted, return -1.

Company Tags

Rakuten eBay HCL Technologies Etsy Ernst & Young American Express Philips Healthcare McKinsey & Company Cerner GE Healthcare Deloitte Oracle Target Johnson & Johnson Unity Technologies HashiCorp OYO Rooms Lyft Texas Instruments AMD Cloudflare Stripe Pinterest Walmart Activision Blizzard Google Microsoft Amazon Meta Apple Netflix Adobe