<?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>Forem: MD ARIFUL HAQUE</title>
    <description>The latest articles on Forem by MD ARIFUL HAQUE (@mdarifulhaque).</description>
    <link>https://forem.com/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>Forem: MD ARIFUL HAQUE</title>
      <link>https://forem.com/mdarifulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mdarifulhaque"/>
    <language>en</language>
    <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://forem.com/mdarifulhaque/1674-minimum-moves-to-make-array-complementary-1ljh</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/1655-minimum-initial-energy-to-finish-tasks-4p5i</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/2553-separate-the-digits-in-an-array-omj</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/2770-maximum-number-of-jumps-to-reach-the-last-index-1co1</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/1914-cyclically-rotating-a-grid-3pac</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/3629-minimum-jumps-to-reach-end-via-prime-teleportation-3i2a</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/3660-jump-game-ix-5670</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/61-rotate-list-285j</link>
      <guid>https://forem.com/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>
    <item>
      <title>48. Rotate Image</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 04 May 2026 15:09:21 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/48-rotate-image-40hp</link>
      <guid>https://forem.com/mdarifulhaque/48-rotate-image-40hp</guid>
      <description>&lt;p&gt;48. Rotate Image&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;Math&lt;/code&gt;, &lt;code&gt;Matrix&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an &lt;code&gt;n x n&lt;/code&gt; 2D &lt;code&gt;matrix&lt;/code&gt; representing an image, rotate the image by &lt;strong&gt;90&lt;/strong&gt; degrees (clockwise).&lt;/p&gt;

&lt;p&gt;You have to rotate the image &lt;a href="https://en.wikipedia.org/wiki/In-place_algorithm" rel="noopener noreferrer"&gt;in-place&lt;/a&gt;, which means you have to modify the input 2D matrix directly. &lt;strong&gt;DO NOT&lt;/strong&gt; allocate another 2D matrix and do the rotation.&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%2Foibx72q2x3blvohbu603.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%2Foibx72q2x3blvohbu603.jpg" alt="mat1" width="642" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; matrix = [[1,2,3],[4,5,6],[7,8,9]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[7,4,1],[8,5,2],[9,6,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%2F8zwvb86p70uq5qfy6r1r.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%2F8zwvb86p70uq5qfy6r1r.jpg" alt="mat2" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]&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 == matrix.length == matrix[i].length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 20&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-1000 &amp;lt;= matrix[i][j] &amp;lt;= 1000&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;The solution rotates an &lt;code&gt;n x n&lt;/code&gt; matrix 90 degrees clockwise &lt;strong&gt;in-place&lt;/strong&gt; without using extra memory. It achieves this by a &lt;strong&gt;two-step process&lt;/strong&gt;: first transposing the matrix (swap rows and columns), then reversing each row. This approach is a standard and efficient solution for this problem.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Transpose the matrix&lt;/strong&gt; — Convert all &lt;code&gt;matrix[i][j]&lt;/code&gt; to &lt;code&gt;matrix[j][i]&lt;/code&gt; by swapping elements across the diagonal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reverse each row&lt;/strong&gt; — After transposition, reversing the order of elements in each row effectively completes the 90° clockwise rotation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-place operations&lt;/strong&gt; — Both steps modify the original matrix directly, satisfying the problem constraint.&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/000048-rotate-image" rel="noopener noreferrer"&gt;48. Rotate Image&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[][] $matrix
 * @return NULL
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$matrix&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;rotate&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="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,4,1],[8,5,2],[9,6,3]]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotate&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;9&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;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;8&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;13&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;7&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;14&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;16&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: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,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;
&lt;strong&gt;Transpose&lt;/strong&gt; brings rows into columns, which is part of the rotation but in the wrong order.

&lt;ul&gt;
&lt;li&gt;In a 3×3 matrix, first row &lt;code&gt;[1, 2, 3]&lt;/code&gt; becomes first column &lt;code&gt;[1, 4, 7]&lt;/code&gt; after transpose.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Reversing each row&lt;/strong&gt; arranges these columns horizontally in the correct order for clockwise rotation.

&lt;ul&gt;
&lt;li&gt;After transpose, first row is &lt;code&gt;[1, 4, 7]&lt;/code&gt;. After reversal → &lt;code&gt;[7, 4, 1]&lt;/code&gt;, which matches final first row.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;For a matrix of size &lt;code&gt;n&lt;/code&gt;, the inner loop for transpose starts at &lt;code&gt;i + 1&lt;/code&gt; to avoid double-swapping.&lt;/li&gt;

