<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MD ARIFUL HAQUE</title>
    <description>The latest articles on DEV Community by MD ARIFUL HAQUE (@mdarifulhaque).</description>
    <link>https://dev.to/mdarifulhaque</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F944478%2F148ec474-230c-4c52-8b8e-5aba51bc6d2e.jpeg</url>
      <title>DEV Community: MD ARIFUL HAQUE</title>
      <link>https://dev.to/mdarifulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdarifulhaque"/>
    <language>en</language>
    <item>
      <title>1306. Jump Game III</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 17 May 2026 15:37:06 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1306-jump-game-iii-8ai</link>
      <guid>https://dev.to/mdarifulhaque/1306-jump-game-iii-8ai</guid>
      <description>&lt;p&gt;1306. Jump Game III&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Depth-First Search&lt;/code&gt;, &lt;code&gt;Breadth-First Search&lt;/code&gt;, &lt;code&gt;Weekly Contest 169&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given an array of non-negative integers &lt;code&gt;arr&lt;/code&gt;, you are initially positioned at &lt;code&gt;start&lt;/code&gt; index of the array. When you are at index &lt;code&gt;i&lt;/code&gt;, you can jump to &lt;code&gt;i + arr[i]&lt;/code&gt; or &lt;code&gt;i - arr[i]&lt;/code&gt;, check if you can reach &lt;strong&gt;any&lt;/strong&gt; index with value 0.&lt;/p&gt;

&lt;p&gt;Notice that you can not jump outside of the array at any time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [4,2,3,0,3,1,2], start = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;All possible ways to reach at index 3 with value 0 are:&lt;/li&gt;
&lt;li&gt;index 5 -&amp;gt; index 4 -&amp;gt; index 1 -&amp;gt; index 3&lt;/li&gt;
&lt;li&gt;index 5 -&amp;gt; index 6 -&amp;gt; index 4 -&amp;gt; index 1 -&amp;gt; index 3&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [4,2,3,0,3,1,2], start = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;One possible way to reach at index 3 with value 0 is:&lt;/li&gt;
&lt;li&gt;index 0 -&amp;gt; index 4 -&amp;gt; index 1 -&amp;gt; index 3&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [3,0,2,1,2], start = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There is no way to reach at index 1 with value 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= arr.length &amp;lt;= 5 * 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= arr[i] &amp;lt; arr.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= start &amp;lt; arr.length&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Think of BFS to solve the problem.&lt;/li&gt;
&lt;li&gt;When you reach a position with a value = 0 then return true.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solution solves &lt;strong&gt;Jump Game III&lt;/strong&gt; using a &lt;strong&gt;BFS (Breadth-First Search)&lt;/strong&gt; approach. Starting from the given &lt;code&gt;start&lt;/code&gt; index, it explores all reachable positions by jumping forward or backward by &lt;code&gt;arr[i]&lt;/code&gt; steps. It stops early and returns &lt;code&gt;true&lt;/code&gt; as soon as it finds an index whose value is &lt;code&gt;0&lt;/code&gt;. If BFS completes without finding a zero, it returns &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
The solution is efficient and avoids revisiting indices, making it suitable for arrays up to 50,000 elements.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;BFS&lt;/strong&gt; to systematically explore reachable indices level by level.&lt;/li&gt;
&lt;li&gt;Maintain a &lt;code&gt;visited&lt;/code&gt; array to prevent revisiting indices and avoid infinite loops.&lt;/li&gt;
&lt;li&gt;Start from the &lt;code&gt;start&lt;/code&gt; index and push it into a queue.&lt;/li&gt;
&lt;li&gt;While the queue is not empty:

