{"id":389,"date":"2025-06-26T22:12:02","date_gmt":"2025-06-26T16:42:02","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=389"},"modified":"2025-06-26T22:12:03","modified_gmt":"2025-06-26T16:42:03","slug":"minimum-multiplications-to-reach-end-python-bfs","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/","title":{"rendered":"Minimum Multiplications to reach End | BFS Solution in Python"},"content":{"rendered":"\n<p>Solve <em>Minimum Multiplications to reach End<\/em> with a clean BFS, modular math, and commented Python code. Easy steps, dry run, and Big-O explained.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/minimum-multiplications-to-reach-end\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-015083d1-057c-40de-af87-641ef6297a88\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#0-1-what-does-the-problem-ask\" style=\"\">1. What does the problem ask?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#1-2-example\" style=\"\">2. Example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#2-3-intuition-amp-approach\" style=\"\">3. Intuition &amp; approach<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#3-31-why-breadth-first-search\" style=\"\">3.1 Why Breadth-First Search?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#4-32-graph-model\" style=\"\">3.2 Graph model<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#5-33-distance-array\" style=\"\">3.3 Distance array<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#6-4-python-code\" style=\"\">4. Python code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#7-5-step-by-step-explanation\" style=\"\">5. Step-by-step explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#8-6-dry-run\" style=\"\">6. Dry run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#9-7-complexity\" style=\"\">7. Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-multiplications-to-reach-end-python-bfs\/#10-8-conclusion\" style=\"\">8. Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1-what-does-the-problem-ask\">1. What does the problem ask?<\/h2>\n\n\n\n<p>You have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>an <strong>array <code>arr<\/code><\/strong> of positive integers (the \u201cmultipliers\u201d),<\/li>\n\n\n\n<li>a <strong>start number<\/strong> and a <strong>target <code>end<\/code><\/strong>,<\/li>\n\n\n\n<li>a fixed <strong>mod value = 100000<\/strong> (so every multiplication is taken <code>mod 100000<\/code>).<\/li>\n<\/ul>\n\n\n\n<p>At each move you may <strong>pick any multiplier <code>m<\/code> in <code>arr<\/code><\/strong> and change your current<br>number <code>x<\/code> to<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"(x \u00d7 m) mod 100000\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">(x \u00d7 m) mod <\/span><span style=\"color: #B5CEA8\">100000<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Return the <strong>minimum number of moves<\/strong> needed to reach <code>end<\/code> from <code>start<\/code>.<br>If it is impossible, return <code>-1<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-2-example\">2. Example<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>arr<\/code><\/th><th><code>start<\/code><\/th><th><code>end<\/code><\/th><th>Answer<\/th><th>Why<\/th><\/tr><\/thead><tbody><tr><td><code>[2,5,7]<\/code><\/td><td><code>3<\/code><\/td><td><code>30<\/code><\/td><td><code>2<\/code><\/td><td>3\u2192(3\u00d75)=15, 15\u2192(15\u00d72)=30<\/td><\/tr><tr><td><code>[3]<\/code><\/td><td><code>7<\/code><\/td><td><code>7<\/code><\/td><td><code>0<\/code><\/td><td>Already at target<\/td><\/tr><tr><td><code>[2]<\/code><\/td><td><code>2<\/code><\/td><td><code>3<\/code><\/td><td><code>-1<\/code><\/td><td>Only even numbers appear, never 3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-3-intuition-amp-approach\">3. Intuition &amp; approach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-31-why-breadth-first-search\">3.1 Why Breadth-First Search?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each move costs <strong>exactly one step<\/strong>.<\/li>\n\n\n\n<li>We want the <strong>fewest steps<\/strong>, so BFS\u2014which explores level by level\u2014naturally gives the shortest path in an unweighted graph.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-32-graph-model\">3.2 Graph model<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vertices<\/strong>: all numbers <code>0 \u2026 99999<\/code>.<\/li>\n\n\n\n<li><strong>Edge<\/strong>: from <code>x<\/code> to <code>(x \u00d7 m) mod 100000<\/code> for every <code>m<\/code> in <code>arr<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Our job is the <strong>shortest path<\/strong> from the vertex <code>start<\/code> to the vertex <code>end<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-33-distance-array\">3.3 Distance array<\/h3>\n\n\n\n<p>A 1-D list <code>distance[num]<\/code> stores the smallest steps discovered so far.<br>Start with <code>\u221e<\/code>, except <code>distance[start] = 0<\/code>.<br>We push <code>(steps, num)<\/code> into a queue and update neighbours only when<br>we find a better (smaller) step count.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-4-python-code\">4. Python code<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\nimport sys\nfrom collections import deque\n\n\nclass Solution:\n    def minimumMultiplications(self, arr: List[int], start: int, end: int) -&gt; int:\n        MOD = 100000                        # fixed modulus\n        distance = [sys.maxsize] * MOD\n        distance[start] = 0                 # 0 moves to reach start\n\n        queue = deque()\n        queue.append([0, start])            # [steps_so_far, current_num]\n\n        while queue:\n            step, num = queue.popleft()\n\n            if num == end:                  # reached goal\n                return step\n\n            # try every multiplier\n            for m in arr:\n                new_num  = (num * m) % MOD\n                new_step = step + 1\n\n                # relax only if shorter path\n                if new_step &lt; distance[new_num]:\n                    distance[new_num] = new_step\n                    queue.append([new_step, new_num])\n\n        return -1                           # unreachable\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> typing <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> List<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> sys<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> collections <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> deque<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">minimumMultiplications<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">start<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        MOD = <\/span><span style=\"color: #B5CEA8\">100000<\/span><span style=\"color: #D4D4D4\">                        <\/span><span style=\"color: #6A9955\"># fixed modulus<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        distance = [sys.maxsize] * MOD<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        distance[start] = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">                 <\/span><span style=\"color: #6A9955\"># 0 moves to reach start<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        queue = deque()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        queue.append([<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, start])            <\/span><span style=\"color: #6A9955\"># [steps_so_far, current_num]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> queue:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            step, num = queue.popleft()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num == end:                  <\/span><span style=\"color: #6A9955\"># reached goal<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> step<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># try every multiplier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> m <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> arr:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                new_num  = (num * m) % MOD<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                new_step = step + <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># relax only if shorter path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> new_step &lt; distance[new_num]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    distance[new_num] = new_step<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    queue.append([new_step, new_num])<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">                           <\/span><span style=\"color: #6A9955\"># unreachable<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-5-step-by-step-explanation\">5. Step-by-step explanation<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Distance table<\/strong> \u2013 size = 100 000, keeps smallest steps to each residue.<\/li>\n\n\n\n<li><strong>Queue<\/strong> \u2013 classic BFS; stores pairs of (steps, number).<\/li>\n\n\n\n<li><strong>Loop<\/strong>\n<ul class=\"wp-block-list\">\n<li>Pop the earliest state.<\/li>\n\n\n\n<li>If number equals <code>end<\/code>, we are done (BFS guarantee).<\/li>\n\n\n\n<li>Otherwise, multiply it by every <code>m<\/code> in <code>arr<\/code>, take <code>mod 100000<\/code>, and push the neighbour if we just found a quicker route.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>End<\/strong> \u2013 if the queue empties without seeing <code>end<\/code>, no sequence of allowed multiplications can reach it \u2192 return <code>-1<\/code>.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-6-dry-run\">6. Dry run<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"arr = [2, 5], start = 3, end = 30\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">arr = [<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">], start = <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, end = <\/span><span style=\"color: #B5CEA8\">30<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Queue pop <code>(steps,num)<\/code><\/th><th>Generated <code>(new_num)<\/code><\/th><th><code>distance<\/code> updates<\/th><\/tr><\/thead><tbody><tr><td>(0, 3)<\/td><td>6, 15<\/td><td>dist[6]=1, dist[15]=1<\/td><\/tr><tr><td>(1, 6)<\/td><td>12, 30<\/td><td>dist[12]=2, <strong>dist[30]=2<\/strong> \u2192 goal \u2192 return 2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Shortest sequence: <code>3 \u2192 15 \u2192 30<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-7-complexity\">7. Complexity<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Metric<\/th><th>Value<\/th><th>Reason<\/th><\/tr><\/thead><tbody><tr><td><strong>Time<\/strong><\/td><td>`O(MOD \u00d7<\/td><td>arr<\/td><\/tr><tr><td><strong>Space<\/strong><\/td><td><code>O(MOD)<\/code><\/td><td><code>distance<\/code> array + queue (at most MOD entries).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>With the modulus fixed at 100 000, this is perfectly fine for interview-size inputs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-8-conclusion\">8. Conclusion<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The task \u201c<strong>Minimum Multiplications to reach End<\/strong>\u201d is a textbook case for <strong>BFS<\/strong> in a modular state space.<\/li>\n\n\n\n<li>Store steps in a <code>distance<\/code> array, multiply &amp; mod to produce neighbours, and stop as soon as you pop the target.<\/li>\n\n\n\n<li>The solution runs fast (linear in the state space) and uses only simple data structures\u2014ideal for coding interviews or contests.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solve Minimum Multiplications to reach End with a clean BFS, modular math, and commented Python code. Easy steps, dry run, and Big-O explained. Here&#8217;s the [Problem Link] to begin with. 1. What does the problem ask? You have: At each move you may pick any multiplier m in arr and change your currentnumber x to<\/p>\n","protected":false},"author":1,"featured_media":391,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[21,17,18],"class_list":{"0":"post-389","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-bfs","10":"tag-graph","11":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/minimum-multiplications-to-reach-end-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":392,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions\/392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/391"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}