&lt;li&gt;The reversal step uses &lt;code&gt;array_reverse&lt;/code&gt; for simplicity, but a manual swap loop would also work and be just as 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;strong&gt;O(n²)&lt;/strong&gt;&lt;/em&gt; — Each element is accessed once during transpose (n²/2 swaps) and once during row reversal (n × n/2 swaps).&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 constant amount of extra space is 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>788. Rotated Digits</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 02 May 2026 17:39:42 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/788-rotated-digits-4bjl</link>
      <guid>https://forem.com/mdarifulhaque/788-rotated-digits-4bjl</guid>
      <description>&lt;p&gt;788. Rotated Digits&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;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Weekly Contest 73&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;An integer &lt;code&gt;x&lt;/code&gt; is a &lt;strong&gt;good&lt;/strong&gt; if after rotating each digit individually by 180 degrees, we get a valid number that is different from &lt;code&gt;x&lt;/code&gt;. Each digit must be rotated - we cannot choose to leave it alone.&lt;/p&gt;

&lt;p&gt;A number is valid if each digit remains a digit after rotation. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, and &lt;code&gt;8&lt;/code&gt; rotate to themselves,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt; rotate to each other (in this case they are rotated in a different direction, in other words, &lt;code&gt;2&lt;/code&gt; or &lt;code&gt;5&lt;/code&gt; gets mirrored),&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;6&lt;/code&gt; and &lt;code&gt;9&lt;/code&gt; rotate to each other, and&lt;/li&gt;
&lt;li&gt;the rest of the numbers do not rotate to any other number and become invalid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given an integer &lt;code&gt;n&lt;/code&gt;, return &lt;em&gt;the number of &lt;strong&gt;good&lt;/strong&gt; integers in the range &lt;code&gt;[1, n]&lt;/code&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; n = 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;There are four good numbers in the range [1, 10] : 2, 5, 6, 9.&lt;/li&gt;
&lt;li&gt;Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.&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; n = 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;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 2&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; n = 100&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 40&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; n = 30&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 13&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; n = 10000&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2320&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 &amp;lt;= 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;The solution counts integers between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; that are &lt;strong&gt;good&lt;/strong&gt; — meaning when each digit is rotated 180°, the resulting number is &lt;strong&gt;different from the original&lt;/strong&gt; and contains &lt;strong&gt;no invalid digits&lt;/strong&gt; (3, 4, 7).&lt;br&gt;&lt;br&gt;
It iterates over each number, checks digit-by-digit using the rotation rules, and increments a counter for good numbers.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Loop through all integers from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each number, check if it is “good” using digit-by-digit validation.&lt;/li&gt;
&lt;li&gt;A number is &lt;strong&gt;not good&lt;/strong&gt; if:

&lt;ul&gt;
&lt;li&gt;Any digit is &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;4&lt;/code&gt;, or &lt;code&gt;7&lt;/code&gt; (invalid).&lt;/li&gt;
&lt;li&gt;After rotation, the number remains unchanged (i.e., contains only &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, or &lt;code&gt;8&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A number is &lt;strong&gt;good&lt;/strong&gt; if:

&lt;ul&gt;
&lt;li&gt;No invalid digits.&lt;/li&gt;
&lt;li&gt;At least one digit from &lt;code&gt;{2,5,6,9}&lt;/code&gt; exists (since those change on rotation).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Return the total count.&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/000788-rotated-digits" rel="noopener noreferrer"&gt;788. Rotated Digits&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 $n
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rotatedDigits&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;$n&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="cd"&gt;/**
 * Check if a number is "good"
 *
 * @param Integer $num
 * @return bool
 */&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;int&lt;/span&gt; &lt;span class="nv"&gt;$num&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;rotatedDigits&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="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&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotatedDigits&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="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotatedDigits&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;rotatedDigits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&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: 40&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotatedDigits&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="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: 13&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;rotatedDigits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&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: 2320&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;Digit rotation mapping:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0,1,8&lt;/code&gt; → rotate to themselves.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2,5&lt;/code&gt; → rotate to each other.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;6,9&lt;/code&gt; → rotate to each other.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3,4,7&lt;/code&gt; → become invalid.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why &lt;code&gt;hasRotating&lt;/code&gt; flag?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If all digits are in &lt;code&gt;{0,1,8}&lt;/code&gt;, the rotated number equals the original → &lt;strong&gt;not good&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If any digit is in &lt;code&gt;{2,5,6,9}&lt;/code&gt;, the rotated number is different → &lt;strong&gt;good&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Early exit:&lt;/strong&gt; If an invalid digit &lt;code&gt;3,4,7&lt;/code&gt; is found, the number is immediately rejected.&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 * d)&lt;/strong&gt;&lt;/em&gt;, where &lt;code&gt;d&lt;/code&gt; is the number of digits in &lt;code&gt;n&lt;/code&gt; (at most 5 for &lt;code&gt;n ≤ 10⁴&lt;/code&gt;). So roughly &lt;em&gt;&lt;strong&gt;O(n log₁₀ n)&lt;/strong&gt;&lt;/em&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 per iteration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trade-off:&lt;/strong&gt; Simple implementation but linear in &lt;code&gt;n&lt;/code&gt;, which is fine given the constraint &lt;code&gt;n ≤ 10⁴&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>396. Rotate Function</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 01 May 2026 17:54:26 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/396-rotate-function-4j42</link>
      <guid>https://forem.com/mdarifulhaque/396-rotate-function-4j42</guid>
      <description>&lt;p&gt;396. Rotate Function&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;Junior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&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;Assume &lt;code&gt;arrₖ&lt;/code&gt; to be an array obtained by rotating &lt;code&gt;nums&lt;/code&gt; by &lt;code&gt;k&lt;/code&gt; positions clock-wise. We define the &lt;strong&gt;rotation function&lt;/strong&gt; &lt;code&gt;F&lt;/code&gt; on &lt;code&gt;nums&lt;/code&gt; as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;F(k) = 0 * arrₖ[0] + 1 * arrₖ[1] + ... + (n - 1) * arrₖ[n - 1].&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;the maximum value of &lt;code&gt;F(0), F(1), ..., F(n-1)&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The test cases are generated so that the answer fits in a &lt;strong&gt;32-bit&lt;/strong&gt; integer.&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 = [4,3,2,6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 26&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.&lt;/strong&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 = [100]&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;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [0,0,0,0]&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;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-100 &amp;lt;= nums[i] &amp;lt;= 100&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;The solution efficiently computes the maximum value of the rotation function &lt;code&gt;F(k)&lt;/code&gt; without explicitly rotating the array for each &lt;code&gt;k&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
It calculates &lt;code&gt;F(0)&lt;/code&gt; directly, then derives each subsequent &lt;code&gt;F(k)&lt;/code&gt; from the previous one using a mathematical recurrence relation.&lt;br&gt;&lt;br&gt;
This approach avoids the &lt;strong&gt;&lt;em&gt;O(n²)&lt;/em&gt;&lt;/strong&gt; brute force and runs in &lt;strong&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/strong&gt; time and &lt;strong&gt;&lt;em&gt;O(1)&lt;/em&gt;&lt;/strong&gt; extra space.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Direct calculation of &lt;code&gt;F(0)&lt;/code&gt;&lt;/strong&gt; &lt;code&gt;F(0) = 0*nums[0] + 1*nums[1] + ... + (n-1)*nums[n-1]&lt;/code&gt; is computed in one pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mathematical recurrence for &lt;code&gt;F(k)&lt;/code&gt;&lt;/strong&gt; Rotating the array clockwise by one position changes &lt;code&gt;F(k)&lt;/code&gt; as follows: &lt;strong&gt;&lt;em&gt;F(k) = F(k-1) + sum(nums) - n ⋅ nums[n-k]&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterative update&lt;/strong&gt; Start from &lt;code&gt;F(0)&lt;/code&gt; and apply the recurrence for each rotation &lt;code&gt;k = 1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;, tracking the maximum value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge case handling&lt;/strong&gt; If &lt;code&gt;n == 1&lt;/code&gt;, return &lt;code&gt;0&lt;/code&gt; immediately since &lt;code&gt;F(0) = 0&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/000396-rotate-function" rel="noopener noreferrer"&gt;396. Rotate Function&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 float|int
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxRotateFunction&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;float&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&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;maxRotateFunction&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;2&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: 26&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxRotateFunction&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;100&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;maxRotateFunction&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;0&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;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: 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;Why the recurrence works&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;When rotating one step clockwise, the last element becomes first, increasing its coefficient from &lt;code&gt;n-1&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt; (a drop of &lt;code&gt;n-1&lt;/code&gt;), and all other elements shift right, increasing their coefficients by 1.
&lt;/li&gt;
&lt;li&gt;The net change is: &lt;strong&gt;&lt;em&gt;F(k) = F(k-1) + sum(nums) - n ⋅ last element before rotation&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Avoiding array rotation&lt;/strong&gt; By keeping track of which element is currently last in the virtual rotation (&lt;code&gt;nums[$n - $k - 1]&lt;/code&gt;), the formula works directly on the original array.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;One-pass optimization&lt;/strong&gt; Instead of recomputing &lt;code&gt;F(k)&lt;/code&gt; from scratch each time, we update in &lt;strong&gt;&lt;em&gt;O(1)&lt;/em&gt;&lt;/strong&gt; per k, leading to &lt;strong&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/strong&gt; total operations.&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;, One pass to compute &lt;code&gt;sum&lt;/code&gt; and &lt;code&gt;F(0)&lt;/code&gt;, plus one pass to update and compare for &lt;code&gt;n-1&lt;/code&gt; rotations.&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 are used, regardless of input size.&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>3742. Maximum Path Score in a Grid</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 30 Apr 2026 17:50:02 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/3742-maximum-path-score-in-a-grid-597e</link>
      <guid>https://forem.com/mdarifulhaque/3742-maximum-path-score-in-a-grid-597e</guid>
      <description>&lt;p&gt;3742. Maximum Path Score in 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;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Matrix&lt;/code&gt;, &lt;code&gt;Weekly Contest 475&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an &lt;code&gt;m x n&lt;/code&gt; grid where each cell contains one of the values 0, 1, or 2. You are also given an integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You start from the top-left corner &lt;code&gt;(0, 0)&lt;/code&gt; and want to reach the bottom-right corner &lt;code&gt;(m - 1, n - 1)&lt;/code&gt; by moving only &lt;strong&gt;right&lt;/strong&gt; or &lt;strong&gt;down&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each cell contributes a specific score and incurs an associated cost, according to their cell values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0: adds 0 to your score and costs 0.&lt;/li&gt;
&lt;li&gt;1: adds 1 to your score and costs 1.&lt;/li&gt;
&lt;li&gt;2: adds 2 to your score and costs 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;strong&gt;maximum&lt;/strong&gt; score achievable without exceeding a total cost of &lt;code&gt;k&lt;/code&gt;, or -1 if no valid path exists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you reach the last cell but the total cost exceeds &lt;code&gt;k&lt;/code&gt;, the path is invalid.&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; grid = [[0, 1],[2, 0]], k = 1&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; The optimal path is:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Cell&lt;/th&gt;
&lt;th&gt;&lt;code&gt;grid[i][j]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Score&lt;/th&gt;
&lt;th&gt;Total Score&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;th&gt;Total Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;(0,0)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(0,1)&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(1,1)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Thus, the maximum possible score is &lt;code&gt;2&lt;/code&gt;.&lt;/p&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; grid = [[0, 1],[1, 2]], k = 1&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; There is no path that reaches cell &lt;code&gt;(1, 1)&lt;/code&gt; without exceeding cost k. Thus, the answer is &lt;code&gt;-1&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;= m, n &amp;lt;= 200&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= k &amp;lt;= 10³&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;grid[0][0] == 0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= grid[i][j] &amp;lt;= 2&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 dynamic programming.&lt;/li&gt;
&lt;li&gt;Let &lt;code&gt;dp[i][j][c]&lt;/code&gt; = max score at cell &lt;code&gt;(i,j)&lt;/code&gt; with total cost exactly &lt;code&gt;c&lt;/code&gt; (0 &amp;lt;= &lt;code&gt;c&lt;/code&gt; &amp;lt;= &lt;code&gt;k&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Update &lt;code&gt;dp[i][j][c]&lt;/code&gt; from &lt;code&gt;(i-1,j)&lt;/code&gt; and &lt;code&gt;(i,j-1)&lt;/code&gt; using &lt;code&gt;cost = (grid[i][j] == 0 ? 0 : 1)&lt;/code&gt; and &lt;code&gt;score = grid[i][j]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Answer = &lt;code&gt;max(dp[m-1][n-1][c])&lt;/code&gt; for &lt;code&gt;c=0..k&lt;/code&gt;, or &lt;code&gt;-1&lt;/code&gt; if none.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;This problem asks for the maximum score from &lt;strong&gt;&lt;em&gt;(0,0)&lt;/em&gt;&lt;/strong&gt; to &lt;strong&gt;&lt;em&gt;(m-1,n-1)&lt;/em&gt;&lt;/strong&gt; moving only right or down, where each cell contributes a score and a cost based on its value (0→score 0, cost 0; 1→score 1, cost 1; 2→score 2, cost 1), with a total cost limit &lt;strong&gt;&lt;em&gt;k&lt;/em&gt;&lt;/strong&gt;. The solution uses dynamic programming where &lt;code&gt;dp[i][j][c]&lt;/code&gt; is the maximum score at cell &lt;code&gt;(i,j)&lt;/code&gt; with &lt;strong&gt;exactly&lt;/strong&gt; cost &lt;code&gt;c&lt;/code&gt;, and transitions come from left or above. To save memory, we only keep two rows of &lt;code&gt;dp&lt;/code&gt; states.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DP definition&lt;/strong&gt; &lt;code&gt;dp[i][j][c]&lt;/code&gt; = maximum score achievable at cell &lt;code&gt;(i,j)&lt;/code&gt; with total cost &lt;strong&gt;exactly&lt;/strong&gt; &lt;code&gt;c&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transitions&lt;/strong&gt; From &lt;code&gt;(i-1,j)&lt;/code&gt; (above) or &lt;code&gt;(i,j-1)&lt;/code&gt; (left), add the current cell’s score and cost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory optimization&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Since &lt;strong&gt;&lt;em&gt;m, n ≤ 200&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;k ≤ 10³&lt;/em&gt;&lt;/strong&gt;, a full 3D array would be large (~ &lt;strong&gt;&lt;em&gt;200 × 200 × 1001&lt;/em&gt;&lt;/strong&gt; ints).
&lt;/li&gt;
&lt;li&gt;We use two 2D arrays &lt;code&gt;prev&lt;/code&gt; (for previous row) and &lt;code&gt;curr&lt;/code&gt; (for current row), each of size &lt;code&gt;n × (k+1)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Initialization&lt;/strong&gt; &lt;code&gt;(0,0)&lt;/code&gt; starts with its own cost and score.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Processing&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;First row: fill left to right.
&lt;/li&gt;
&lt;li&gt;Then for each next row, process first column using &lt;code&gt;prev&lt;/code&gt;, then remaining columns using both &lt;code&gt;prev&lt;/code&gt; and &lt;code&gt;curr&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Answer&lt;/strong&gt; Max of &lt;code&gt;dp[m-1][n-1][c]&lt;/code&gt; for all &lt;code&gt;c ≤ k&lt;/code&gt;. If all -1, return -1.&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/003742-maximum-path-score-in-a-grid" rel="noopener noreferrer"&gt;3742. Maximum Path Score in 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;maxPathScore&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;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="nv"&gt;$grid1&lt;/span&gt; &lt;span class="o"&gt;=&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="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="nv"&gt;$k1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxPathScore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$grid1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k1&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="nv"&gt;$grid2&lt;/span&gt; &lt;span class="o"&gt;=&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="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="nv"&gt;$k2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxPathScore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$grid2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k2&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;Step 1: Start at (0,0)&lt;/strong&gt; Only one path starts here. Add its cost and score to &lt;code&gt;dp[0][0][cost]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2: Fill first row&lt;/strong&gt; Each cell only has a path from left. For each previous cost &lt;code&gt;c&lt;/code&gt; that’s reachable, create new cost = &lt;code&gt;c + cell_cost&lt;/code&gt;, new score = &lt;code&gt;prev_score + cell_score&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3: Fill first column of next rows&lt;/strong&gt; Each cell only has a path from above. Same transition logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4: Fill remaining cells&lt;/strong&gt; Each cell can come from left (already in &lt;code&gt;curr&lt;/code&gt;) or above (in &lt;code&gt;prev&lt;/code&gt;). Take max of both transitions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 5: Answer retrieval&lt;/strong&gt; After processing all rows, last cell’s DP array contains max scores for each exact cost. We want max score for cost ≤ k, so loop over all costs up to &lt;code&gt;k&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(m × n × k)&lt;/strong&gt;&lt;/em&gt; — For each cell, we loop over all costs &lt;code&gt;c&lt;/code&gt; from &lt;code&gt;0..k&lt;/code&gt; to update from two possible previous cells.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n × k)&lt;/strong&gt;&lt;/em&gt; — Only two rows of &lt;code&gt;n × (k+1)&lt;/code&gt; DP tables are kept at any time.&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>