&lt;ul&gt;
&lt;li&gt;Pop the current index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;arr[i] == 0&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compute &lt;code&gt;forward = i + arr[i]&lt;/code&gt; and &lt;code&gt;backward = i - arr[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;forward&lt;/code&gt; is within bounds and not visited, mark visited and enqueue it.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;backward&lt;/code&gt; is within bounds and not visited, mark visited and enqueue it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If BFS finishes, return &lt;code&gt;false&lt;/code&gt; (no zero reachable).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001306-jump-game-iii" rel="noopener noreferrer"&gt;1306. Jump Game III&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $arr
 * @param Integer $start
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;canReach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$start&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;canReach&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;canReach&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;canReach&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BFS&lt;/strong&gt; guarantees we explore all possible paths without recursion depth issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visited array&lt;/strong&gt; is crucial to prevent reprocessing indices, which could happen if there are cycles in jumps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early termination&lt;/strong&gt; happens as soon as a zero is found, optimizing performance.&lt;/li&gt;
&lt;li&gt;This method works because each jump is deterministic: from &lt;code&gt;i&lt;/code&gt; you can only go to &lt;code&gt;i ± arr[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;BFS is preferred over DFS here because it naturally finds the shortest path, though for this problem the first zero found is sufficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, Each index is visited at most once, and each visit does constant-time work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, Due to the visited array and queue (which in worst case can hold all indices).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>154. Find Minimum in Rotated Sorted Array II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 16 May 2026 13:23:36 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/154-find-minimum-in-rotated-sorted-array-ii-3nd3</link>
      <guid>https://dev.to/mdarifulhaque/154-find-minimum-in-rotated-sorted-array-ii-3nd3</guid>
      <description>&lt;p&gt;154. Find Minimum in Rotated Sorted Array II&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Suppose an array of length &lt;code&gt;n&lt;/code&gt; sorted in ascending order is &lt;strong&gt;rotated&lt;/strong&gt; between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; times. For example, the array &lt;code&gt;nums = [0,1,4,4,5,6,7]&lt;/code&gt; might become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[4,5,6,7,0,1,4]&lt;/code&gt; if it was rotated &lt;code&gt;4&lt;/code&gt; times.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0,1,4,4,5,6,7]&lt;/code&gt; if it was rotated &lt;code&gt;7&lt;/code&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that &lt;strong&gt;rotating&lt;/strong&gt; an array &lt;code&gt;[a[0], a[1], a[2], ..., a[n-1]]&lt;/code&gt; 1 time results in the array &lt;code&gt;[a[n-1], a[0], a[1], a[2], ..., a[n-2]]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given the sorted rotated array &lt;code&gt;nums&lt;/code&gt; that may contain &lt;strong&gt;duplicates&lt;/strong&gt;, return &lt;em&gt;the minimum element of this array&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You must decrease the overall operation steps as much as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [2,2,2,0,1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n == nums.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-5000 &amp;lt;= nums[i] &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums&lt;/code&gt; is sorted and rotated between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Follow up:&lt;/strong&gt; This problem is similar to &lt;a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/" rel="noopener noreferrer"&gt;Find Minimum in Rotated Sorted Array&lt;/a&gt;, but &lt;code&gt;nums&lt;/code&gt; may contain &lt;strong&gt;duplicates&lt;/strong&gt;. Would this affect the runtime complexity? How and why?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solution uses a modified &lt;strong&gt;binary search&lt;/strong&gt; to find the minimum element in a rotated sorted array that may contain duplicates.&lt;br&gt;&lt;br&gt;
Unlike the standard version (Leetcode 153), duplicate values can appear anywhere in the array, which can make it impossible to determine which half contains the minimum using a simple comparison of &lt;code&gt;nums[mid]&lt;/code&gt; and &lt;code&gt;nums[high]&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
The algorithm handles this by reducing the search space when a tie occurs between &lt;code&gt;nums[mid]&lt;/code&gt; and &lt;code&gt;nums[high]&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Binary Search with Duplicate Handling&lt;/strong&gt; Use two pointers &lt;code&gt;low&lt;/code&gt; and &lt;code&gt;high&lt;/code&gt; to define the current search range.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare &lt;code&gt;nums[mid]&lt;/code&gt; and &lt;code&gt;nums[high]&lt;/code&gt;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nums[mid] &amp;gt; nums[high]&lt;/code&gt;: Minimum is in the right half → move &lt;code&gt;low&lt;/code&gt; to &lt;code&gt;mid + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;nums[mid] &amp;lt; nums[high]&lt;/code&gt;: Minimum is in the left half → move &lt;code&gt;high&lt;/code&gt; to &lt;code&gt;mid&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;nums[mid] == nums[high]&lt;/code&gt;: Cannot decide, so reduce &lt;code&gt;high&lt;/code&gt; by 1 to remove a duplicate from consideration.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminate when &lt;code&gt;low == high&lt;/code&gt;&lt;/strong&gt; At that point, &lt;code&gt;nums[low]&lt;/code&gt; is the minimum.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000154-find-minimum-in-rotated-sorted-array-ii" rel="noopener noreferrer"&gt;154. Find Minimum in Rotated Sorted Array II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Binary search&lt;/strong&gt; is efficient for sorted rotated arrays, but duplicates can break the deterministic half-elimination.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;nums[mid] &amp;gt; nums[high]&lt;/code&gt;, the array is rotated such that the minimum lies strictly to the right of &lt;code&gt;mid&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;nums[mid] &amp;lt; nums[high]&lt;/code&gt;, the segment from &lt;code&gt;mid&lt;/code&gt; to &lt;code&gt;high&lt;/code&gt; is properly sorted, so the minimum must be at or before &lt;code&gt;mid&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;nums[mid] == nums[high]&lt;/code&gt;, we can't tell if the minimum is left or right — but since &lt;code&gt;nums[mid]&lt;/code&gt; equals &lt;code&gt;nums[high]&lt;/code&gt;, we can safely shrink the range by ignoring &lt;code&gt;high&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This approach still works in &lt;strong&gt;O(log n)&lt;/strong&gt; on average, but degrades to &lt;strong&gt;O(n)&lt;/strong&gt; in worst case (e.g., when all elements are equal).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Average: &lt;strong&gt;O(log n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Worst case (all elements equal): &lt;strong&gt;O(n)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — only a few variables used.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>153. Find Minimum in Rotated Sorted Array</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 15 May 2026 17:41:30 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/153-find-minimum-in-rotated-sorted-array-31i1</link>
      <guid>https://dev.to/mdarifulhaque/153-find-minimum-in-rotated-sorted-array-31i1</guid>
      <description>&lt;p&gt;153. Find Minimum in Rotated Sorted Array&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Suppose an array of length &lt;code&gt;n&lt;/code&gt; sorted in ascending order is &lt;strong&gt;rotated&lt;/strong&gt; between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; times. For example, the array &lt;code&gt;nums = [0,1,2,4,5,6,7]&lt;/code&gt; might become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[4,5,6,7,0,1,2]&lt;/code&gt; if it was rotated &lt;code&gt;4&lt;/code&gt; times.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0,1,2,4,5,6,7]&lt;/code&gt; if it was rotated &lt;code&gt;7&lt;/code&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that &lt;strong&gt;rotating&lt;/strong&gt; an array &lt;code&gt;[a[0], a[1], a[2], ..., a[n-1]]&lt;/code&gt; 1 time results in the array &lt;code&gt;[a[n-1], a[0], a[1], a[2], ..., a[n-2]]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given the sorted rotated array &lt;code&gt;nums&lt;/code&gt; of &lt;strong&gt;unique&lt;/strong&gt; elements, return &lt;em&gt;the minimum element of this array&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You must write an algorithm that runs in &lt;code&gt;O(log n) time&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [3,4,5,1,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The original array was [1,2,3,4,5] rotated 3 times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [4,5,6,7,0,1,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [11,13,15,17]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 11&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The original array was [11,13,15,17] and it was rotated 4 times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n == nums.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-5000 &amp;lt;= nums[i] &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;All the integers of &lt;code&gt;nums&lt;/code&gt; are &lt;strong&gt;unique&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums&lt;/code&gt; is sorted and rotated between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2].&lt;/li&gt;
&lt;li&gt;You can divide the search space into two and see which direction to go. Can you think of an algorithm which has O(logN) search complexity?&lt;/li&gt;
&lt;li&gt;All the elements to the left of inflection point &amp;gt; first element of the array.&lt;/li&gt;
&lt;li&gt;All the elements to the right of inflection point &amp;lt; first element of the array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solution finds the minimum element in a rotated sorted array of unique integers using &lt;strong&gt;binary search&lt;/strong&gt; in &lt;strong&gt;O(log n)&lt;/strong&gt; time.&lt;br&gt;&lt;br&gt;
It compares the middle element with the rightmost element to decide whether the minimum lies in the left or right half.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Binary search on rotated sorted array&lt;/strong&gt; Use &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; pointers, compute &lt;code&gt;mid&lt;/code&gt;, and compare &lt;code&gt;nums[mid]&lt;/code&gt; with &lt;code&gt;nums[right]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;nums[mid] &amp;gt; nums[right]&lt;/code&gt;&lt;/strong&gt; The smallest element must be in the right half, so move &lt;code&gt;left&lt;/code&gt; to &lt;code&gt;mid + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;nums[mid] &amp;lt;= nums[right]&lt;/code&gt;&lt;/strong&gt; The smallest element is in the left half (including &lt;code&gt;mid&lt;/code&gt;), so move &lt;code&gt;right&lt;/code&gt; to &lt;code&gt;mid&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminate when &lt;code&gt;left == right&lt;/code&gt;&lt;/strong&gt; That position holds the minimum.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000153-find-minimum-in-rotated-sorted-array" rel="noopener noreferrer"&gt;153. Find Minimum in Rotated Sorted Array&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Output: 11&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The array is a sorted array that has been rotated, so it consists of two increasing segments:
&lt;code&gt;[large increasing part, small increasing part]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The minimum is the start of the second segment.&lt;/li&gt;
&lt;li&gt;Comparing &lt;code&gt;mid&lt;/code&gt; with &lt;code&gt;right&lt;/code&gt; helps determine which segment &lt;code&gt;mid&lt;/code&gt; lies in:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;mid&lt;/code&gt; &amp;gt; &lt;code&gt;right&lt;/code&gt;, then &lt;code&gt;mid&lt;/code&gt; is in the first larger segment, so min is to the right.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;mid&lt;/code&gt; &amp;lt;= &lt;code&gt;right&lt;/code&gt;, then &lt;code&gt;mid&lt;/code&gt; is in the second smaller segment or the right part, so min is to the left or at &lt;code&gt;mid&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;This comparison works because all elements are unique and sorted, so no ambiguity.&lt;/li&gt;

&lt;li&gt;The loop ends when the search space narrows to one element — the minimum.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(log n)&lt;/strong&gt;&lt;/em&gt; — each step halves the search space.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — only a few integer variables used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2784. Check if Array is Good</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 14 May 2026 16:48:54 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2784-check-if-array-is-good-37pp</link>
      <guid>https://dev.to/mdarifulhaque/2784-check-if-array-is-good-37pp</guid>
      <description>&lt;p&gt;2784. Check if Array is Good&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Biweekly Contest 109&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;nums&lt;/code&gt;. We consider an array &lt;strong&gt;good&lt;/strong&gt; if it is a permutation of an array &lt;code&gt;base[n]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;base[n] = [1, 2, ..., n - 1, n, n]&lt;/code&gt; (in other words, it is an array of length &lt;code&gt;n + 1&lt;/code&gt; which contains &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n - 1&lt;/code&gt; exactly once, plus two occurrences of &lt;code&gt;n&lt;/code&gt;). For example, &lt;code&gt;base[1] = [1, 1]&lt;/code&gt; and &lt;code&gt;base[3] = [1, 2, 3, 3]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if the given array is good, otherwise return &lt;code&gt;false&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A permutation of integers represents an arrangement of these numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [2, 1, 3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since the maximum element of the array is &lt;code&gt;3&lt;/code&gt;, the only candidate n for which this array could be a permutation of &lt;code&gt;base[n]&lt;/code&gt;, is &lt;code&gt;n = 3&lt;/code&gt;. However, &lt;code&gt;base[3]&lt;/code&gt; has four elements but array &lt;code&gt;nums&lt;/code&gt; has three. Therefore, it can not be a permutation of &lt;code&gt;base[3] = [1, 2, 3, 3]&lt;/code&gt;. So the answer is &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1, 3, 3, 2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since the maximum element of the array is &lt;code&gt;3&lt;/code&gt;, the only candidate n for which this array could be a permutation of &lt;code&gt;base[n]&lt;/code&gt;, is &lt;code&gt;n = 3&lt;/code&gt;. It can be seen that &lt;code&gt;nums&lt;/code&gt; is a permutation of &lt;code&gt;base[3] = [1, 2, 3, 3]&lt;/code&gt; (by swapping the second and fourth elements in &lt;code&gt;nums&lt;/code&gt;, we reach &lt;code&gt;base[3]&lt;/code&gt;). Therefore, the answer is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1, 1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since the maximum element of the array is &lt;code&gt;1&lt;/code&gt;, the only candidate n for which this array could be a permutation of &lt;code&gt;base[n]&lt;/code&gt;, is &lt;code&gt;n = 1&lt;/code&gt;. It can be seen that &lt;code&gt;nums&lt;/code&gt; is a permutation of &lt;code&gt;base[1] = [1, 1]&lt;/code&gt;. Therefore, the answer is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 4:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [3, 4, 4, 1, 2, 1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since the maximum element of the array is &lt;code&gt;4&lt;/code&gt;, the only candidate n for which this array could be a permutation of &lt;code&gt;base[n]&lt;/code&gt;, is &lt;code&gt;n = 4&lt;/code&gt;. However, &lt;code&gt;base[4]&lt;/code&gt; has five elements but array &lt;code&gt;nums&lt;/code&gt; has six. Therefore, it can not be a permutation of &lt;code&gt;base[4] = [1, 2, 3, 4, 4]&lt;/code&gt;. So the answer is &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= num[i] &amp;lt;= 200&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the maximum element of the array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution determines if an array is a permutation of &lt;code&gt;base[n] = [1, 2, ..., n-1, n, n]&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
It first finds the maximum element &lt;code&gt;n&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt;, then verifies the array length matches &lt;code&gt;n+1&lt;/code&gt;, and finally checks that numbers &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt; appear exactly once, and &lt;code&gt;n&lt;/code&gt; appears exactly twice.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; Find the maximum value &lt;code&gt;n&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Check if the array length equals &lt;code&gt;n + 1&lt;/code&gt; (since &lt;code&gt;base[n]&lt;/code&gt; has &lt;code&gt;n+1&lt;/code&gt; elements).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Count frequency of each number in &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; Verify that numbers from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt; appear exactly once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 5:&lt;/strong&gt; Verify that &lt;code&gt;n&lt;/code&gt; appears exactly twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 6:&lt;/strong&gt; If all checks pass, return &lt;code&gt;true&lt;/code&gt;; otherwise, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002784-check-if-array-is-good" rel="noopener noreferrer"&gt;2784. Check if Array is Good&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                      &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Find candidate &lt;code&gt;n&lt;/code&gt;:&lt;/strong&gt; The maximum element of &lt;code&gt;nums&lt;/code&gt; must be the &lt;code&gt;n&lt;/code&gt; in &lt;code&gt;base[n]&lt;/code&gt; because &lt;code&gt;base[n]&lt;/code&gt; contains &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt; once, and &lt;code&gt;n&lt;/code&gt; twice (so &lt;code&gt;n&lt;/code&gt; is the largest number in the array).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Length check:&lt;/strong&gt; &lt;code&gt;base[n]&lt;/code&gt; has length &lt;code&gt;n+1&lt;/code&gt;. If &lt;code&gt;nums&lt;/code&gt; length is different, it cannot be a permutation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frequency check for 1 to n-1:&lt;/strong&gt; Each of these numbers must appear exactly once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frequency check for n:&lt;/strong&gt; &lt;code&gt;n&lt;/code&gt; must appear exactly twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early returns:&lt;/strong&gt; The solution fails as soon as any condition is violated, making it efficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;em&gt;O(m)&lt;/em&gt;*&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Where &lt;code&gt;m&lt;/code&gt; is the length of &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;max()&lt;/code&gt; is O(m), &lt;code&gt;array_count_values()&lt;/code&gt; is O(m), loop from 1 to &lt;code&gt;n-1&lt;/code&gt; is O(n) ≤ O(m) since &lt;code&gt;n+1 = m&lt;/code&gt; if valid.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Frequency array stores at most &lt;code&gt;m&lt;/code&gt; distinct entries.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1674. Minimum Moves to Make Array Complementary</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 13 May 2026 18:22:18 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1674-minimum-moves-to-make-array-complementary-1ljh</link>
      <guid>https://dev.to/mdarifulhaque/1674-minimum-moves-to-make-array-complementary-1ljh</guid>
      <description>&lt;p&gt;1674. Minimum Moves to Make Array Complementary&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Weekly Contest 217&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;nums&lt;/code&gt; of &lt;strong&gt;even&lt;/strong&gt; length &lt;code&gt;n&lt;/code&gt; and an integer &lt;code&gt;limit&lt;/code&gt;. In one move, you can replace any integer from &lt;code&gt;nums&lt;/code&gt; with another integer between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;limit&lt;/code&gt;, inclusive.&lt;/p&gt;

&lt;p&gt;The array &lt;code&gt;nums&lt;/code&gt; is &lt;strong&gt;complementary&lt;/strong&gt; if for all indices &lt;code&gt;i&lt;/code&gt; (&lt;strong&gt;0-indexed&lt;/strong&gt;), &lt;code&gt;nums[i] + nums[n - 1 - i]&lt;/code&gt; equals the same number. For example, the array &lt;code&gt;[1,2,3,4]&lt;/code&gt; is complementary because for all indices &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;nums[i] + nums[n - 1 - i] = 5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;minimum&lt;/strong&gt; number of moves required to make &lt;code&gt;nums&lt;/code&gt; &lt;strong&gt;complementary&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,4,3], limit = 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;In 1 move, you can change nums to &lt;a href="https://dev.tounderlined%20elements%20are%20changed"&gt;1,2,2,3&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;nums[0] + nums[3] = 1 + 3 = 4.&lt;/li&gt;
&lt;li&gt;nums[1] + nums[2] = 2 + 2 = 4.&lt;/li&gt;
&lt;li&gt;nums[2] + nums[1] = 2 + 2 = 4.&lt;/li&gt;
&lt;li&gt;nums[3] + nums[0] = 3 + 1 = 4.&lt;/li&gt;
&lt;li&gt;Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,2,1], limit = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 &amp;gt; limit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,1,2], limit = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; nums is already complementary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n == nums.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= n &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= limit &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; is even.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Given a target sum &lt;code&gt;x&lt;/code&gt;, each pair of &lt;code&gt;nums[i]&lt;/code&gt; and &lt;code&gt;nums[n-1-i]&lt;/code&gt; would either need 0, 1, or 2 modifications.&lt;/li&gt;
&lt;li&gt;Can you find the optimal target sum &lt;code&gt;x&lt;/code&gt; value such that the sum of modifications is minimized?&lt;/li&gt;
&lt;li&gt;Create a difference array to efficiently sum all the modifications.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution uses a &lt;strong&gt;difference array (prefix sum)&lt;/strong&gt; technique to efficiently compute, for each possible target sum &lt;code&gt;S&lt;/code&gt;, the number of moves needed to make all complementary pairs sum to &lt;code&gt;S&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
It iterates over each pair &lt;code&gt;(a, b)&lt;/code&gt; and updates a range of possible target sums with incremental move counts using constant-time range updates. Finally, it scans through all possible sums to find the minimum moves.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Observation for one pair&lt;/strong&gt;: For a given target sum &lt;code&gt;S&lt;/code&gt;, a pair &lt;code&gt;(a, b)&lt;/code&gt; needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;0 moves&lt;/strong&gt; if &lt;code&gt;S == a + b&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1 move&lt;/strong&gt; if &lt;code&gt;min(a, b) + 1 ≤ S ≤ max(a, b) + limit&lt;/code&gt; and &lt;code&gt;S ≠ a + b&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 moves&lt;/strong&gt; otherwise&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Key trick&lt;/strong&gt;: Instead of checking all &lt;code&gt;S&lt;/code&gt; for each pair (which is O(limit²)), we use a &lt;strong&gt;difference array&lt;/strong&gt; to mark where the move count changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Range update logic&lt;/strong&gt; (for a pair &lt;code&gt;(a, b)&lt;/code&gt; with &lt;code&gt;lo = min(a, b)&lt;/code&gt;, &lt;code&gt;hi = max(a, b)&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;lo + 1&lt;/code&gt;: &lt;strong&gt;2 moves&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;From &lt;code&gt;lo + 1&lt;/code&gt; to &lt;code&gt;a + b - 1&lt;/code&gt;: &lt;strong&gt;1 move&lt;/strong&gt; (decrease by 1 from previous)&lt;/li&gt;
&lt;li&gt;At &lt;code&gt;a + b&lt;/code&gt;: &lt;strong&gt;0 moves&lt;/strong&gt; (decrease by 1 again)&lt;/li&gt;
&lt;li&gt;From &lt;code&gt;a + b + 1&lt;/code&gt; to &lt;code&gt;hi + limit&lt;/code&gt;: &lt;strong&gt;1 move&lt;/strong&gt; (increase by 1)&lt;/li&gt;
&lt;li&gt;From &lt;code&gt;hi + limit + 1&lt;/code&gt; to &lt;code&gt;2*limit&lt;/code&gt;: &lt;strong&gt;2 moves&lt;/strong&gt; (increase by 1 again)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefix sum&lt;/strong&gt; over the diff array gives the move count for each &lt;code&gt;S&lt;/code&gt;. Track the minimum value.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001674-minimum-moves-to-make-array-complementary" rel="noopener noreferrer"&gt;1674. Minimum Moves to Make Array Complementary&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @param Integer $limit
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minMoves&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$limit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minMoves&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minMoves&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minMoves&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;diff&lt;/code&gt; array is initialized to size &lt;code&gt;2*limit + 2&lt;/code&gt; to cover all possible sums from &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;2*limit&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each pair &lt;code&gt;(a, b)&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;Start with base cost 2 for all sums (&lt;code&gt;diff[2] += 2&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Decrease by 1 at &lt;code&gt;lo + 1&lt;/code&gt; (1 move zone starts).&lt;/li&gt;
&lt;li&gt;Decrease by 1 at &lt;code&gt;a + b&lt;/code&gt; (0 moves at exact sum).&lt;/li&gt;
&lt;li&gt;Increase by 1 at &lt;code&gt;a + b + 1&lt;/code&gt; (1 move zone resumes).&lt;/li&gt;
&lt;li&gt;Increase by 1 at &lt;code&gt;hi + limit + 1&lt;/code&gt; (2 moves zone restarts).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;After processing all pairs, compute prefix sums of &lt;code&gt;diff&lt;/code&gt; to get total moves for each &lt;code&gt;S&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;Find the minimum across all valid sums.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n + limit)&lt;/strong&gt;&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Each pair updates a constant number of positions in &lt;code&gt;diff&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Prefix sum scan over &lt;code&gt;O(limit)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(limit)&lt;/strong&gt;&lt;/em&gt;, Difference array of size &lt;code&gt;2*limit + 2&lt;/code&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1655. Minimum Initial Energy to Finish Tasks</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 12 May 2026 17:29:23 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1655-minimum-initial-energy-to-finish-tasks-4p5i</link>
      <guid>https://dev.to/mdarifulhaque/1655-minimum-initial-energy-to-finish-tasks-4p5i</guid>
      <description>&lt;p&gt;1655. Minimum Initial Energy to Finish Tasks&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Weekly Contest 216&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an array &lt;code&gt;tasks&lt;/code&gt; where &lt;code&gt;tasks[i] = [actuali, minimumi]&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;actuali&lt;/code&gt; is the actual amount of energy you &lt;strong&gt;spend to finish&lt;/strong&gt; the &lt;code&gt;iᵗʰ&lt;/code&gt; task.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;minimumi&lt;/code&gt; is the minimum amount of energy you &lt;strong&gt;require to begin&lt;/strong&gt; the &lt;code&gt;iᵗʰ&lt;/code&gt; task.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if the task is &lt;code&gt;[10, 12]&lt;/code&gt; and your current energy is &lt;code&gt;11&lt;/code&gt;, you cannot start this task. However, if your current energy is &lt;code&gt;13&lt;/code&gt;, you can complete this task, and your energy will be &lt;code&gt;3&lt;/code&gt; after finishing it.&lt;/p&gt;

&lt;p&gt;You can finish the tasks in &lt;strong&gt;any order&lt;/strong&gt; you like.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;minimum&lt;/strong&gt; initial amount of energy you will need to finish all the tasks&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; tasks = [[1,2],[2,4],[4,8]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 8&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Starting with 8 energy, we finish the tasks in the following order:

&lt;ul&gt;
&lt;li&gt;3rd task. Now energy = 8 - 4 = 4.&lt;/li&gt;
&lt;li&gt;2nd task. Now energy = 4 - 2 = 2.&lt;/li&gt;
&lt;li&gt;1st task. Now energy = 2 - 1 = 1.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 32&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Starting with 32 energy, we finish the tasks in the following order:

&lt;ul&gt;
&lt;li&gt;1st task. Now energy = 32 - 1 = 31.&lt;/li&gt;
&lt;li&gt;2nd task. Now energy = 31 - 2 = 29.&lt;/li&gt;
&lt;li&gt;3rd task. Now energy = 29 - 10 = 19.&lt;/li&gt;
&lt;li&gt;4th task. Now energy = 19 - 10 = 9.&lt;/li&gt;
&lt;li&gt;5th task. Now energy = 9 - 8 = 1.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 27&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Starting with 27 energy, we finish the tasks in the following order:

&lt;ul&gt;
&lt;li&gt;5th task. Now energy = 27 - 5 = 22.&lt;/li&gt;
&lt;li&gt;2nd task. Now energy = 22 - 2 = 20.&lt;/li&gt;
&lt;li&gt;3rd task. Now energy = 20 - 3 = 17.&lt;/li&gt;
&lt;li&gt;1st task. Now energy = 17 - 1 = 16.&lt;/li&gt;
&lt;li&gt;4th task. Now energy = 16 - 4 = 12.&lt;/li&gt;
&lt;li&gt;6th task. Now energy = 12 - 6 = 6.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= tasks.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= actualᵢ &amp;lt;= minimumᵢ &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We can easily figure that the &lt;code&gt;f(x)&lt;/code&gt; : does &lt;code&gt;x&lt;/code&gt; solve this array is monotonic so binary Search is doable&lt;/li&gt;
&lt;li&gt;Figure a sorting pattern&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution determines the &lt;strong&gt;minimum initial energy&lt;/strong&gt; required to complete all tasks in any order, where each task has an &lt;strong&gt;actual energy cost&lt;/strong&gt; and a &lt;strong&gt;minimum required energy to start&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
It uses &lt;strong&gt;greedy sorting&lt;/strong&gt; by &lt;code&gt;(minimum - actual)&lt;/code&gt; in descending order to ensure tasks that are "harder to start" are done earlier, then calculates the needed starting energy.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sorting Strategy&lt;/strong&gt; Sort tasks by &lt;code&gt;(minimum - actual)&lt;/code&gt; in &lt;strong&gt;descending order&lt;/strong&gt;. This prioritizes tasks where the gap between required start energy and actual energy is largest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simulation &amp;amp; Energy Tracking&lt;/strong&gt; Iterate through tasks in sorted order, keeping track of:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;sumActual&lt;/code&gt; = total actual energy spent so far&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;initial&lt;/code&gt; = maximum starting energy needed to reach the current point&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Formula Updating&lt;/strong&gt; For each task &lt;code&gt;(actual, minimum)&lt;/code&gt;, the energy needed before this task is at least &lt;code&gt;minimum + sumActual&lt;/code&gt;. Update &lt;code&gt;initial&lt;/code&gt; to the maximum such value across all tasks, then add &lt;code&gt;actual&lt;/code&gt; to &lt;code&gt;sumActual&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result&lt;/strong&gt; After processing all tasks, &lt;code&gt;initial&lt;/code&gt; contains the minimum starting energy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001655-minimum-initial-energy-to-finish-tasks" rel="noopener noreferrer"&gt;1655. Minimum Initial Energy to Finish Tasks&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[][] $tasks
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minimumEffort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$tasks&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumEffort&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                             &lt;span class="c1"&gt;// Output: 8&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumEffort&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 32&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumEffort&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 27&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Sorting by &lt;code&gt;minimum - actual&lt;/code&gt; descending ensures tasks with &lt;strong&gt;high minimum requirement relative to actual cost&lt;/strong&gt; are attempted first.
This is optimal because starting with less energy fails for tasks that require a high threshold, so earlier energy "buffer" is more valuable for them.&lt;/li&gt;
&lt;li&gt;The greedy choice is proven optimal by exchange arguments (typical for scheduling with constraints).&lt;/li&gt;
&lt;li&gt;The formula &lt;code&gt;initial = max(initial, minimum + sumActual)&lt;/code&gt; ensures that before doing the current task, we have enough energy to meet its &lt;code&gt;minimum&lt;/code&gt; requirement, considering all previous tasks have been done (cost added to &lt;code&gt;sumActual&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;No binary search needed because sorting + linear scan directly gives the minimal starting energy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n log n)&lt;/strong&gt;&lt;/em&gt; due to sorting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; extra space (ignoring input storage).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Sorting Comparison:&lt;/strong&gt; Each comparison is &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iteration:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; after sorting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2553. Separate the Digits in an Array</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 11 May 2026 18:01:37 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2553-separate-the-digits-in-an-array-omj</link>
      <guid>https://dev.to/mdarifulhaque/2553-separate-the-digits-in-an-array-omj</guid>
      <description>&lt;p&gt;2553. Separate the Digits in an Array&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Simulation&lt;/code&gt;, &lt;code&gt;Biweekly Contest 97&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given an array of positive integers &lt;code&gt;nums&lt;/code&gt;, return &lt;em&gt;an array &lt;code&gt;answer&lt;/code&gt; that consists of the digits of each integer in &lt;code&gt;nums&lt;/code&gt; after separating them in &lt;strong&gt;the same order&lt;/strong&gt; they appear in &lt;code&gt;nums&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To separate the digits of an integer is to get all the digits it has in the same order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, for the integer &lt;code&gt;10921&lt;/code&gt;, the separation of its digits is &lt;code&gt;[1,0,9,2,1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [13,25,83,77]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,3,2,5,8,3,7,7]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The separation of 13 is [1,3].&lt;/li&gt;
&lt;li&gt;The separation of 25 is [2,5].&lt;/li&gt;
&lt;li&gt;The separation of 83 is [8,3].&lt;/li&gt;
&lt;li&gt;The separation of 77 is [7,7].&lt;/li&gt;
&lt;li&gt;answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [7,1,3,9]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [7,1,3,9]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;The separation of each integer in nums is itself.&lt;/li&gt;
&lt;li&gt;answer = [7,1,3,9].&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert each number into a list and append that list to the answer.&lt;/li&gt;
&lt;li&gt;You can convert the integer into a string to do that easily.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem requires taking an array of positive integers and returning a new array that contains the individual digits of each number, in the original order of numbers and digits.&lt;br&gt;&lt;br&gt;
The solution uses string conversion to split each number into its digits, then appends those digits to the result array.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Iterate through each integer in the input array.&lt;/li&gt;
&lt;li&gt;Convert each integer to a string to easily access each character (digit).&lt;/li&gt;
&lt;li&gt;Split the string into an array of characters.&lt;/li&gt;
&lt;li&gt;Convert each character back to an integer and append it to the result array.&lt;/li&gt;
&lt;li&gt;Return the final array of digits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002553-separate-the-digits-in-an-array" rel="noopener noreferrer"&gt;2553. Separate the Digits in an Array&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;separateDigits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;separateDigits&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;83&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: [1,3,2,5,8,3,7,7]&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;separateDigits&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// Output: [7,1,3,9]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; Initialize an empty array &lt;code&gt;$answer&lt;/code&gt; to hold the final digits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Loop through each number &lt;code&gt;$num&lt;/code&gt; in &lt;code&gt;$nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Convert &lt;code&gt;$num&lt;/code&gt; to a string and split it into individual characters using &lt;code&gt;str_split()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; Loop through each character in the split array, cast it to an integer, and push it to &lt;code&gt;$answer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 5:&lt;/strong&gt; After processing all numbers, return &lt;code&gt;$answer&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n * m)&lt;/strong&gt;&lt;/em&gt;, where &lt;code&gt;n&lt;/code&gt; is the number of integers in &lt;code&gt;nums&lt;/code&gt; and &lt;code&gt;m&lt;/code&gt; is the maximum number of digits in any integer (up to 6 digits since &lt;code&gt;nums[i] ≤ 10⁵&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(total digits)&lt;/strong&gt;&lt;/em&gt; for the output array, which is the sum of digits across all numbers in &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency:&lt;/strong&gt; The approach is optimal because we must output each digit exactly once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2770. Maximum Number of Jumps to Reach the Last Index</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 10 May 2026 17:59:19 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2770-maximum-number-of-jumps-to-reach-the-last-index-1co1</link>
      <guid>https://dev.to/mdarifulhaque/2770-maximum-number-of-jumps-to-reach-the-last-index-1co1</guid>
      <description>&lt;p&gt;2770. Maximum Number of Jumps to Reach the Last Index&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Weekly Contest 353&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a &lt;strong&gt;0-indexed&lt;/strong&gt; array &lt;code&gt;nums&lt;/code&gt; of &lt;code&gt;n&lt;/code&gt; integers and an integer &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are initially positioned at index &lt;code&gt;0&lt;/code&gt;. In one step, you can jump from index &lt;code&gt;i&lt;/code&gt; to any index &lt;code&gt;j&lt;/code&gt; such that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= i &amp;lt; j &amp;lt; n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-target &amp;lt;= nums[j] - nums[i] &amp;lt;= target&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum number of jumps&lt;/strong&gt; you can make to reach index &lt;code&gt;n - 1&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If there is no way to reach index &lt;code&gt;n - 1&lt;/code&gt;, return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,6,4,1,2], target = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:&lt;/li&gt;
&lt;li&gt;Jump from index 0 to index 1.&lt;/li&gt;
&lt;li&gt;Jump from index 1 to index 3.&lt;/li&gt;
&lt;li&gt;Jump from index 3 to index 5.&lt;/li&gt;
&lt;li&gt;It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,6,4,1,2], target = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:&lt;/li&gt;
&lt;li&gt;Jump from index 0 to index 1.&lt;/li&gt;
&lt;li&gt;Jump from index 1 to index 2.&lt;/li&gt;
&lt;li&gt;Jump from index 2 to index 3.&lt;/li&gt;
&lt;li&gt;Jump from index 3 to index 4.&lt;/li&gt;
&lt;li&gt;Jump from index 4 to index 5.&lt;/li&gt;
&lt;li&gt;It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,6,4,1,2], target = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= nums.length == n &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10⁹ &amp;lt;= nums[i] &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= target &amp;lt;= 2 * 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use a dynamic programming approach.&lt;/li&gt;
&lt;li&gt;Define a dynamic programming array &lt;code&gt;dp&lt;/code&gt; of size &lt;code&gt;n&lt;/code&gt;, where &lt;code&gt;dp[i]&lt;/code&gt; represents the maximum number of jumps from index &lt;code&gt;0&lt;/code&gt; to index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each j iterate over all &lt;code&gt;i &amp;lt; j&lt;/code&gt;. Set &lt;code&gt;dp[j] = max(dp[j], dp[i] + 1)&lt;/code&gt; if &lt;code&gt;-target &amp;lt;= nums[j] - nums[i] &amp;lt;= target&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution uses &lt;strong&gt;dynamic programming&lt;/strong&gt; to find the maximum number of jumps from index &lt;code&gt;0&lt;/code&gt; to index &lt;code&gt;n-1&lt;/code&gt; in the array &lt;code&gt;nums&lt;/code&gt;, where each jump from &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;j&lt;/code&gt; must satisfy &lt;code&gt;-target ≤ nums[j] − nums[i] ≤ target&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
If the last index is unreachable, return &lt;code&gt;-1&lt;/code&gt;. The DP array tracks the maximum jumps possible to reach each index, and the answer is the value stored at the last index.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Programming Array&lt;/strong&gt;: Create a &lt;code&gt;dp&lt;/code&gt; array of size &lt;code&gt;n&lt;/code&gt; where &lt;code&gt;dp[i]&lt;/code&gt; stores the maximum number of jumps to reach index &lt;code&gt;i&lt;/code&gt; from index &lt;code&gt;0&lt;/code&gt;. Initialize &lt;code&gt;dp[0] = 0&lt;/code&gt; and all others to &lt;code&gt;-inf&lt;/code&gt; (or &lt;code&gt;PHP_INT_MIN&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transition&lt;/strong&gt;: For each &lt;code&gt;j&lt;/code&gt; from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;, check all &lt;code&gt;i &amp;lt; j&lt;/code&gt;. If the difference between &lt;code&gt;nums[j]&lt;/code&gt; and &lt;code&gt;nums[i]&lt;/code&gt; is within &lt;code&gt;[-target, target]&lt;/code&gt; and &lt;code&gt;dp[i]&lt;/code&gt; is not &lt;code&gt;-inf&lt;/code&gt;, update &lt;code&gt;dp[j]&lt;/code&gt; as &lt;code&gt;max(dp[j], dp[i] + 1)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result&lt;/strong&gt;: If &lt;code&gt;dp[n-1]&lt;/code&gt; is still &lt;code&gt;-inf&lt;/code&gt;, return &lt;code&gt;-1&lt;/code&gt;. Otherwise, return &lt;code&gt;dp[n-1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002770-maximum-number-of-jumps-to-reach-the-last-index" rel="noopener noreferrer"&gt;2770. Maximum Number of Jumps to Reach the Last Index&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @param Integer $target
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maximumJumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maximumJumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maximumJumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maximumJumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt;: &lt;code&gt;dp[0] = 0&lt;/code&gt; because we start at index 0 with 0 jumps. All other indices are initially unreachable (&lt;code&gt;PHP_INT_MIN&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outer loop (j)&lt;/strong&gt;: Iterates over each target index &lt;code&gt;j&lt;/code&gt; from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt; to compute the best way to reach &lt;code&gt;j&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inner loop (i)&lt;/strong&gt;: Checks all previous indices &lt;code&gt;i&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;j-1&lt;/code&gt; to see if a valid jump from &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;j&lt;/code&gt; exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Valid jump condition&lt;/strong&gt;: &lt;code&gt;-target &amp;lt;= nums[j] - nums[i] &amp;lt;= target&lt;/code&gt; ensures the jump is allowed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State update&lt;/strong&gt;: If &lt;code&gt;i&lt;/code&gt; is reachable (&lt;code&gt;dp[i] != PHP_INT_MIN&lt;/code&gt;), then &lt;code&gt;dp[j] = max(dp[j], dp[i] + 1)&lt;/code&gt; means we can extend the path ending at &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;j&lt;/code&gt; with one more jump.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why this works&lt;/strong&gt;: The DP ensures that &lt;code&gt;dp[j]&lt;/code&gt; always holds the maximum jumps to &lt;code&gt;j&lt;/code&gt; because we consider all possible &lt;code&gt;i &amp;lt; j&lt;/code&gt; that can jump to &lt;code&gt;j&lt;/code&gt; and keep the best value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return&lt;/strong&gt;: If &lt;code&gt;dp[n-1]&lt;/code&gt; is still &lt;code&gt;-inf&lt;/code&gt;, no sequence exists → return &lt;code&gt;-1&lt;/code&gt;. Otherwise, return the computed maximum jumps.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n²)&lt;/strong&gt;&lt;/em&gt;, because we have two nested loops iterating over &lt;code&gt;n&lt;/code&gt; elements each.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, for the DP array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1914. Cyclically Rotating a Grid</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 09 May 2026 14:57:33 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1914-cyclically-rotating-a-grid-3pac</link>
      <guid>https://dev.to/mdarifulhaque/1914-cyclically-rotating-a-grid-3pac</guid>
      <description>&lt;p&gt;1914. Cyclically Rotating a Grid&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Matrix&lt;/code&gt;, &lt;code&gt;Simulation&lt;/code&gt;, &lt;code&gt;Weekly Contest 247&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an &lt;code&gt;m x n&lt;/code&gt; integer matrix &lt;code&gt;grid&lt;/code&gt;, where &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are both &lt;strong&gt;even&lt;/strong&gt; integers, and an integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fug5zs4e17q6cqhfm6c3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fug5zs4e17q6cqhfm6c3l.png" alt="ringofgrid" width="231" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A cyclic rotation of the matrix is done by cyclically rotating &lt;strong&gt;each layer&lt;/strong&gt; in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the &lt;strong&gt;counter-clockwise&lt;/strong&gt; direction. An example rotation is shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58woc9zelu8xben0hcra.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58woc9zelu8xben0hcra.jpg" alt="explanation_grid" width="659" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the matrix after applying &lt;code&gt;k&lt;/code&gt; cyclic rotations to it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9geyneta3kpizak9gbjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9geyneta3kpizak9gbjn.png" alt="rod2" width="421" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [[40,10],[30,20]], k = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[10,20],[40,30]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The figures above represent the grid at every state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7amsx659rzy0x5b7e40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7amsx659rzy0x5b7e40.png" alt="ringofgrid5" width="231" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr86pz0var9e4e38voz6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr86pz0var9e4e38voz6i.png" alt="ringofgrid6" width="231" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr4x5hp5fu351p8ga8j7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr4x5hp5fu351p8ga8j7.png" alt="ringofgrid7" width="231" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The figures above represent the grid at every state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;m == grid.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n == grid[i].length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= m, n &amp;lt;= 50&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are &lt;strong&gt;even&lt;/strong&gt; integers.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= grid[i][j] &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= k &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, you need to consider each layer separately as an array.&lt;/li&gt;
&lt;li&gt;Just cycle this array and then re-assign it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution extracts each &lt;strong&gt;layer&lt;/strong&gt; of the matrix into a 1D array, performs a &lt;strong&gt;counter-clockwise rotation&lt;/strong&gt; by &lt;code&gt;k&lt;/code&gt; positions (modulo the layer’s length), and then places the rotated values back into the layer in the correct order. This process is repeated for all layers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Since both &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are even, the number of layers is &lt;code&gt;min(m, n) / 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each layer, extract its elements in &lt;strong&gt;counter-clockwise order&lt;/strong&gt;: top row → right column → bottom row → left column.&lt;/li&gt;
&lt;li&gt;Compute effective rotations: &lt;code&gt;rot = k % len(elements)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Rotate the array using &lt;code&gt;array_slice&lt;/code&gt; to move the first &lt;code&gt;rot&lt;/code&gt; elements to the end (counter-clockwise = shift left).&lt;/li&gt;
&lt;li&gt;Place the rotated elements back into the grid in the same traversal order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001914-cyclically-rotating-a-grid" rel="noopener noreferrer"&gt;1914. Cyclically Rotating a Grid&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[][] $grid
 * @param Integer $k
 * @return Integer[][]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rotateGrid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateGrid&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                           &lt;span class="c1"&gt;// Output: [[10,20],[40,30]]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateGrid&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Layer extraction order&lt;/strong&gt; matches the rotation direction (counter-clockwise), so rotating the 1D array corresponds to the required layer rotation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modulo operation&lt;/strong&gt; reduces large &lt;code&gt;k&lt;/code&gt; (up to &lt;em&gt;&lt;strong&gt;10⁹&lt;/strong&gt;&lt;/em&gt;) to at most the layer’s perimeter length, making the solution efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traversal for reinsertion&lt;/strong&gt; mirrors the extraction order to correctly place rotated elements into the grid.&lt;/li&gt;
&lt;li&gt;The algorithm works for any &lt;strong&gt;even dimensions&lt;/strong&gt;, with a straightforward way to handle each rectangular concentric layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m × n)&lt;/strong&gt;&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Each element is visited a constant number of times (once to extract, once to reinsert).
&lt;/li&gt;
&lt;li&gt;Layers are processed independently, and total elements = &lt;em&gt;&lt;strong&gt;m × n&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m + n)&lt;/strong&gt;&lt;/em&gt;, In each layer, we store its perimeter elements in a temporary array. The largest perimeter is at the outermost layer: &lt;em&gt;&lt;strong&gt;2 × (m + n) - 4&lt;/strong&gt;&lt;/em&gt;, which is &lt;em&gt;&lt;strong&gt;O(m + n)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3629. Minimum Jumps to Reach End via Prime Teleportation</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 08 May 2026 15:50:33 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3629-minimum-jumps-to-reach-end-via-prime-teleportation-3i2a</link>
      <guid>https://dev.to/mdarifulhaque/3629-minimum-jumps-to-reach-end-via-prime-teleportation-3i2a</guid>
      <description>&lt;p&gt;3629. Minimum Jumps to Reach End via Prime Teleportation&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Breadth-First Search&lt;/code&gt;, &lt;code&gt;Number Theory&lt;/code&gt;, &lt;code&gt;Weekly Contest 460&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;nums&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You start at index 0, and your goal is to reach index &lt;code&gt;n - 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From any index &lt;code&gt;i&lt;/code&gt;, you may perform one of the following operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Adjacent Step&lt;/strong&gt;: Jump to index &lt;code&gt;i + 1&lt;/code&gt; or &lt;code&gt;i - 1&lt;/code&gt;, if the index is within bounds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prime Teleportation&lt;/strong&gt;: If &lt;code&gt;nums[i]&lt;/code&gt; is a prime number&lt;sup id="fnref1"&gt;1&lt;/sup&gt; &lt;code&gt;p&lt;/code&gt;, you may instantly jump to any index &lt;code&gt;j != i&lt;/code&gt; such that &lt;code&gt;nums[j] % p == 0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;strong&gt;minimum&lt;/strong&gt; number of jumps required to reach index &lt;code&gt;n - 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,4,6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;One optimal sequence of jumps is:&lt;/li&gt;
&lt;li&gt;Start at index &lt;code&gt;i = 0&lt;/code&gt;. Take an adjacent step to index 1.&lt;/li&gt;
&lt;li&gt;At index &lt;code&gt;i = 1&lt;/code&gt;, &lt;code&gt;nums[1] = 2&lt;/code&gt; is a prime number. Therefore, we teleport to index &lt;code&gt;i = 3&lt;/code&gt; as &lt;code&gt;nums[3] = 6&lt;/code&gt; is divisible by 2.&lt;/li&gt;
&lt;li&gt;Thus, the answer is 2.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [2,3,4,7,9]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;One optimal sequence of jumps is:&lt;/li&gt;
&lt;li&gt;Start at index &lt;code&gt;i = 0&lt;/code&gt;. Take an adjacent step to index &lt;code&gt;i = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At index &lt;code&gt;i = 1&lt;/code&gt;, &lt;code&gt;nums[1] = 3&lt;/code&gt; is a prime number. Therefore, we teleport to index &lt;code&gt;i = 4&lt;/code&gt; since &lt;code&gt;nums[4] = 9&lt;/code&gt; is divisible by 3.&lt;/li&gt;
&lt;li&gt;Thus, the answer is 2.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [4,6,5,8]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since no teleportation is possible, we move through &lt;code&gt;0 → 1 → 2 → 3&lt;/code&gt;. Thus, the answer is 3.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n == nums.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁶&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use a breadth-first search.&lt;/li&gt;
&lt;li&gt;Precompute prime factors of each &lt;code&gt;nums[i]&lt;/code&gt; via a sieve, and build a bucket &lt;code&gt;bucket[p]&lt;/code&gt; mapping each prime &lt;code&gt;p&lt;/code&gt; to all indices &lt;code&gt;j&lt;/code&gt; with &lt;code&gt;nums[j] % p == 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;During the BFS, when at index &lt;code&gt;i&lt;/code&gt;, enqueue its adjacent steps (&lt;code&gt;i+1&lt;/code&gt; and &lt;code&gt;i-1&lt;/code&gt;) and all indices in &lt;code&gt;bucket[p]&lt;/code&gt; for each prime &lt;code&gt;p&lt;/code&gt; dividing &lt;code&gt;nums[i]&lt;/code&gt;, then clear &lt;code&gt;bucket[p]&lt;/code&gt; so each prime's bucket is visited only once.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solution uses &lt;strong&gt;Breadth-First Search (BFS)&lt;/strong&gt; to find the shortest path from index &lt;code&gt;0&lt;/code&gt; to index &lt;code&gt;n-1&lt;/code&gt; in an array.&lt;br&gt;&lt;br&gt;
Jumps can be either &lt;strong&gt;adjacent moves&lt;/strong&gt; (&lt;code&gt;i+1&lt;/code&gt; or &lt;code&gt;i-1&lt;/code&gt;) or &lt;strong&gt;prime teleportation&lt;/strong&gt; — if &lt;code&gt;nums[i]&lt;/code&gt; is prime, you can jump to any index &lt;code&gt;j&lt;/code&gt; where &lt;code&gt;nums[j]&lt;/code&gt; is divisible by that prime.&lt;br&gt;&lt;br&gt;
To optimize, the solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Precomputes prime factors of all numbers using a &lt;strong&gt;Sieve of Eratosthenes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Builds a mapping from each prime to indices whose values are divisible by it.&lt;/li&gt;
&lt;li&gt;During BFS, when a prime-numbered index is reached, all indices in its bucket are enqueued at once, and the bucket is cleared to avoid repeated processing.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BFS for shortest path&lt;/strong&gt;: Since all jumps have equal cost (1 jump), BFS guarantees minimal steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Precomputation with SPF (Smallest Prime Factor)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;A sieve finds the smallest prime factor for all numbers up to &lt;code&gt;max(nums)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This enables efficient factorization and prime checking.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bucket mapping&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;For each index &lt;code&gt;i&lt;/code&gt;, factorize &lt;code&gt;nums[i]&lt;/code&gt; using the SPF array.&lt;/li&gt;
&lt;li&gt;For each distinct prime factor &lt;code&gt;p&lt;/code&gt;, add &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;bucket[p]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BFS queue state&lt;/strong&gt;: &lt;code&gt;(index, distance)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adjacent moves&lt;/strong&gt;: Always enqueue neighbors if unvisited.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prime teleportation&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nums[i]&lt;/code&gt; is prime &lt;code&gt;p&lt;/code&gt;, and we haven't visited &lt;code&gt;p&lt;/code&gt; before, enqueue &lt;strong&gt;all indices&lt;/strong&gt; in &lt;code&gt;bucket[p]&lt;/code&gt; (except current) and clear &lt;code&gt;bucket[p]&lt;/code&gt; to prevent re-processing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visited tracking&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;visitedIndex&lt;/code&gt; for indices.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;visitedPrime&lt;/code&gt; for primes to avoid teleporting from the same prime multiple times.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003629-minimum-jumps-to-reach-end-via-prime-teleportation" rel="noopener noreferrer"&gt;3629. Minimum Jumps to Reach End via Prime Teleportation&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minJumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minJumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minJumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minJumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1 — Sieve for SPF&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Build &lt;code&gt;spf[x]&lt;/code&gt; = smallest prime factor of &lt;code&gt;x&lt;/code&gt; for all &lt;code&gt;x&lt;/code&gt; up to &lt;code&gt;max(nums)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This allows quick factorization and prime checking in &lt;code&gt;O(log x)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 2 — Build prime-to-indices buckets&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For each &lt;code&gt;nums[i]&lt;/code&gt;, factorize it using SPF to get unique prime factors.&lt;/li&gt;
&lt;li&gt;For each prime &lt;code&gt;p&lt;/code&gt; in factors, append &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;bucket[p]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 3 — BFS initialization&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Start at index &lt;code&gt;0&lt;/code&gt; with distance &lt;code&gt;0&lt;/code&gt;, mark visited.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 4 — Process each index in BFS&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If current index is target, return distance.&lt;/li&gt;
&lt;li&gt;Enqueue &lt;code&gt;i-1&lt;/code&gt; and &lt;code&gt;i+1&lt;/code&gt; if unvisited.&lt;/li&gt;
&lt;li&gt;If current number is prime &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;p&lt;/code&gt; not visited before:

&lt;ul&gt;
&lt;li&gt;Enqueue all indices in &lt;code&gt;bucket[p]&lt;/code&gt; that are unvisited.&lt;/li&gt;
&lt;li&gt;Delete &lt;code&gt;bucket[p]&lt;/code&gt; to avoid future use (primes are used once).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 5 — BFS guarantees minimum steps&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;First time we reach target is guaranteed to have minimum jumps.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Sieve: &lt;code&gt;O(M log log M)&lt;/code&gt; where &lt;code&gt;M = max(nums) ≤ 1e6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Bucket building: &lt;code&gt;O(n log M)&lt;/code&gt; — each number factorized in &lt;code&gt;O(log M)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;BFS: Each index enqueued once (&lt;code&gt;O(n)&lt;/code&gt;), each prime bucket cleared once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overall&lt;/strong&gt;: &lt;code&gt;O(M log log M + n log M)&lt;/code&gt;, efficient for constraints.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;SPF array: &lt;code&gt;O(M)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Buckets: &lt;code&gt;O(n + total factors)&lt;/code&gt; — each index stored once per distinct prime factor.&lt;/li&gt;
&lt;li&gt;Visited sets: &lt;code&gt;O(n + P)&lt;/code&gt; where &lt;code&gt;P&lt;/code&gt; is number of primes ≤ &lt;code&gt;M&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Queue: &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overall&lt;/strong&gt;: &lt;code&gt;O(n + M)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;strong&gt;Prime:&lt;/strong&gt; A prime number is a natural number greater than 1 with only two factors, 1 and itself. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3660. Jump Game IX</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 07 May 2026 18:27:29 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3660-jump-game-ix-5670</link>
      <guid>https://dev.to/mdarifulhaque/3660-jump-game-ix-5670</guid>
      <description>&lt;p&gt;3660. Jump Game IX&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Weekly Contest 464&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;nums&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From any index &lt;code&gt;i&lt;/code&gt;, you can jump to another index &lt;code&gt;j&lt;/code&gt; under the following rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jump to index &lt;code&gt;j&lt;/code&gt; where &lt;code&gt;j &amp;gt; i&lt;/code&gt; is allowed only if &lt;code&gt;nums[j] &amp;lt; nums[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Jump to index &lt;code&gt;j&lt;/code&gt; where &lt;code&gt;j &amp;lt; i&lt;/code&gt; is allowed only if &lt;code&gt;nums[j] &amp;gt; nums[i]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For each index &lt;code&gt;i&lt;/code&gt;, find the &lt;strong&gt;maximum value&lt;/strong&gt; in &lt;code&gt;nums&lt;/code&gt; that can be reached by following &lt;strong&gt;any&lt;/strong&gt; sequence of valid jumps starting at &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return an array &lt;code&gt;ans&lt;/code&gt; where &lt;code&gt;ans[i]&lt;/code&gt; is the &lt;strong&gt;maximum value&lt;/strong&gt; reachable starting from index &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [2,1,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,2,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;i = 0&lt;/code&gt;: No jump increases the value.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;i = 1&lt;/code&gt;: Jump to &lt;code&gt;j = 0&lt;/code&gt; as &lt;code&gt;nums[j] = 2&lt;/code&gt; is greater than &lt;code&gt;nums[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;i = 2&lt;/code&gt;: Since &lt;code&gt;nums[2] = 3&lt;/code&gt; is the maximum value in &lt;code&gt;nums&lt;/code&gt;, no jump increases the value.&lt;/li&gt;
&lt;li&gt;Thus, &lt;code&gt;ans = [2, 2, 3]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [2,3,1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [3,3,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;i = 0&lt;/code&gt;: Jump forward to &lt;code&gt;j = 2&lt;/code&gt; as &lt;code&gt;nums[j] = 1&lt;/code&gt; is less than &lt;code&gt;nums[i] = 2&lt;/code&gt;, then from &lt;code&gt;i = 2&lt;/code&gt; jump to &lt;code&gt;j = 1&lt;/code&gt; as &lt;code&gt;nums[j] = 3&lt;/code&gt; is greater than &lt;code&gt;nums[2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;i = 1&lt;/code&gt;: Since &lt;code&gt;nums[1] = 3&lt;/code&gt; is the maximum value in &lt;code&gt;nums&lt;/code&gt;, no jump increases the value.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;i = 2&lt;/code&gt;: Jump to &lt;code&gt;j = 1&lt;/code&gt; as &lt;code&gt;nums[j] = 3&lt;/code&gt; is greater than &lt;code&gt;nums[2] = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thus, &lt;code&gt;ans = [3, 3, 3]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Think of the array as a directed graph where edges represent valid jumps.&lt;/li&gt;
&lt;li&gt;From index &lt;code&gt;i&lt;/code&gt;, forward jumps go only to smaller values; backward jumps go only to larger values.&lt;/li&gt;
&lt;li&gt;The maximum reachable value from &lt;code&gt;i&lt;/code&gt; is the maximum value in the connected component reachable under these jump rules.&lt;/li&gt;
&lt;li&gt;You can find connected ranges by looking at prefix maximums and suffix minimums, a cut happens where all values to the left are &amp;lt;= all values to the right.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This problem involves a directed graph where each index &lt;code&gt;i&lt;/code&gt; can jump to a larger index &lt;code&gt;j&lt;/code&gt; if &lt;code&gt;nums[j] &amp;lt; nums[i]&lt;/code&gt;, or to a smaller index &lt;code&gt;j&lt;/code&gt; if &lt;code&gt;nums[j] &amp;gt; nums[i]&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
The goal is to determine, for each starting index, the &lt;strong&gt;maximum value&lt;/strong&gt; reachable via any sequence of valid jumps.&lt;/p&gt;

&lt;p&gt;The solution uses the observation that the array can be partitioned into segments where all values in a segment are either &lt;strong&gt;all larger&lt;/strong&gt; or &lt;strong&gt;all smaller&lt;/strong&gt; than values in adjacent segments — meaning no valid jumps can cross segment boundaries.&lt;br&gt;&lt;br&gt;
Inside each segment, any index can reach every other index, so the maximum reachable value from any index in the segment is simply the &lt;strong&gt;maximum value&lt;/strong&gt; in that segment.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Precompute prefix maximums&lt;/strong&gt; and &lt;strong&gt;suffix minimums&lt;/strong&gt; to identify segment boundaries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Segment definition&lt;/strong&gt;: A cut occurs at index &lt;code&gt;i&lt;/code&gt; if all values up to &lt;code&gt;i&lt;/code&gt; are ≤ all values after &lt;code&gt;i&lt;/code&gt;.
This ensures no jump can cross this boundary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Segment building&lt;/strong&gt;: Scan from left to right, when &lt;code&gt;prefixMax[i] &amp;lt;= suffixMin[i+1]&lt;/code&gt;, end current segment and start new one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result assignment&lt;/strong&gt;: For each segment, find its maximum value and assign it to every index in the segment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003660-jump-game-ix" rel="noopener noreferrer"&gt;3660. Jump Game IX&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="nv"&gt;$nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;jumpGameIX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nv"&gt;$nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;jumpGameIX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefix max array&lt;/strong&gt;: &lt;code&gt;prefixMax[i]&lt;/code&gt; = max value from &lt;code&gt;nums[0..i]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Suffix min array&lt;/strong&gt;: &lt;code&gt;suffixMin[i]&lt;/code&gt; = min value from &lt;code&gt;nums[i..n-1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why &lt;code&gt;prefixMax[i] &amp;lt;= suffixMin[i+1]&lt;/code&gt; works as a cut&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;All elements on left ≤ all elements on right&lt;/li&gt;
&lt;li&gt;Any forward jump (j &amp;gt; i) requires &lt;code&gt;nums[j] &amp;lt; nums[i]&lt;/code&gt;, but if right side values are larger, impossible&lt;/li&gt;
&lt;li&gt;Any backward jump (j &amp;lt; i) requires &lt;code&gt;nums[j] &amp;gt; nums[i]&lt;/code&gt;, but if left side values are smaller, impossible&lt;/li&gt;
&lt;li&gt;So no edge across this boundary&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Inside a segment, the graph is strongly connected (can reach any index), so max value in segment = answer for all indices in that segment.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Two linear passes for &lt;code&gt;prefix&lt;/code&gt; &amp;amp; &lt;code&gt;suffix&lt;/code&gt; arrays&lt;/li&gt;
&lt;li&gt;One linear pass to build segments&lt;/li&gt;
&lt;li&gt;One linear pass to assign values&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; - Stores &lt;code&gt;prefixMax&lt;/code&gt;, &lt;code&gt;suffixMin&lt;/code&gt;, &lt;code&gt;ans&lt;/code&gt; arrays (can be optimized but fine for constraints)&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>61. Rotate List</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 05 May 2026 17:09:44 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/61-rotate-list-285j</link>
      <guid>https://dev.to/mdarifulhaque/61-rotate-list-285j</guid>
      <description>&lt;p&gt;61. Rotate List&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Linked List&lt;/code&gt;, &lt;code&gt;Two Pointers&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given the &lt;code&gt;head&lt;/code&gt; of a linked list, rotate the list to the right by &lt;code&gt;k&lt;/code&gt; places.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ea7lad4gzdldyn4oje7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ea7lad4gzdldyn4oje7.jpg" alt="rotate1" width="712" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2,3,4,5], k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [4,5,1,2,3]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6fafcj1glb4j3mk6514.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6fafcj1glb4j3mk6514.jpg" alt="roate2" width="472" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [0,1,2], k = 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,0,1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1], k = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 4:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2,3], k = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,2,3]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 5:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [], k = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; []&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 6:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2,3], k = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,3,1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number of nodes in the list is in the range &lt;code&gt;[0, 500]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-100 &amp;lt;= Node.val &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= k &amp;lt;= 2 * 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solution rotates a singly linked list to the right by &lt;code&gt;k&lt;/code&gt; places in &lt;strong&gt;O(n)&lt;/strong&gt; time and &lt;strong&gt;O(1)&lt;/strong&gt; space. It first computes the list length, reduces &lt;code&gt;k&lt;/code&gt; modulo the length, finds the new head and tail, then performs the rotation by linking the tail to the old head and breaking the list at the new tail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Find list length and tail node&lt;/strong&gt; to handle cases where &lt;code&gt;k&lt;/code&gt; is larger than the list length.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduce &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt; by taking &lt;code&gt;k % length&lt;/code&gt; to avoid unnecessary rotations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Locate new tail&lt;/strong&gt; at position &lt;code&gt;length - k - 1&lt;/code&gt; using traversal from head.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Perform rotation&lt;/strong&gt; by making the original tail point to the original head, and the new tail point to &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return new head&lt;/strong&gt;, which is the node after the new tail.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000061-rotate-list" rel="noopener noreferrer"&gt;61. Rotate List&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param ListNode $head
 * @param Integer $k
 * @return ListNode
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: [4,5,1,2,3]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: [2,0,1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// Output: [1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: [1,2,3]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;([],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Output: []&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotateRight&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: [2,3,1]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases&lt;/strong&gt; If the list is empty, has one node, or &lt;code&gt;k == 0&lt;/code&gt;, return the head immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find length and tail&lt;/strong&gt; Traverse the list to count nodes and store the last node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimize &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt; Since rotating by &lt;code&gt;length&lt;/code&gt; gives the same list, set &lt;code&gt;k = k % length&lt;/code&gt;. If &lt;code&gt;k == 0&lt;/code&gt;, no rotation is needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find new tail position&lt;/strong&gt; The new tail is at index &lt;code&gt;length - k - 1&lt;/code&gt; (0-based). Traverse from head to this node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-link nodes&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Original tail's &lt;code&gt;next&lt;/code&gt; points to original head (close loop).&lt;/li&gt;
&lt;li&gt;New tail's &lt;code&gt;next&lt;/code&gt; is set to &lt;code&gt;null&lt;/code&gt; (break loop).&lt;/li&gt;
&lt;li&gt;New head is &lt;code&gt;newTail-&amp;gt;next&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return new head&lt;/strong&gt; This completes the rotation.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; - One pass to find length, another partial pass to find new tail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; - Only a few pointers used, no extra data structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
