<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MD ARIFUL HAQUE</title>
    <description>The latest articles on DEV Community by MD ARIFUL HAQUE (@mdarifulhaque).</description>
    <link>https://dev.to/mdarifulhaque</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F944478%2F148ec474-230c-4c52-8b8e-5aba51bc6d2e.jpeg</url>
      <title>DEV Community: MD ARIFUL HAQUE</title>
      <link>https://dev.to/mdarifulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdarifulhaque"/>
    <language>en</language>
    <item>
      <title>3903. Smallest Stable Index I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 04 Sep 2026 17:33:02 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3903-smallest-stable-index-i-5040</link>
      <guid>https://dev.to/mdarifulhaque/3903-smallest-stable-index-i-5040</guid>
      <description>&lt;p&gt;3903. Smallest Stable Index I&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;Prefix Sum&lt;/code&gt;, &lt;code&gt;Weekly Contest 498&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; and an integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For each index &lt;code&gt;i&lt;/code&gt;, define its &lt;strong&gt;instability score&lt;/strong&gt; as &lt;code&gt;max(nums[0..i]) - min(nums[i..n - 1])&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;max(nums[0..i])&lt;/code&gt; is the &lt;strong&gt;largest&lt;/strong&gt; value among the elements from index 0 to index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;min(nums[i..n - 1])&lt;/code&gt; is the &lt;strong&gt;smallest&lt;/strong&gt; value among the elements from index &lt;code&gt;i&lt;/code&gt; to index &lt;code&gt;n - 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An index &lt;code&gt;i&lt;/code&gt; is called &lt;strong&gt;stable&lt;/strong&gt; if its instability score is &lt;strong&gt;less than or equal to&lt;/strong&gt; &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;smallest&lt;/strong&gt; stable index. If no such index exists, return -1.&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 = [5,0,1,4], k = 3&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;At index 0: The maximum in &lt;code&gt;[5]&lt;/code&gt; is 5, and the minimum in &lt;code&gt;[5, 0, 1, 4]&lt;/code&gt; is 0, so the instability score is &lt;code&gt;5 - 0 = 5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At index 1: The maximum in &lt;code&gt;[5, 0]&lt;/code&gt; is 5, and the minimum in &lt;code&gt;[0, 1, 4]&lt;/code&gt; is 0, so the instability score is &lt;code&gt;5 - 0 = 5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At index 2: The maximum in &lt;code&gt;[5, 0, 1]&lt;/code&gt; is 5, and the minimum in &lt;code&gt;[1, 4]&lt;/code&gt; is 1, so the instability score is &lt;code&gt;5 - 1 = 4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At index 3: The maximum in &lt;code&gt;[5, 0, 1, 4]&lt;/code&gt; is 5, and the minimum in &lt;code&gt;[4]&lt;/code&gt; is 4, so the instability score is &lt;code&gt;5 - 4 = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This is the first index with an instability score less than or equal to &lt;code&gt;k = 3&lt;/code&gt;. Thus, 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 = [3,2,1], 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;

&lt;ul&gt;
&lt;li&gt;At index 0, the instability score is &lt;code&gt;3 - 1 = 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At index 1, the instability score is &lt;code&gt;3 - 1 = 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At index 2, the instability score is &lt;code&gt;3 - 1 = 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;None of these values is less than or equal to &lt;code&gt;k = 1&lt;/code&gt;, so the answer is -1.&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 = [0], k = 0&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; At index 0, the instability score is &lt;code&gt;0 - 0 = 0&lt;/code&gt;, which is less than or equal to &lt;code&gt;k = 0&lt;/code&gt;. Therefore, the answer is 0.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,3], 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 5:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [4,2,2,4], k = 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 7:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [100], k = 50&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 8:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [5,5,5,5], k = 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;1 &amp;lt;= nums.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= nums[i] &amp;lt;= 10⁹&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;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Simulate as described&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We solve the problem by precomputing prefix maximums and suffix minimums for the given array, then scanning from left to right to find the first index where the instability score &lt;code&gt;(max(0..i) - min(i..n-1))&lt;/code&gt; is ≤ &lt;code&gt;k&lt;/code&gt;. This allows us to evaluate each index in constant time after an &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; preprocessing step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Precompute a &lt;code&gt;prefixMax&lt;/code&gt; array where &lt;code&gt;prefixMax[i]&lt;/code&gt; = maximum value in &lt;code&gt;nums[0..i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Precompute a &lt;code&gt;suffixMin&lt;/code&gt; array where &lt;code&gt;suffixMin[i]&lt;/code&gt; = minimum value in &lt;code&gt;nums[i..n-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Iterate over all indices from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each index &lt;code&gt;i&lt;/code&gt;, compute the instability score as &lt;code&gt;prefixMax[i] - suffixMin[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return the first index where this score ≤ &lt;code&gt;k&lt;/code&gt;. If none, return &lt;code&gt;-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/003903-smallest-stable-index-i" rel="noopener noreferrer"&gt;3903. Smallest Stable Index I&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 $k
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;firstStableIndex&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;$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="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;firstStableIndex&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;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;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="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;firstStableIndex&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;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;firstStableIndex&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="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;firstStableIndex&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&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;firstStableIndex&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;1&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;firstStableIndex&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;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;firstStableIndex&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="mi"&gt;50&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;firstStableIndex&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;5&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;5&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;Prefix maximums&lt;/strong&gt; are built left-to-right: &lt;code&gt;prefixMax[i] = max(prefixMax[i-1], nums[i])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Suffix minimums&lt;/strong&gt; are built right-to-left: &lt;code&gt;suffixMin[i] = min(suffixMin[i+1], nums[i])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instability score&lt;/strong&gt; for index &lt;code&gt;i&lt;/code&gt; is simply the difference between the precomputed prefix max and suffix min at that index.&lt;/li&gt;
&lt;li&gt;Since we process indices in increasing order, the first one satisfying the condition is the smallest stable index.&lt;/li&gt;
&lt;li&gt;This method handles all edge cases, including single‑element arrays and negative results, without needing nested loops.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/strong&gt; – we do three linear passes (prefix, suffix, and scanning) over the array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/strong&gt; – we store two auxiliary arrays of size &lt;code&gt;n&lt;/code&gt; for prefix max and suffix min.&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>3876. Construct Uniform Parity Array II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 03 Sep 2026 18:03:28 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3876-construct-uniform-parity-array-ii-knf</link>
      <guid>https://dev.to/mdarifulhaque/3876-construct-uniform-parity-array-ii-knf</guid>
      <description>&lt;p&gt;3876. Construct Uniform Parity Array II&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;Math&lt;/code&gt;, &lt;code&gt;Weekly Contest 494&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an array &lt;code&gt;nums1&lt;/code&gt; of &lt;code&gt;n&lt;/code&gt; &lt;strong&gt;distinct&lt;/strong&gt; integers.&lt;/p&gt;

&lt;p&gt;You want to construct another array &lt;code&gt;nums2&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt; such that the elements in &lt;code&gt;nums2&lt;/code&gt; are either &lt;strong&gt;all odd or all even&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For each index &lt;code&gt;i&lt;/code&gt;, you must choose &lt;strong&gt;exactly one&lt;/strong&gt; of the following (in any order):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums2[i] = nums1[i] - nums1[j]&lt;/code&gt;, for an index &lt;code&gt;j != i&lt;/code&gt;, such that &lt;code&gt;nums1[i] - nums1[j] &amp;gt;= 1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if it is possible to construct such an array&lt;/em&gt;, otherwise return &lt;em&gt;&lt;code&gt;false&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; nums1 = [1,4,7]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;code&gt;nums2[0] = nums1[0] = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;nums2[1] = nums1[1] - nums1[0] = 4 - 1 = 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;nums2[2] = nums1[2] = 7&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums2 = [1, 3, 7]&lt;/code&gt;, and all elements are odd. Thus, the answer is &lt;code&gt;true&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; nums1 = [2,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; It is not possible to construct &lt;code&gt;nums2&lt;/code&gt; such that all elements have the same parity. Thus, the answer is &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [4,6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;code&gt;nums2[0] = nums1[0] = 4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;nums2[1] = nums1[1] = 6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums2 = [4, 6]&lt;/code&gt;, and all elements are even. Thus, the answer is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [11,16]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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; nums1 = [2,4,6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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; nums1 = [1,3,5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [5,10]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [8,9]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [3,6,9]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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 == nums1.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums1[i] &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums1&lt;/code&gt; consists of distinct integers.&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;Try fixing the parity to either all even or all odd.&lt;/li&gt;
&lt;li&gt;Use the smallest odd/even element if a subtraction is needed to match the chosen parity.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We solve the problem by checking both possible target parities (all odd or all even) for &lt;code&gt;nums2&lt;/code&gt;. For each target parity, we determine whether every element in &lt;code&gt;nums1&lt;/code&gt; can either stay as is (if it already matches the target parity) or be transformed by subtracting the smallest number of the opposite parity, as long as the result remains positive. If either parity works, we return &lt;code&gt;true&lt;/code&gt;; otherwise, &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fix target parity:&lt;/strong&gt; Try to make all elements in &lt;code&gt;nums2&lt;/code&gt; odd, then try to make all even.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key observation:&lt;/strong&gt; To change the parity of an element, we must subtract an odd number (since odd – odd = even, even – odd = odd). The best candidate for subtraction is the &lt;strong&gt;smallest odd number&lt;/strong&gt; in &lt;code&gt;nums1&lt;/code&gt;, because using a larger odd number would make the result smaller and could go below 1, making it invalid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check validity for each parity:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;all odd&lt;/strong&gt;: Any even number must be subtracted by the smallest odd number &lt;code&gt;minOdd&lt;/code&gt;, and the result must be at least 1.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;all even&lt;/strong&gt;: Any odd number must be subtracted by &lt;code&gt;minOdd&lt;/code&gt; (an odd number) to become even, and the result must be at least 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Special case:&lt;/strong&gt; If there are no odd numbers, all elements are already even, so all-even is trivially possible.&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/003876-construct-uniform-parity-array-ii" rel="noopener noreferrer"&gt;3876. Construct Uniform Parity Array II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums1
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;uniformArray&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;$nums1&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;uniformArray&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;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="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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;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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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;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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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;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: true&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;We first find the minimum odd number in &lt;code&gt;nums1&lt;/code&gt;. This is crucial because subtracting any odd number preserves the parity change but the smallest one gives the largest possible result, avoiding negative or zero values.&lt;/li&gt;
&lt;li&gt;To check &lt;strong&gt;all odd&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Iterate through &lt;code&gt;nums1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If a number is already odd, we can keep it as is.&lt;/li&gt;
&lt;li&gt;If it’s even, we must subtract &lt;code&gt;minOdd&lt;/code&gt;. If &lt;code&gt;minOdd&lt;/code&gt; doesn’t exist or &lt;code&gt;num - minOdd &amp;lt; 1&lt;/code&gt;, this parity fails.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To check &lt;strong&gt;all even&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If no odd numbers exist, all are even → success.&lt;/li&gt;
&lt;li&gt;Otherwise, for each odd number, subtract &lt;code&gt;minOdd&lt;/code&gt;; if the result is less than 1, this parity fails.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If either check passes, return &lt;code&gt;true&lt;/code&gt;; else &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&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; — We scan the array a constant number of times (to find min odd and to validate each parity).&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; — We use only a few extra variables.&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>3875. Construct Uniform Parity Array I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 02 Sep 2026 17:54:24 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3875-construct-uniform-parity-array-i-721</link>
      <guid>https://dev.to/mdarifulhaque/3875-construct-uniform-parity-array-i-721</guid>
      <description>&lt;p&gt;3875. Construct Uniform Parity Array I&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;Math&lt;/code&gt;, &lt;code&gt;Weekly Contest 494&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an array &lt;code&gt;nums1&lt;/code&gt; of n &lt;strong&gt;distinct&lt;/strong&gt; integers.&lt;/p&gt;

&lt;p&gt;You want to construct another array &lt;code&gt;nums2&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt; such that the elements in &lt;code&gt;nums2&lt;/code&gt; are either &lt;strong&gt;all odd or all even&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For each index &lt;code&gt;i&lt;/code&gt;, you must choose &lt;strong&gt;exactly one&lt;/strong&gt; of the following (in any order):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums2[i] = nums1[i] - nums1[j], for an index j != i&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if it is possible to construct such an array&lt;/em&gt;, otherwise, return &lt;em&gt;&lt;code&gt;false&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; nums1 = [2,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Choose &lt;code&gt;nums2[0] = nums1[0] - nums1[1] = 2 - 3 = -1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;code&gt;nums2[1] = nums1[1] = 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums2 = [-1, 3]&lt;/code&gt;, and both elements are odd. Thus, the answer is &lt;code&gt;true&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; nums1 = [4,6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Choose &lt;code&gt;nums2[0] = nums1[0] = 4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;code&gt;nums2[1] = nums1[1] = 6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums2 = [4, 6]&lt;/code&gt;, and all elements are even. Thus, the answer is &lt;code&gt;true&lt;/code&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; nums1 = [1, 3, 5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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; nums1 = [2, 4, 6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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; nums1 = [1, 2, 3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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; nums1 = [5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [1, 4, 7, 10]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [3, 6, 9, 12]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [100, 99]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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 == nums1.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums1[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums1&lt;/code&gt; consists of distinct integers.&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;There is only one possible answer.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We have determined that the construction of &lt;code&gt;nums2&lt;/code&gt; is &lt;strong&gt;always possible&lt;/strong&gt; for any valid input array &lt;code&gt;nums1&lt;/code&gt; of distinct integers. The key insight is that we have enough flexibility in choosing either the original value or a difference with another element to force all elements in &lt;code&gt;nums2&lt;/code&gt; to share the same parity. The parity of a difference between two numbers is determined solely by their parity relationship, and since we can choose a different &lt;code&gt;j&lt;/code&gt; for each &lt;code&gt;i&lt;/code&gt; (or even use the same &lt;code&gt;j&lt;/code&gt; for multiple &lt;code&gt;i&lt;/code&gt;), we can always achieve uniform parity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Observation:&lt;/strong&gt; The operation &lt;code&gt;nums1[i] - nums1[j]&lt;/code&gt; changes the parity of &lt;code&gt;nums2[i]&lt;/code&gt; based on the parity of &lt;code&gt;nums1[j]&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; and &lt;code&gt;nums1[j]&lt;/code&gt; have the same parity, their difference is even.&lt;/li&gt;
&lt;li&gt;If they have different parity, their difference is odd.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base Case:&lt;/strong&gt; For any &lt;code&gt;i&lt;/code&gt;, we can always choose &lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt;, preserving its original parity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strategy:&lt;/strong&gt; We can choose to make all elements either odd or even by:

&lt;ul&gt;
&lt;li&gt;If all numbers are already even, keep them all as even.&lt;/li&gt;
&lt;li&gt;If all numbers are already odd, keep them all as odd.&lt;/li&gt;
&lt;li&gt;If there is a mix of even and odd numbers, we can use differences to flip parity for some elements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Critical Insight:&lt;/strong&gt; With at least one even and one odd number in &lt;code&gt;nums1&lt;/code&gt;, for any index &lt;code&gt;i&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; is even, we can make it odd by subtracting an odd number (&lt;code&gt;nums1[j]&lt;/code&gt; where &lt;code&gt;nums1[j]&lt;/code&gt; is odd).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; is odd, we can make it even by subtracting an even number (&lt;code&gt;nums1[j]&lt;/code&gt; where &lt;code&gt;nums1[j]&lt;/code&gt; is even).&lt;/li&gt;
&lt;li&gt;Therefore, we can choose the parity for each element independently, and we can choose all elements to be odd or all to be even.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases:&lt;/strong&gt; When &lt;code&gt;n = 1&lt;/code&gt;, there is no &lt;code&gt;j != i&lt;/code&gt;, so we must use &lt;code&gt;nums2[0] = nums1[0]&lt;/code&gt;. This single element is trivially uniform (both all odd or all even), so the answer is always &lt;code&gt;true&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/003875-construct-uniform-parity-array-i" rel="noopener noreferrer"&gt;3875. Construct Uniform Parity Array I&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[] $nums1
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;uniformArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$connections&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$queries&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;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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;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;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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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;9&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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;uniformArray&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="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="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; Check if all elements in &lt;code&gt;nums1&lt;/code&gt; are even → choose &lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt; for all &lt;code&gt;i&lt;/code&gt; → all even → &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Check if all elements in &lt;code&gt;nums1&lt;/code&gt; are odd → choose &lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt; for all &lt;code&gt;i&lt;/code&gt; → all odd → &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; If there is a mix of even and odd numbers in &lt;code&gt;nums1&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;Let &lt;code&gt;E&lt;/code&gt; be an even element and &lt;code&gt;O&lt;/code&gt; be an odd element from &lt;code&gt;nums1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For any index &lt;code&gt;i&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;To make &lt;code&gt;nums2[i]&lt;/code&gt; even:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; is even → use &lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt; (even).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; is odd → use &lt;code&gt;nums2[i] = nums1[i] - E&lt;/code&gt; (odd - even = odd? Wait, odd - even = odd, that's not even). Actually: odd - even = odd, not even. Let's correct:&lt;/li&gt;
&lt;li&gt;For odd &lt;code&gt;nums1[i]&lt;/code&gt;, to get even: use &lt;code&gt;nums2[i] = nums1[i] - O&lt;/code&gt;? No, odd - odd = even. So use &lt;code&gt;nums2[i] = nums1[i] - O&lt;/code&gt; where &lt;code&gt;O&lt;/code&gt; is an odd element.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To make &lt;code&gt;nums2[i]&lt;/code&gt; odd:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; is odd → use &lt;code&gt;nums2[i] = nums1[i]&lt;/code&gt; (odd).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;nums1[i]&lt;/code&gt; is even → use &lt;code&gt;nums2[i] = nums1[i] - O&lt;/code&gt; (even - odd = odd).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; Since we can always choose the appropriate operation for each index independently, we can construct &lt;code&gt;nums2&lt;/code&gt; with all elements being either odd or even.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 5:&lt;/strong&gt; Therefore, the answer is always &lt;code&gt;true&lt;/code&gt; for all valid inputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; - The solution performs no iterative operations over the array; it simply returns &lt;code&gt;true&lt;/code&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; - No additional data structures are 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>3568. Minimum Moves to Clean the Classroom</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 01 Sep 2026 14:59:54 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3568-minimum-moves-to-clean-the-classroom-2c5k</link>
      <guid>https://dev.to/mdarifulhaque/3568-minimum-moves-to-clean-the-classroom-2c5k</guid>
      <description>&lt;p&gt;3568. Minimum Moves to Clean the Classroom&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;Bit Manipulation&lt;/code&gt;, &lt;code&gt;Breadth-First Search&lt;/code&gt;, &lt;code&gt;Matrix&lt;/code&gt;, &lt;code&gt;Weekly Contest 452&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an &lt;code&gt;m x n&lt;/code&gt; grid &lt;code&gt;classroom&lt;/code&gt; where a student volunteer is tasked with cleaning up litter scattered around the room. Each cell in the grid is one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'S'&lt;/code&gt;: Starting position of the student&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'L'&lt;/code&gt;: Litter that must be collected (once collected, the cell becomes empty)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'R'&lt;/code&gt;: Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'X'&lt;/code&gt;: Obstacle the student cannot pass through&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'.'&lt;/code&gt;: Empty space&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are also given an integer &lt;code&gt;energy&lt;/code&gt;, representing the student's maximum energy capacity. The student starts with this energy from the starting position &lt;code&gt;'S'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Each move to an adjacent cell (up, down, left, or right) costs 1 unit of energy. If the energy reaches 0, the student can only continue if they are on a reset area &lt;code&gt;'R'&lt;/code&gt;, which resets the energy to its &lt;strong&gt;maximum&lt;/strong&gt; capacity &lt;code&gt;energy&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 collect all litter items, or &lt;code&gt;-1&lt;/code&gt; if it's impossible&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; classroom = ["S.", "XL"], energy = 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;

&lt;ul&gt;
&lt;li&gt;The student starts at cell &lt;code&gt;(0, 0)&lt;/code&gt; with 2 units of energy.&lt;/li&gt;
&lt;li&gt;Since cell &lt;code&gt;(1, 0)&lt;/code&gt; contains an obstacle &lt;code&gt;'X'&lt;/code&gt;, the student cannot move directly downward.&lt;/li&gt;
&lt;li&gt;A valid sequence of moves to collect all litter is as follows:&lt;/li&gt;
&lt;li&gt;Move 1: From &lt;code&gt;(0, 0)&lt;/code&gt; → &lt;code&gt;(0, 1)&lt;/code&gt; with 1 unit of energy and 1 unit remaining.&lt;/li&gt;
&lt;li&gt;Move 2: From &lt;code&gt;(0, 1)&lt;/code&gt;→ &lt;code&gt;(1, 1)&lt;/code&gt; to collect the litter &lt;code&gt;'L'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The student collects all the litter using 2 moves. Thus, the output 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; classroom = ["LS", "RL"], energy = 4&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;The student starts at cell &lt;code&gt;(0, 1)&lt;/code&gt; with 4 units of energy.&lt;/li&gt;
&lt;li&gt;A valid sequence of moves to collect all litter is as follows:&lt;/li&gt;
&lt;li&gt;Move 1: From &lt;code&gt;(0, 1)&lt;/code&gt; → &lt;code&gt;(0, 0)&lt;/code&gt; to collect the first litter &lt;code&gt;'L'&lt;/code&gt; with 1 unit of energy used and 3 units remaining.&lt;/li&gt;
&lt;li&gt;Move 2: From &lt;code&gt;(0, 0)&lt;/code&gt; → &lt;code&gt;(1, 0)&lt;/code&gt; to &lt;code&gt;'R'&lt;/code&gt; to reset and restore energy back to 4.&lt;/li&gt;
&lt;li&gt;Move 3: From &lt;code&gt;(1, 0)&lt;/code&gt; → &lt;code&gt;(1, 1)&lt;/code&gt; to collect the second litter &lt;code&gt;'L'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The student collects all the litter using 3 moves. Thus, the output is 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; classroom = ["L.S", "RXL"], energy = 3&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; No valid path collects all &lt;code&gt;'L'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; classroom = ["L", "R", ".", "S", "."], energy = 1&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 5:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; classroom = ["S...................", "....................", "....................", "....................", ".....RL........RR...", "....................", "......L....R..L.....", ".....L..............", ".........L........LL", "....................", "....................", "....................", "........L...........", "....................", "....................", "....................", "....L...............", "....................", ".....R..............", "..............L....."], energy = 20&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 71&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; classroom = ["SL"], energy = 1&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 7:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; classroom = ["S.L", "..R"], energy = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; classroom = ["SX", "XL"], energy = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -1&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; classroom = ["SRL"], energy = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&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 == classroom.length &amp;lt;= 20&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n == classroom[i].length &amp;lt;= 20&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;classroom[i][j]&lt;/code&gt; is one of &lt;code&gt;'S'&lt;/code&gt;, &lt;code&gt;'L'&lt;/code&gt;, &lt;code&gt;'R'&lt;/code&gt;, &lt;code&gt;'X'&lt;/code&gt;, or &lt;code&gt;'.'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= energy &amp;lt;= 50&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;There is exactly &lt;strong&gt;one&lt;/strong&gt; &lt;code&gt;'S'&lt;/code&gt; in the grid.&lt;/li&gt;
&lt;li&gt;There are &lt;strong&gt;at most&lt;/strong&gt; 10 &lt;code&gt;'L'&lt;/code&gt; cells in the grid.&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 BFS with states &lt;code&gt;(x, y, mask, e, steps)&lt;/code&gt;, initializing with &lt;code&gt;(sx, sy, 0, energy, 0)&lt;/code&gt;, and for each move update &lt;code&gt;e&lt;/code&gt; (–1 per step), update &lt;code&gt;mask&lt;/code&gt; on &lt;code&gt;'L'&lt;/code&gt;, reset &lt;code&gt;e=energy&lt;/code&gt; on &lt;code&gt;'R'&lt;/code&gt;, and return &lt;code&gt;steps&lt;/code&gt; when &lt;code&gt;mask == fullMask&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Maintain a 3D array &lt;code&gt;bestEnergy[x][y][mask]&lt;/code&gt; storing the maximum &lt;code&gt;e&lt;/code&gt; seen for each &lt;code&gt;(x,y,mask)&lt;/code&gt; and skip any new state with &lt;code&gt;e &amp;lt;= bestEnergy[x][y][mask]&lt;/code&gt; to prune.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We implement a &lt;strong&gt;state‑space BFS&lt;/strong&gt; over &lt;code&gt;(x, y, mask, energy)&lt;/code&gt;, where &lt;code&gt;mask&lt;/code&gt; tracks which litter cells (&lt;code&gt;L&lt;/code&gt;) have been collected. The search respects energy constraints, resets on &lt;code&gt;R&lt;/code&gt;, and prunes dominated states to keep the problem feasible (≤10 litter, 20×20 grid, energy≤50). The BFS guarantees the first time we visit &lt;code&gt;fullMask&lt;/code&gt; yields the minimum number of moves.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;State definition&lt;/strong&gt; – &lt;code&gt;(x, y, mask, e)&lt;/code&gt;: position, collected‑litter bitmask, current energy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Initialisation&lt;/strong&gt; – start at &lt;code&gt;S&lt;/code&gt; with &lt;code&gt;mask=0&lt;/code&gt;, &lt;code&gt;e=energy&lt;/code&gt;, &lt;code&gt;steps=0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transition&lt;/strong&gt; – for each neighbor not &lt;code&gt;X&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;ne = e - 1&lt;/code&gt;. If &lt;code&gt;e == 0&lt;/code&gt;, we &lt;strong&gt;can only move&lt;/strong&gt; if currently on &lt;code&gt;R&lt;/code&gt; (reset to &lt;code&gt;energy&lt;/code&gt; before moving → &lt;code&gt;ne = energy - 1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;After moving, if the new cell is &lt;code&gt;R&lt;/code&gt;, set &lt;code&gt;ne = energy&lt;/code&gt; (reset).&lt;/li&gt;
&lt;li&gt;If the new cell is &lt;code&gt;L&lt;/code&gt; and not yet in &lt;code&gt;mask&lt;/code&gt;, add its bit.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pruning&lt;/strong&gt; – maintain &lt;code&gt;bestEnergy[x][y][mask]&lt;/code&gt; = maximum energy seen for that state; skip if &lt;code&gt;ne &amp;lt;= bestEnergy[...]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Termination&lt;/strong&gt; – return &lt;code&gt;steps + 1&lt;/code&gt; when &lt;code&gt;mask == fullMask&lt;/code&gt;; if queue empties, return &lt;code&gt;-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/003568-minimum-moves-to-clean-the-classroom" rel="noopener noreferrer"&gt;3568. Minimum Moves to Clean the Classroom&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 String[] $classroom
 * @param Integer $energy
 * @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;$classroom&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;$energy&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="s2"&gt;"S."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"XL"&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="s2"&gt;"LS"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"RL"&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: 3&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="s2"&gt;"L.S"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"RXL"&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: -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="s2"&gt;"L"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"R"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"S"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&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="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="nv"&gt;$classroom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"S..................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;".....RL........RR..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"......L....R..L....."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;".....L.............."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;".........L........LL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"........L..........."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"....L..............."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"...................."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;".....R.............."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"..............L....."&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$energy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&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;minMoves&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$classroom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$energy&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: 71&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="s2"&gt;"SL"&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: 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="s2"&gt;"S.L"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"..R"&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: 5&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="s2"&gt;"SX"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"XL"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minMoves&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"SRL"&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: 2&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BFS overstates, not just cells&lt;/strong&gt; – because energy and collected litter matter, a simple grid BFS is insufficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling energy zero&lt;/strong&gt; – the student may only &lt;strong&gt;leave&lt;/strong&gt; an &lt;code&gt;R&lt;/code&gt; cell with zero energy; we implement this by checking &lt;code&gt;e == 0&lt;/code&gt; before moving and resetting if standing on &lt;code&gt;R&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reset on arrival&lt;/strong&gt; – after moving into &lt;code&gt;R&lt;/code&gt;, we immediately refill energy to &lt;code&gt;energy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mask for litter&lt;/strong&gt; – up to 10 litter cells → bitmask fits in 1024 states; efficient memory with &lt;code&gt;bestEnergy[20][20][1024]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pruning with max energy&lt;/strong&gt; – if we reach the same &lt;code&gt;(x, y, mask)&lt;/code&gt; with lower or equal energy, it is never better than a previous visit with higher energy, so we discard it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correctness&lt;/strong&gt; – BFS explores states in non‑decreasing move count, so the first full‑mask state is optimal. Obstacles and resets are correctly modeled, and all moves are reversible.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time&lt;/strong&gt; – &lt;code&gt;O(m * n * 2ᴸ * energy)&lt;/code&gt; in worst case, but pruning keeps it near &lt;code&gt;O(m * n * 2ᴸ)&lt;/code&gt; because &lt;code&gt;energy&lt;/code&gt; is bounded by 50 and dominated states are skipped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space&lt;/strong&gt; – &lt;code&gt;O(m * n * 2ᴸ)&lt;/code&gt; for &lt;code&gt;bestEnergy&lt;/code&gt;, plus queue overhead.&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>2058. Find the Minimum and Maximum Number of Nodes Between Critical Points</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 31 Aug 2026 16:54:40 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points-2mpd</link>
      <guid>https://dev.to/mdarifulhaque/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points-2mpd</guid>
      <description>&lt;p&gt;2058. Find the Minimum and Maximum Number of Nodes Between Critical Points&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;Linked List&lt;/code&gt;, &lt;code&gt;Weekly Contest 265&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;critical point&lt;/strong&gt; in a linked list is defined as &lt;strong&gt;either&lt;/strong&gt; a &lt;strong&gt;local maxima&lt;/strong&gt; or a &lt;strong&gt;local minima&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A node is a &lt;strong&gt;local maxima&lt;/strong&gt; if the current node has a value &lt;strong&gt;strictly greater&lt;/strong&gt; than the previous node and the next node.&lt;/p&gt;

&lt;p&gt;A node is a &lt;strong&gt;local minima&lt;/strong&gt; if the current node has a value &lt;strong&gt;strictly smaller&lt;/strong&gt; than the previous node and the next node.&lt;/p&gt;

&lt;p&gt;Note that a node can only be a local maxima/minima if there exists &lt;strong&gt;both&lt;/strong&gt; a previous node and a next node.&lt;/p&gt;

&lt;p&gt;Given a linked list &lt;code&gt;head&lt;/code&gt;, return &lt;em&gt;an array of length 2 containing &lt;code&gt;[minDistance, maxDistance]&lt;/code&gt; where &lt;code&gt;minDistance&lt;/code&gt; is the &lt;strong&gt;minimum distance&lt;/strong&gt; between &lt;strong&gt;any two distinct&lt;/strong&gt; critical points and &lt;code&gt;maxDistance&lt;/code&gt; is the &lt;strong&gt;maximum distance&lt;/strong&gt; between &lt;strong&gt;any two distinct&lt;/strong&gt; critical points. If there are &lt;strong&gt;fewer&lt;/strong&gt; than two critical points, return &lt;code&gt;[-1, -1]&lt;/code&gt;&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%2Frdbidurt7qkmi0qb3dlj.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%2Frdbidurt7qkmi0qb3dlj.png" alt="a1" width="148" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [3,1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [-1,-1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no critical points in [3,1].&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%2Fnbcwm6qldirf1sbzlgzg.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%2Fnbcwm6qldirf1sbzlgzg.png" alt="a2" width="624" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [5,3,1,2,5,1,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are three critical points:

&lt;ul&gt;
&lt;li&gt;[5,3,&lt;strong&gt;1&lt;/strong&gt;,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.&lt;/li&gt;
&lt;li&gt;[5,3,1,2,&lt;strong&gt;5&lt;/strong&gt;,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.&lt;/li&gt;
&lt;li&gt;[5,3,1,2,5,&lt;strong&gt;1&lt;/strong&gt;,2]: The sixth node is a local minima because 1 is less than 5 and 2.&lt;/li&gt;
&lt;li&gt;The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.&lt;/li&gt;
&lt;li&gt;The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;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%2Faej48qtc08ou6k1obmp9.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%2Faej48qtc08ou6k1obmp9.png" alt="a5" width="624" height="39"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1,3,2,2,3,2,2,2,7]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [3,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are two critical points:

&lt;ul&gt;
&lt;li&gt;[1,&lt;strong&gt;3&lt;/strong&gt;,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.&lt;/li&gt;
&lt;li&gt;[1,3,2,2,&lt;strong&gt;3&lt;/strong&gt;,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.&lt;/li&gt;
&lt;li&gt;Both the minimum and maximum distances are between the second and the fifth node.&lt;/li&gt;
&lt;li&gt;Thus, minDistance and maxDistance is 5 - 2 = 3.&lt;/li&gt;
&lt;li&gt;Note that the last node is not considered a local maxima because it does not have a next node.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [2,1,3,1,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,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 = [1,2,3,4,5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [-1,-1]&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 = [5,4,3,2,1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [-1,-1]&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; head = [1,2,1,2,1,2,1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,4]&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;[2, 10&lt;sup&gt;5&lt;/sup&gt;]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= Node.val &amp;lt;= 10&lt;sup&gt;5&lt;/sup&gt;&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;The maximum distance must be the distance between the first and last critical point.&lt;/li&gt;
&lt;li&gt;For each adjacent critical point, calculate the difference and check if it is the minimum distance.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We solve this problem by traversing the linked list once, identifying all critical points (local maxima and minima), and tracking their positions. We maintain the first critical point position, the previous critical point position, and calculate the minimum distance between adjacent critical points. The maximum distance is simply the difference between the first and last critical points.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single Pass Traversal&lt;/strong&gt;: Iterate through the linked list while maintaining three pointers: previous, current, and next nodes to evaluate each position&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Critical Point Detection&lt;/strong&gt;: For each node (except first and last), check if it's a local maxima (greater than both neighbors) or local minima (smaller than both neighbors)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Track Positions&lt;/strong&gt;: Keep track of:

&lt;ul&gt;
&lt;li&gt;First critical point index&lt;/li&gt;
&lt;li&gt;Previous critical point index (for calculating minimum distance)&lt;/li&gt;
&lt;li&gt;Current index while traversing&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distance Calculation&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;When a new critical point is found, calculate distance from previous critical point and update minimum distance&lt;/li&gt;
&lt;li&gt;Maximum distance is calculated at the end as the difference between last and first critical points&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Case Handling&lt;/strong&gt;: Return [-1, -1] if fewer than 2 critical points are found&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/002058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points" rel="noopener noreferrer"&gt;2058. Find the Minimum and Maximum Number of Nodes Between Critical Points&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[] $c
 * @param Integer[][] $connections
 * @param Integer[][] $queries
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;nodesBetweenCriticalPoints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$connections&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$queries&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;nodesBetweenCriticalPoints&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="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,-1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: [1,3]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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;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;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&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,3]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: [1,3]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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="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,-1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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;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;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: [-1,-1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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;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;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;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: [1,6]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;nodesBetweenCriticalPoints&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: [2,4]&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;: We start with &lt;code&gt;$result = [-1, -1]&lt;/code&gt; and set &lt;code&gt;$minDistance = PHP_INT_MAX&lt;/code&gt; to track the minimum distance between critical points&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Three-Pointer Technique&lt;/strong&gt;: Using &lt;code&gt;$previousNode&lt;/code&gt;, &lt;code&gt;$currentNode&lt;/code&gt;, and &lt;code&gt;$currentNode-&amp;gt;next&lt;/code&gt; allows us to check if the current node satisfies the critical point conditions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Critical Point Detection&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Local minima: &lt;code&gt;$currentNode-&amp;gt;val &amp;lt; $previousNode-&amp;gt;val &amp;amp;&amp;amp; $currentNode-&amp;gt;val &amp;lt; $currentNode-&amp;gt;next-&amp;gt;val&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Local maxima: &lt;code&gt;$currentNode-&amp;gt;val &amp;gt; $previousNode-&amp;gt;val &amp;amp;&amp;amp; $currentNode-&amp;gt;val &amp;gt; $currentNode-&amp;gt;next-&amp;gt;val&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index Tracking&lt;/strong&gt;: &lt;code&gt;$currentIndex&lt;/code&gt; starts at 1 (since head is at position 0) and increments with each node traversal&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;First Critical Point&lt;/strong&gt;: When the first critical point is found, we set both &lt;code&gt;$firstCriticalIndex&lt;/code&gt; and &lt;code&gt;$previousCriticalIndex&lt;/code&gt; to the current index&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subsequent Critical Points&lt;/strong&gt;: For each new critical point, we calculate the distance from the previous critical point and update &lt;code&gt;$minDistance&lt;/code&gt;, then update &lt;code&gt;$previousCriticalIndex&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final Computation&lt;/strong&gt;: If at least two critical points exist (&lt;code&gt;$minDistance != PHP_INT_MAX&lt;/code&gt;), we compute:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$maxDistance = $previousCriticalIndex - $firstCriticalIndex&lt;/code&gt; (distance between first and last critical points)&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;[$minDistance, $maxDistance]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; where n is the number of nodes in the linked list&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We traverse the list exactly once, performing constant-time operations at each node&lt;/li&gt;
&lt;li&gt;No additional passes or nested loops are required&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We only use a few variables to track indices and distances&lt;/li&gt;
&lt;li&gt;No extra data structures are used that scale with input size&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2091. Removing Minimum and Maximum From Array</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 30 Aug 2026 14:00:19 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2091-removing-minimum-and-maximum-from-array-16f</link>
      <guid>https://dev.to/mdarifulhaque/2091-removing-minimum-and-maximum-from-array-16f</guid>
      <description>&lt;p&gt;2091. Removing Minimum and Maximum From Array&lt;/p&gt;

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

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

&lt;p&gt;You are given a &lt;strong&gt;0-indexed&lt;/strong&gt; array of &lt;strong&gt;distinct&lt;/strong&gt; integers &lt;code&gt;nums&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is an element in &lt;code&gt;nums&lt;/code&gt; that has the &lt;strong&gt;lowest&lt;/strong&gt; value and an element that has the &lt;strong&gt;highest&lt;/strong&gt; value. We call them the &lt;strong&gt;minimum&lt;/strong&gt; and &lt;strong&gt;maximum&lt;/strong&gt; respectively. Your goal is to remove &lt;strong&gt;both&lt;/strong&gt; these elements from the array.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;deletion&lt;/strong&gt; is defined as either removing an element from the front of the array or removing an element from the &lt;strong&gt;back&lt;/strong&gt; of the array.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;minimum&lt;/strong&gt; number of deletions it would take to remove &lt;strong&gt;both&lt;/strong&gt; the minimum and maximum element from the array&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 = [2,&lt;strong&gt;10&lt;/strong&gt;,7,5,4,&lt;strong&gt;1&lt;/strong&gt;,8,6]&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;The minimum element in the array is nums[5], which is 1.&lt;/li&gt;
&lt;li&gt;The maximum element in the array is nums[1], which is 10.&lt;/li&gt;
&lt;li&gt;We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.&lt;/li&gt;
&lt;li&gt;This results in 2 + 3 = 5 deletions, which is the minimum number possible.&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 = [0,&lt;strong&gt;-4&lt;/strong&gt;,&lt;strong&gt;19&lt;/strong&gt;,1,8,-2,-3,5]&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;The minimum element in the array is nums[1], which is -4.&lt;/li&gt;
&lt;li&gt;The maximum element in the array is nums[2], which is 19.&lt;/li&gt;
&lt;li&gt;We can remove both the minimum and maximum by removing 3 elements from the front.&lt;/li&gt;
&lt;li&gt;This results in only 3 deletions, which is the minimum number possible.&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 = [&lt;strong&gt;101&lt;/strong&gt;]&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;There is only one element in the array, which makes it both the minimum and maximum element.&lt;/li&gt;
&lt;li&gt;We can remove it with 1 deletion.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1, 2, 3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 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; nums = [3, 2, 1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&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; nums = [5, 1, 4, 2, 3]&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 7:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [100, -100, 0]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&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;-10⁵ &amp;lt;= nums[i] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The integers in &lt;code&gt;nums&lt;/code&gt; are &lt;strong&gt;distinct&lt;/strong&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;There can only be three scenarios for deletions such that both minimum and maximum elements are removed:&lt;/li&gt;
&lt;li&gt;Scenario 1: Both elements are removed by only deleting from the front.&lt;/li&gt;
&lt;li&gt;Scenario 2: Both elements are removed by only deleting from the back.&lt;/li&gt;
&lt;li&gt;Scenario 3: Delete from the front to remove one of the elements, and delete from the back to remove the other element.&lt;/li&gt;
&lt;li&gt;Compare which of the three scenarios results in the minimum number of moves.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We analyzed the problem and determined that the optimal strategy always falls into one of three possible deletion scenarios. By identifying the positions of the minimum and maximum elements, we can compute the cost of each scenario and return the smallest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Find the indices of the minimum and maximum values in the array.&lt;/li&gt;
&lt;li&gt;Since the array contains distinct integers, these indices are unique.&lt;/li&gt;
&lt;li&gt;Consider three possible ways to delete both elements:

&lt;ol&gt;
&lt;li&gt;Delete only from the front.&lt;/li&gt;
&lt;li&gt;Delete only from the back.&lt;/li&gt;
&lt;li&gt;Delete one from the front and the other from the back.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Compute the number of deletions required for each scenario.&lt;/li&gt;
&lt;li&gt;Return the minimum among the three.&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/002091-removing-minimum-and-maximum-from-array" rel="noopener noreferrer"&gt;2091. Removing Minimum and Maximum From 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;minimumDeletions&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;minimumDeletions&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;10&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;5&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;8&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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDeletions&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="o"&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;19&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;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&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: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDeletions&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;101&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;minimumDeletions&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: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDeletions&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;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: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDeletions&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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="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;minimumDeletions&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&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="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="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;minimumDeletions&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;100&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: 2&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Find min and max indices:&lt;/strong&gt; Traverse the array once to locate the positions of the smallest and largest values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Order indices:&lt;/strong&gt; Let &lt;code&gt;left = min(minIdx, maxIdx)&lt;/code&gt; and &lt;code&gt;right = max(minIdx, maxIdx)&lt;/code&gt; for easier calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scenario 1 – Delete from front only:&lt;/strong&gt; The farther element from the front is at &lt;code&gt;max(left, right)&lt;/code&gt;, so deletions needed = &lt;code&gt;max(left, right) + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scenario 2 – Delete from back only:&lt;/strong&gt; The farther element from the back is at &lt;code&gt;min(left, right)&lt;/code&gt;, so deletions needed = &lt;code&gt;n - min(left, right)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scenario 3 – Delete one from front and one from back:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Remove the left element from front: &lt;code&gt;left + 1&lt;/code&gt; deletions.
&lt;/li&gt;
&lt;li&gt;Remove the right element from back: &lt;code&gt;n - right&lt;/code&gt; deletions.
&lt;/li&gt;
&lt;li&gt;Total = &lt;code&gt;(left + 1) + (n - right)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge case:&lt;/strong&gt; If there is only one element, it is both min and max, and we remove it with 1 deletion.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&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; — single traversal to find min and max indices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — only a few integer variables used.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3734. Lexicographically Smallest Palindromic Permutation Greater Than Target</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 28 Aug 2026 22:00:07 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3734-lexicographically-smallest-palindromic-permutation-greater-than-target-2ibj</link>
      <guid>https://dev.to/mdarifulhaque/3734-lexicographically-smallest-palindromic-permutation-greater-than-target-2ibj</guid>
      <description>&lt;p&gt;3734. Lexicographically Smallest Palindromic Permutation Greater Than Target&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;Two Pointers&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Enumeration&lt;/code&gt;, &lt;code&gt;Weekly Contest 474&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two strings &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt;, each of length &lt;code&gt;n&lt;/code&gt;, consisting of lowercase English letters.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;lexicographically smallest&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/strong&gt; &lt;strong&gt;string&lt;/strong&gt; that is &lt;strong&gt;both&lt;/strong&gt; a &lt;strong&gt;palindromic&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/strong&gt; &lt;strong&gt;permutation&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/strong&gt; of &lt;code&gt;s&lt;/code&gt; and &lt;strong&gt;strictly&lt;/strong&gt; greater than &lt;code&gt;target&lt;/code&gt;. If no such permutation exists, return an empty string.&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; s = "baba", target = "abba"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "baab"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The palindromic permutations of &lt;code&gt;s&lt;/code&gt; (in lexicographical order) are &lt;code&gt;"abba"&lt;/code&gt; and &lt;code&gt;"baab"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The lexicographically smallest permutation that is strictly greater than &lt;code&gt;target&lt;/code&gt; is &lt;code&gt;"baab"&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; s = "baba", target = "bbaa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ""&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The palindromic permutations of &lt;code&gt;s&lt;/code&gt; (in lexicographical order) are &lt;code&gt;"abba"&lt;/code&gt; and &lt;code&gt;"baab"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;None of them is lexicographically strictly greater than &lt;code&gt;target&lt;/code&gt;. Therefore, the answer is &lt;code&gt;""&lt;/code&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; s = "abc", target = "abb"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ""&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;s&lt;/code&gt; has no palindromic permutations. Therefore, the answer is &lt;code&gt;""&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "aac", target = "abb"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "aca"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The only palindromic permutation of &lt;code&gt;s&lt;/code&gt; is &lt;code&gt;"aca"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;"aca" is strictly greater than &lt;code&gt;target&lt;/code&gt;. Therefore, the answer is &lt;code&gt;"aca"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&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; s = "aa", target = "aa"&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; s = "aabb", target = "baaa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "abba"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "zzz", target = "zzz"&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 8:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "abba", target = "aaaa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "abba"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "aabbcc", target = "cccccc"&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 10:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "abcba", target = "abcba"&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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n == s.length == target.length &amp;lt;= 300&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; consist of only lowercase English letters.&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;A palindromic permutation exists only if at most one character has an odd count (for odd-length strings) or all counts are even (for even-length strings).&lt;/li&gt;
&lt;li&gt;Focus on constructing the first half of the palindrome. The second half is determined by mirroring.&lt;/li&gt;
&lt;li&gt;To be lexicographically greater than target, the first half must be greater than or equal to target's first half, with careful handling of the middle character for odd-length strings.&lt;/li&gt;
&lt;li&gt;Use a backtracking approach or greedy selection for each position in the first half, trying the smallest available character that can still produce a valid palindrome.&lt;/li&gt;
&lt;li&gt;After building the first half, mirror it (and add the middle character if needed) to form the full palindrome and verify it is strictly greater than target.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We need to construct the lexicographically smallest palindrome permutation of &lt;code&gt;s&lt;/code&gt; that is strictly greater than &lt;code&gt;target&lt;/code&gt;. Since palindromes are determined entirely by their first half (plus an optional middle character), we reduce the problem to building the smallest valid first half that yields a full palindrome &amp;gt; &lt;code&gt;target&lt;/code&gt;, using a greedy backtracking approach with pruning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Verify that &lt;code&gt;s&lt;/code&gt; has at least one palindromic permutation (at most one character with odd frequency).&lt;/li&gt;
&lt;li&gt;Build a pool of characters available for the first half: each character count divided by 2.&lt;/li&gt;
&lt;li&gt;Use backtracking to construct the first half position by position.&lt;/li&gt;
&lt;li&gt;At each step, try to match &lt;code&gt;target&lt;/code&gt; if possible; otherwise, pick the smallest available character that is greater than &lt;code&gt;target[idx]&lt;/code&gt; to make the prefix strictly greater.&lt;/li&gt;
&lt;li&gt;Once the first half is complete, mirror it, add the middle character if needed, and check if the resulting palindrome &amp;gt; &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return the first valid palindrome found (which will be lexicographically smallest).&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/003734-lexicographically-smallest-palindromic-permutation-greater-than-target" rel="noopener noreferrer"&gt;3734. Lexicographically Smallest Palindromic Permutation Greater Than Target&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 String $s
 * @param String $target
 * @return String
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&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;string&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;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"baba"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"abba"&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: "baab"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"baba"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bbaa"&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;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"abb"&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;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aac"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"abb"&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: "aca"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"aa"&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;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aabb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"baaa"&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: "abba"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"zzz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"zzz"&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;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abba"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"aaaa"&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: "abba"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aabbcc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"cccccc"&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;lexPalindromicPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abcba"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"abcba"&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="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;Validity Check&lt;/strong&gt;: A palindrome permutation exists iff at most one character has an odd count. If not, return &lt;code&gt;""&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pool Construction&lt;/strong&gt;: We only need to permute the first half. For each character, we can use at most &lt;code&gt;count / 2&lt;/code&gt; occurrences.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backtracking Logic&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If the prefix is already greater than &lt;code&gt;target&lt;/code&gt;'s prefix, we simply pick the smallest available character for each remaining position.&lt;/li&gt;
&lt;li&gt;If the prefix is still equal to &lt;code&gt;target&lt;/code&gt;'s prefix, we first try to place the exact same character as &lt;code&gt;target[idx]&lt;/code&gt;. If that fails, we try larger characters in ascending order.&lt;/li&gt;
&lt;li&gt;The recursion stops when the first half is full; then we construct the full palindrome and compare with &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lexicographic Minimality&lt;/strong&gt;: Because we try characters in ascending order and stop at the first successful full palindrome, the result is lexicographically smallest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mirroring&lt;/strong&gt;: For even &lt;code&gt;n&lt;/code&gt;, &lt;em&gt;&lt;strong&gt;palindrome = firstHalf + reverse(firstHalf)&lt;/strong&gt;&lt;/em&gt;. For odd &lt;code&gt;n&lt;/code&gt;, &lt;em&gt;&lt;strong&gt;palindrome = firstHalf + midChar + reverse(firstHalf)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n * 26⁽ⁿ/²⁾)&lt;/strong&gt;&lt;/em&gt; in worst case, but with pruning it’s much faster. The pool limits choices significantly. For &lt;code&gt;n ≤ 300&lt;/code&gt;, backtracking is efficient because the number of distinct characters is fixed (26) and we greedily stop early.&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 recursion stack and storage of the first half.&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;Lexicographically Smaller:&lt;/strong&gt; A string &lt;code&gt;a&lt;/code&gt; is &lt;strong&gt;lexicographically smaller&lt;/strong&gt; than a string &lt;code&gt;b&lt;/code&gt; if in the first position where &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; differ, string &lt;code&gt;a&lt;/code&gt; has a letter that appears earlier in the alphabet than the corresponding letter in &lt;code&gt;b&lt;/code&gt;. If the first &lt;code&gt;min(a.length, b.length)&lt;/code&gt; characters do not differ, then the shorter string is the lexicographically smaller one.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;strong&gt;Palindrome:&lt;/strong&gt; A &lt;strong&gt;palindrome&lt;/strong&gt; is a string that reads the same forward and backward.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;strong&gt;Permutation:&lt;/strong&gt; A &lt;strong&gt;permutation&lt;/strong&gt; is a rearrangement of all the elements of a set.&amp;nbsp;↩&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>3720. Lexicographically Smallest Permutation Greater Than Target</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 27 Aug 2026 17:52:36 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3720-lexicographically-smallest-permutation-greater-than-target-1ap</link>
      <guid>https://dev.to/mdarifulhaque/3720-lexicographically-smallest-permutation-greater-than-target-1ap</guid>
      <description>&lt;p&gt;3720. Lexicographically Smallest Permutation Greater Than Target&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;Hash Table&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Counting&lt;/code&gt;, &lt;code&gt;Enumeration&lt;/code&gt;, &lt;code&gt;Weekly Contest 472&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two strings &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt;, both having length &lt;code&gt;n&lt;/code&gt;, consisting of lowercase English letters.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;lexicographically smallest&lt;/strong&gt; &lt;strong&gt;permutation&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/strong&gt; of &lt;code&gt;s&lt;/code&gt; that is &lt;strong&gt;strictly&lt;/strong&gt; greater than &lt;code&gt;target&lt;/code&gt;. If no permutation of &lt;code&gt;s&lt;/code&gt; is lexicographically strictly greater than &lt;code&gt;target&lt;/code&gt;, return an empty string.&lt;/p&gt;

&lt;p&gt;A string &lt;code&gt;a&lt;/code&gt; is &lt;strong&gt;lexicographically strictly greater&lt;/strong&gt; than a string &lt;code&gt;b&lt;/code&gt; (of the same length) if in the first position where &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; differ, string &lt;code&gt;a&lt;/code&gt; has a letter that appears later in the alphabet than the corresponding letter in &lt;code&gt;b&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; s = "abc", target = "bba"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "bca"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The permutations of &lt;code&gt;s&lt;/code&gt; (in lexicographical order) are &lt;code&gt;"abc"&lt;/code&gt;, &lt;code&gt;"acb"&lt;/code&gt;, &lt;code&gt;"bac"&lt;/code&gt;, &lt;code&gt;"bca"&lt;/code&gt;, &lt;code&gt;"cab"&lt;/code&gt;, and &lt;code&gt;"cba"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The lexicographically smallest permutation that is strictly greater than &lt;code&gt;target&lt;/code&gt; is &lt;code&gt;"bca"&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; s = "leet", target = "code"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "eelt"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The permutations of &lt;code&gt;s&lt;/code&gt; (in lexicographical order) are &lt;code&gt;"eelt"&lt;/code&gt;, &lt;code&gt;"eetl"&lt;/code&gt;, &lt;code&gt;"elet"&lt;/code&gt;, &lt;code&gt;"elte"&lt;/code&gt;, &lt;code&gt;"etel"&lt;/code&gt;, &lt;code&gt;"etle"&lt;/code&gt;, &lt;code&gt;"leet"&lt;/code&gt;, &lt;code&gt;"lete"&lt;/code&gt;, &lt;code&gt;"ltee"&lt;/code&gt;, &lt;code&gt;"teel"&lt;/code&gt;, &lt;code&gt;"tele"&lt;/code&gt;, and &lt;code&gt;"tlee"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The lexicographically smallest permutation that is strictly greater than &lt;code&gt;target&lt;/code&gt; is &lt;code&gt;"eelt"&lt;/code&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; s = "baba", target = "bbaa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ""&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The permutations of &lt;code&gt;s&lt;/code&gt; (in lexicographical order) are &lt;code&gt;"aabb"&lt;/code&gt;, &lt;code&gt;"abab"&lt;/code&gt;, &lt;code&gt;"abba"&lt;/code&gt;, &lt;code&gt;"baab"&lt;/code&gt;, &lt;code&gt;"baba"&lt;/code&gt;, and &lt;code&gt;"bbaa"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;None of them is lexicographically strictly greater than &lt;code&gt;target&lt;/code&gt;. Therefore, the answer is &lt;code&gt;""&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "ab", target = "ab"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "ba"&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; s = "aab", target = "aba"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "baa"&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; s = "z", target = "a"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "z"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "cba", target = "cba"&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 8:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "aaa", target = "aab"&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 9:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "xyz", target = "abc"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "xyz"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "aabc", target = "abac"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "abac"&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;= s.length == target.length &amp;lt;= 300&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; consist of only lowercase English letters.&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;Maintain frequency counts of &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Walk left-to-right; if equal to &lt;code&gt;target[i]&lt;/code&gt; is possible, take it and continue.&lt;/li&gt;
&lt;li&gt;If not, try the smallest letter strictly greater than &lt;code&gt;target[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If neither, backtrack left to the most recent index where you matched &lt;code&gt;target&lt;/code&gt; and try to bump there.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We implement a greedy backtracking algorithm that constructs the lexicographically smallest permutation of &lt;code&gt;s&lt;/code&gt; greater than &lt;code&gt;target&lt;/code&gt; by first matching &lt;code&gt;target&lt;/code&gt; from left to right as much as possible, then finding the rightmost position where we can place a larger character, and finally filling the remaining positions with the smallest available characters in ascending order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Frequency Counting&lt;/strong&gt; – Count occurrences of each character in &lt;code&gt;s&lt;/code&gt; to track available letters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Greedy Match&lt;/strong&gt; – Iterate through &lt;code&gt;target&lt;/code&gt; and consume matching characters from the frequency pool as long as possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backtracking&lt;/strong&gt; – Starting from the last matched position backward, restore consumed characters and attempt to place a character strictly larger than &lt;code&gt;target[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Construction&lt;/strong&gt; – Once a larger character is placed, fill the rest of the string with the remaining characters in ascending order to ensure lexicographic minimality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallback&lt;/strong&gt; – If no divergence point yields a valid permutation, return an empty string.&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/003720-lexicographically-smallest-permutation-greater-than-target" rel="noopener noreferrer"&gt;3720. Lexicographically Smallest Permutation Greater Than Target&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 String $s
 * @param String $target
 * @return String
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&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;string&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;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bba"&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: "bca"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"leet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"code"&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: "eelt"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"baba"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bbaa"&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;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ab"&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: "ba"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"aba"&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: "baa"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&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: "z"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cba"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"cba"&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;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aaa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"aab"&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;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"xyz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"abc"&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: "xyz"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;lexGreaterPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aabc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"abac"&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: "abac"&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 – Frequency Array:&lt;/strong&gt; We create an array &lt;code&gt;cnt[26]&lt;/code&gt; to store how many of each letter are available from &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2 – Longest Prefix Match:&lt;/strong&gt; We walk through &lt;code&gt;target&lt;/code&gt; from left to right. For each &lt;code&gt;target[i]&lt;/code&gt;, if we have that character available, we consume it and increase &lt;code&gt;match_len&lt;/code&gt;. If not, we stop – this gives us the longest prefix of &lt;code&gt;target&lt;/code&gt; that can be formed using characters from &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3 – Backtrack to Find Divergence:&lt;/strong&gt; We iterate &lt;code&gt;i&lt;/code&gt; from &lt;code&gt;match_len&lt;/code&gt; down to &lt;code&gt;0&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;i &amp;lt; match_len&lt;/code&gt;, we restore the character &lt;code&gt;target[i]&lt;/code&gt; back into the pool (because we're undoing that match).&lt;/li&gt;
&lt;li&gt;Then, we try to find a character &lt;code&gt;c&lt;/code&gt; &amp;gt; &lt;code&gt;target[i]&lt;/code&gt; that is still available.&lt;/li&gt;
&lt;li&gt;The first such &lt;code&gt;c&lt;/code&gt; found (smallest possible) gives us the optimal divergence point.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4 – Build the Result&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The prefix is &lt;code&gt;target[0..i-1]&lt;/code&gt; (already matched and fixed).&lt;/li&gt;
&lt;li&gt;The divergence character is &lt;code&gt;c&lt;/code&gt; (the smallest greater letter available).&lt;/li&gt;
&lt;li&gt;The suffix is constructed by appending all remaining characters from the frequency array in ascending order – this guarantees the smallest possible continuation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases:&lt;/strong&gt; If we exhaust all backtracking positions without finding a larger character, no permutation exists, so we return &lt;code&gt;""&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;code&gt;O(n * 26)&lt;/code&gt; = &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Matching phase: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Backtracking phase: at most &lt;code&gt;n&lt;/code&gt; iterations, each scanning up to 26 characters → &lt;code&gt;O(26n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Suffix construction: &lt;code&gt;O(26 + n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Overall linear in &lt;code&gt;n&lt;/code&gt; with a constant factor of 26.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(n)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frequency array: &lt;code&gt;O(26)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Prefix and suffix strings: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Overall linear space relative to input length.&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;Permutation:&lt;/strong&gt; A permutation is a rearrangement of all the characters of a string.&amp;nbsp;↩&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>2904. Shortest and Lexicographically Smallest Beautiful String</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 26 Aug 2026 17:19:35 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2904-shortest-and-lexicographically-smallest-beautiful-string-332e</link>
      <guid>https://dev.to/mdarifulhaque/2904-shortest-and-lexicographically-smallest-beautiful-string-332e</guid>
      <description>&lt;p&gt;2904. Shortest and Lexicographically Smallest Beautiful String&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;String&lt;/code&gt;, &lt;code&gt;Sliding Window&lt;/code&gt;, &lt;code&gt;Weekly Contest 367&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a binary string &lt;code&gt;s&lt;/code&gt; and a positive integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A substring of &lt;code&gt;s&lt;/code&gt; is &lt;strong&gt;beautiful&lt;/strong&gt; if the number of &lt;code&gt;1&lt;/code&gt;'s in it is exactly &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let &lt;code&gt;len&lt;/code&gt; be the length of the &lt;strong&gt;shortest&lt;/strong&gt; beautiful substring.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the lexicographically &lt;strong&gt;smallest&lt;/strong&gt; beautiful substring of string &lt;code&gt;s&lt;/code&gt; with length equal to &lt;code&gt;len&lt;/code&gt;&lt;/em&gt;. If &lt;code&gt;s&lt;/code&gt; doesn't contain a beautiful substring, return &lt;em&gt;an &lt;strong&gt;empty&lt;/strong&gt; string&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A string &lt;code&gt;a&lt;/code&gt; is lexicographically &lt;strong&gt;larger&lt;/strong&gt; than a string &lt;code&gt;b&lt;/code&gt; (of the same length) if in the first position where &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; differ, &lt;code&gt;a&lt;/code&gt; has a character strictly larger than the corresponding character in &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, &lt;code&gt;"abcd"&lt;/code&gt; is lexicographically larger than &lt;code&gt;"abcc"&lt;/code&gt; because the first position they differ is at the fourth character, and &lt;code&gt;d&lt;/code&gt; is greater than &lt;code&gt;c&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; s = "100011001", k = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "11001"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;There are 7 beautiful substrings in this example:&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The substring "100011001".&lt;/li&gt;
&lt;li&gt;The length of the shortest beautiful substring is 5.&lt;/li&gt;
&lt;li&gt;The lexicographically smallest beautiful substring with length 5 is the substring "11001".&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; s = "1011", k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "11"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;There are 3 beautiful substrings in this example:&lt;/li&gt;
&lt;li&gt;The substring "1011".&lt;/li&gt;
&lt;li&gt;The substring "1011".&lt;/li&gt;
&lt;li&gt;The substring "1011".&lt;/li&gt;
&lt;li&gt;The length of the shortest beautiful substring is 2.&lt;/li&gt;
&lt;li&gt;The lexicographically smallest beautiful substring with length 2 is the substring "11".&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; s = "000", k = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ""&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no beautiful substrings in this example.&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; s = "111", k = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "111"&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; s = "01010", k = 1&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 6:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "10101", k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "101"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "1111", k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "11"&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "1001", k = 4&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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= k &amp;lt;= s.length&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Notice that if we consider that index &lt;code&gt;i&lt;/code&gt; is the leftmost index of a beautiful substring, it has only one candidate &lt;code&gt;j&lt;/code&gt;, such that &lt;code&gt;s[i:j]&lt;/code&gt; is beautiful and shortest too.&lt;/li&gt;
&lt;li&gt;We can iterate over all possibilities of leftmost index &lt;code&gt;i&lt;/code&gt; take &lt;code&gt;s[i:j]&lt;/code&gt; and compare with the shortest and the lexicographically smallest beautiful string we could get before index &lt;code&gt;i&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;We solve the problem by iterating through all possible starting positions in the string, extending each substring until we find exactly &lt;code&gt;k&lt;/code&gt; ones, which gives us the shortest beautiful substring starting at that position. We then track the global minimum length and lexicographically smallest among substrings of that minimum length, ensuring an efficient and correct solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Brute force with early termination:&lt;/strong&gt; For each starting index &lt;code&gt;i&lt;/code&gt;, we extend the substring to the right, counting ones until we either exceed &lt;code&gt;k&lt;/code&gt; (break) or reach exactly &lt;code&gt;k&lt;/code&gt; ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Candidate selection:&lt;/strong&gt; When we reach exactly &lt;code&gt;k&lt;/code&gt; ones, the current substring is the shortest beautiful substring starting at &lt;code&gt;i&lt;/code&gt; (since extending further would only increase length unnecessarily).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global comparison:&lt;/strong&gt; We compare this candidate with our best result so far—preferring shorter length first, and for equal lengths, the lexicographically smaller string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge case handling:&lt;/strong&gt; If no beautiful substring is found (less than &lt;code&gt;k&lt;/code&gt; ones total), return an empty string.&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/002904-shortest-and-lexicographically-smallest-beautiful-string" rel="noopener noreferrer"&gt;2904. Shortest and Lexicographically Smallest Beautiful String&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 String $s
 * @param Integer $k
 * @return String
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$s&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;string&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;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"100011001"&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: "11001"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"10110"&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: "110"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"000"&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: ""&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"111"&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: "111"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"01010"&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: "1"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"10101"&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: "101"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"1111"&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: "11"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;shortestBeautifulSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"1001"&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: ""&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;Sliding window concept:&lt;/strong&gt; We don't use a traditional sliding window, but rather a nested loop where the inner loop stops as soon as the condition is met, making it efficient for each start position.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why &lt;code&gt;break&lt;/code&gt; after finding &lt;code&gt;k&lt;/code&gt; ones:&lt;/strong&gt; For a fixed start &lt;code&gt;i&lt;/code&gt;, the first time we reach &lt;code&gt;k&lt;/code&gt; ones gives the shortest length possible. Any longer substring starting at &lt;code&gt;i&lt;/code&gt; would be longer and thus not needed for shortest-length consideration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lexicographical comparison:&lt;/strong&gt; PHP's string comparison (&lt;code&gt;&amp;lt;&lt;/code&gt;) works lexicographically, so we can directly compare substrings of the same length.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time complexity optimization:&lt;/strong&gt; The inner loop breaks early, and the total operations are bounded by the number of substrings considered (at most &lt;code&gt;n²&lt;/code&gt; in worst case, but with pruning).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&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; in the worst case, where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;s&lt;/code&gt;. Although we have nested loops, the inner loop terminates early when &lt;code&gt;k&lt;/code&gt; ones are found or exceeded. In a string with sparse ones, it may scan more, but with &lt;code&gt;n ≤ 100&lt;/code&gt;, this is acceptable.&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 storing the substring and the result, or &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; if we consider only the output string length.&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>3718. Smallest Missing Multiple of K</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 25 Aug 2026 18:15:39 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3718-smallest-missing-multiple-of-k-29h8</link>
      <guid>https://dev.to/mdarifulhaque/3718-smallest-missing-multiple-of-k-29h8</guid>
      <description>&lt;p&gt;3718. Smallest Missing Multiple of K&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Weekly Contest 472&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;k&lt;/code&gt;, return the &lt;strong&gt;smallest positive multiple&lt;/strong&gt; of &lt;code&gt;k&lt;/code&gt; that is &lt;strong&gt;missing&lt;/strong&gt; from &lt;code&gt;nums&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;multiple&lt;/strong&gt; of &lt;code&gt;k&lt;/code&gt; is any positive integer divisible by &lt;code&gt;k&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 = [8,2,3,4,6], k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The multiples of &lt;code&gt;k = 2&lt;/code&gt; are &lt;code&gt;2, 4, 6, 8, 10, 12...&lt;/code&gt; and the smallest multiple missing from &lt;code&gt;nums&lt;/code&gt; is 10.&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,4,7,10,15], k = 5&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; The multiples of &lt;code&gt;k = 5&lt;/code&gt; are &lt;code&gt;5, 10, 15, 20...&lt;/code&gt; and the smallest multiple missing from &lt;code&gt;nums&lt;/code&gt; is 5.&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 = [2,4,6,8,10], k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 12&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [5,10,15,20], k = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 25&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; nums = [3,6,9,12,15], k = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 18&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; nums = [1,2,3,4,5,6,7,8,9,10], k = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 11&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,4,7,10,15], k = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= k &amp;lt;= 100&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;Add the values in &lt;code&gt;nums&lt;/code&gt; to a hash set&lt;/li&gt;
&lt;li&gt;Iterate through the positive multiples of &lt;code&gt;k&lt;/code&gt; and return the first one not in the hash set&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We solve the problem by first storing all array elements in a hash set for &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; lookups. We then iterate upward through positive multiples of &lt;code&gt;k&lt;/code&gt; starting from &lt;code&gt;k&lt;/code&gt; itself, and return the first multiple that is not present in the set. This ensures the smallest missing multiple is found efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use a hash set&lt;/strong&gt; – Convert the array into a hash set (via &lt;code&gt;array_flip&lt;/code&gt;) to quickly check membership.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Start from the first positive multiple&lt;/strong&gt; – Begin checking from &lt;code&gt;k&lt;/code&gt; (the smallest positive multiple of &lt;code&gt;k&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Increment by &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt; – Move to the next multiple (&lt;code&gt;+= k&lt;/code&gt;) until we find one not in the set.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return the missing multiple&lt;/strong&gt; – The first missing multiple encountered is the answer.&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/003718-smallest-missing-multiple-of-k" rel="noopener noreferrer"&gt;3718. Smallest Missing Multiple of K&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 $k
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;missingMultiple&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;$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="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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;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;6&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: 10&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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;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;10&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;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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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="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;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: 12&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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;10&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;20&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: 25&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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;9&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;15&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: 18&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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;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: 11&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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="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: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;missingMultiple&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="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="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; &lt;code&gt;$set = array_flip($nums);&lt;/code&gt; – This creates a hash map where keys are the array values, enabling constant-time existence checks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;code&gt;$multiple = $k;&lt;/code&gt; – We start with the smallest positive multiple of &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;code&gt;while (isset($set[$multiple])) { $multiple += $k; }&lt;/code&gt; – Keep advancing to the next multiple while the current one exists in the set.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; Once the loop exits, &lt;code&gt;$multiple&lt;/code&gt; is the smallest multiple of &lt;code&gt;k&lt;/code&gt; not present in &lt;code&gt;nums&lt;/code&gt;, so we return it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n + m) where &lt;strong&gt;n&lt;/strong&gt; is the number of elements in &lt;code&gt;nums&lt;/code&gt; (to build the set) and &lt;strong&gt;m&lt;/strong&gt; is the number of multiples we check until we find a missing one. In the worst case, &lt;code&gt;m&lt;/code&gt; could be up to the largest multiple present plus &lt;code&gt;k&lt;/code&gt;, but since constraints are small (≤100), this is effectively constant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; O(n) – for the hash set storing up to &lt;code&gt;n&lt;/code&gt; unique values.&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>1872. Stone Game VIII</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 24 Aug 2026 15:29:26 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1872-stone-game-viii-2j8j</link>
      <guid>https://dev.to/mdarifulhaque/1872-stone-game-viii-2j8j</guid>
      <description>&lt;p&gt;1872. Stone Game VIII&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;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Minimax&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Game Theory&lt;/code&gt;, &lt;code&gt;Zero-Sum Game&lt;/code&gt;, &lt;code&gt;Weekly Contest 242&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Alice and Bob take turns playing a game, with &lt;strong&gt;Alice starting first&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are &lt;code&gt;n&lt;/code&gt; stones arranged in a row. On each player's turn, while the number of stones is &lt;strong&gt;more than one&lt;/strong&gt;, they will do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose an integer &lt;code&gt;x &amp;gt; 1&lt;/code&gt;, and &lt;strong&gt;remove&lt;/strong&gt; the leftmost &lt;code&gt;x&lt;/code&gt; stones from the row.&lt;/li&gt;
&lt;li&gt;Add the &lt;strong&gt;sum&lt;/strong&gt; of the &lt;strong&gt;removed&lt;/strong&gt; stones' values to the player's score.&lt;/li&gt;
&lt;li&gt;Place a &lt;strong&gt;new stone&lt;/strong&gt;, whose value is equal to that sum, on the left side of the row.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The game stops when &lt;strong&gt;only one&lt;/strong&gt; stone is left in the row.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;score difference&lt;/strong&gt; between Alice and Bob is &lt;code&gt;(Alice's score - Bob's score)&lt;/code&gt;. Alice's goal is to &lt;strong&gt;maximize&lt;/strong&gt; the score difference, and Bob's goal is to &lt;strong&gt;minimize&lt;/strong&gt; the score difference.&lt;/p&gt;

&lt;p&gt;Given an integer array &lt;code&gt;stones&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt; where &lt;code&gt;stones[i]&lt;/code&gt; represents the value of the &lt;code&gt;iᵗʰ&lt;/code&gt; stone &lt;strong&gt;from the left&lt;/strong&gt;, return &lt;em&gt;the &lt;strong&gt;score difference&lt;/strong&gt; between Alice and Bob if they both play &lt;strong&gt;optimally&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; stones = [-1,2,-3,4,-5]&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;Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of value 2 on the left. stones = [2,-5].&lt;/li&gt;
&lt;li&gt;Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on the left. stones = [-3].&lt;/li&gt;
&lt;li&gt;The difference between their scores is 2 - (-3) = 5.&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; stones = [7,-6,5,10,5,-2,-6]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 13&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a stone of value 13 on the left. stones = [13].&lt;/li&gt;
&lt;li&gt;The difference between their scores is 13 - 0 = 13.&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; stones = [-10,-12]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -22&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her score and places a stone of value -22 on the left. stones = [-22].&lt;/li&gt;
&lt;li&gt;The difference between their scores is (-22) - 0 = -22.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; stones = [1,2,3,4]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&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; stones = [-1,-2,-3,-4]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -10&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; stones = [5,0,-5,10]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; stones = [100, -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 8:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; stones = [-5, -3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -8&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; stones = [100, -50]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 50&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 == stones.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;-10⁴ &amp;lt;= stones[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;Let's note that the only thing that matters is how many stones were removed so we can maintain &lt;code&gt;dp[numberOfRemovedStones]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dp[x] = max(sum of all elements up to y - dp[y])&lt;/code&gt; for all &lt;code&gt;y &amp;gt; x&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;We present a dynamic programming solution that computes the optimal score difference in Stone Game VIII. By leveraging prefix sums and a backward &lt;code&gt;DP&lt;/code&gt; state, we efficiently determine the maximum difference Alice can guarantee, given optimal play from both players. The solution runs in &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; time and &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; space, making it feasible for n up to &lt;code&gt;1e5&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The game state after any move is completely determined by the current leftmost stone’s value, which is the prefix sum up to some index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We define &lt;code&gt;dp[i]&lt;/code&gt; as the maximum score difference the current player can achieve when the game starts with a single stone of value &lt;code&gt;prefix[i]&lt;/code&gt; (i.e., the first &lt;code&gt;i+1&lt;/code&gt; original stones have been merged).&lt;/li&gt;
&lt;li&gt;If only two stones remain (i.e., &lt;code&gt;i = n-2&lt;/code&gt;), the current player must take both, so &lt;code&gt;dp[n-2] = prefix[n-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;i &amp;lt; n-2&lt;/code&gt;, the current player can either:

&lt;ul&gt;
&lt;li&gt;Take all remaining stones immediately (score = &lt;code&gt;prefix[n-1]&lt;/code&gt;), or&lt;/li&gt;
&lt;li&gt;Take &lt;code&gt;x&lt;/code&gt; stones (where &lt;code&gt;x &amp;gt; 1&lt;/code&gt;) and leave the opponent with a state starting at &lt;code&gt;prefix[i+1]&lt;/code&gt;, with the opponent's optimal difference being &lt;code&gt;dp[i+1]&lt;/code&gt;. The current player’s difference then becomes &lt;code&gt;prefix[i+1] - dp[i+1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;We maximize over these two choices, which reduces to &lt;code&gt;dp[i] = max(dp[i+1], prefix[i+1] - dp[i+1])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We compute &lt;code&gt;dp&lt;/code&gt; from right to left and return &lt;code&gt;dp[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/001872-stone-game-viii" rel="noopener noreferrer"&gt;1872. Stone Game VIII&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[] $stones
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;stoneGameVIII&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;$stones&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;stoneGameVIII&lt;/span&gt;&lt;span class="p"&gt;([&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&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="o"&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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&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="o"&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;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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&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: 13&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&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: -22&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&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="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&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&lt;/span&gt;&lt;span class="p"&gt;([&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&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: -10&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&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;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: 10&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&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="o"&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;stoneGameVIII&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&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: -8&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stoneGameVIII&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="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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&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="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;stoneGameVIII&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;50&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: 50&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 sums&lt;/strong&gt; are precomputed so that &lt;code&gt;prefix[i]&lt;/code&gt; equals the sum of the first &lt;code&gt;i+1&lt;/code&gt; stones. This represents the value of the merged stone after removing the first &lt;code&gt;i+1&lt;/code&gt; stones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DP state definition&lt;/strong&gt; is crucial: &lt;code&gt;dp[i]&lt;/code&gt; is the best score difference for the player whose turn it is, assuming the row starts with one stone whose value is &lt;code&gt;prefix[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base case&lt;/strong&gt;: When &lt;code&gt;i = n-2&lt;/code&gt;, there are exactly two stones (the merged stone of value &lt;code&gt;prefix[n-2]&lt;/code&gt; and the last original stone &lt;code&gt;stones[n-1]&lt;/code&gt;). The only legal move is to remove both, so the difference is &lt;code&gt;prefix[n-1] - 0 = prefix[n-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recurrence&lt;/strong&gt;: For &lt;code&gt;i &amp;lt; n-2&lt;/code&gt;, the current player has two options:

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Take all&lt;/strong&gt;: remove all remaining stones, get &lt;code&gt;prefix[n-1]&lt;/code&gt; directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Take some but not all&lt;/strong&gt;: remove &lt;code&gt;x &amp;gt; 1&lt;/code&gt; stones, which corresponds to moving to state &lt;code&gt;i+1&lt;/code&gt;. The current player gains &lt;code&gt;prefix[i+1]&lt;/code&gt;, and then the opponent will get &lt;code&gt;dp[i+1]&lt;/code&gt; from the new state, so the net difference is &lt;code&gt;prefix[i+1] - dp[i+1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;dp[i+1]&lt;/code&gt; already represents optimal play from the next state, we only need to choose the maximum between &lt;code&gt;dp[i+1]&lt;/code&gt; and &lt;code&gt;prefix[i+1] - dp[i+1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We iterate &lt;strong&gt;backwards&lt;/strong&gt; from &lt;code&gt;n-3&lt;/code&gt; down to &lt;code&gt;0&lt;/code&gt; because &lt;code&gt;dp[i]&lt;/code&gt; depends only on &lt;code&gt;dp[i+1]&lt;/code&gt; and &lt;code&gt;prefix[i+1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Finally, &lt;code&gt;dp[0]&lt;/code&gt; gives the answer, as the game initially starts with a row of &lt;code&gt;n&lt;/code&gt; stones, equivalent to a single stone of value &lt;code&gt;prefix[0] = stones[0]&lt;/code&gt; after removing 0 stones – but since the first move must remove at least 2 stones, our recurrence correctly handles that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&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; – we compute prefix sums in one pass, then fill the DP array in a second pass.&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; – we store prefix sums and the &lt;code&gt;DP&lt;/code&gt; array. This can be optimized to &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; extra space by keeping only the last &lt;code&gt;DP&lt;/code&gt; value, but &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; is acceptable given 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>1927. Sum Game</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 23 Aug 2026 14:54:20 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1927-sum-game-1ldj</link>
      <guid>https://dev.to/mdarifulhaque/1927-sum-game-1ldj</guid>
      <description>&lt;p&gt;1927. Sum Game&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;Math&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Game Theory&lt;/code&gt;, &lt;code&gt;Biweekly Contest 56&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Alice and Bob take turns playing a game, with &lt;strong&gt;Alice starting first&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You are given a string &lt;code&gt;num&lt;/code&gt; of &lt;strong&gt;even length&lt;/strong&gt; consisting of digits and &lt;code&gt;'?'&lt;/code&gt; characters. On each turn, a player will do the following if there is still at least one &lt;code&gt;'?'&lt;/code&gt; in &lt;code&gt;num&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose an index &lt;code&gt;i&lt;/code&gt; where&lt;code&gt;num[i] == '?'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Replace &lt;code&gt;num[i]&lt;/code&gt; with any digit between &lt;code&gt;'0'&lt;/code&gt; and &lt;code&gt;'9'&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The game ends when there are no more &lt;code&gt;'?'&lt;/code&gt; characters in &lt;code&gt;num&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Bob to win, the sum of the digits in the first half of &lt;code&gt;num&lt;/code&gt; must be &lt;strong&gt;equal&lt;/strong&gt; to the sum of the digits in the second half. For Alice to win, the sums must &lt;strong&gt;not be equal&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, if the game ended with &lt;code&gt;num = "243801"&lt;/code&gt;, then Bob wins because &lt;code&gt;2+4+3 = 8+0+1&lt;/code&gt;. If the game ended with &lt;code&gt;num = "243803"&lt;/code&gt;, then Alice wins because &lt;code&gt;2+4+3 != 8+0+3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Assuming Alice and Bob play &lt;strong&gt;optimally&lt;/strong&gt;, return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if Alice will win and &lt;code&gt;false&lt;/code&gt; if Bob will win&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; num = "5023"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;There are no moves to be made.&lt;/li&gt;
&lt;li&gt;The sum of the first half is equal to the sum of the second half: 5 + 0 = 2 + 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; num = "25??"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Alice can replace one of the '?'s with '9' and it will be impossible for Bob to make the sums equal.&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; num = "?3295???"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;It can be proven that Bob will always win. One possible outcome is:

&lt;ul&gt;
&lt;li&gt;Alice replaces the first '?' with '9'. num = "93295???".&lt;/li&gt;
&lt;li&gt;Bob replaces one of the '?' in the right half with '9'. num = "932959??".&lt;/li&gt;
&lt;li&gt;Alice replaces one of the '?' in the right half with '2'. num = "9329592?".&lt;/li&gt;
&lt;li&gt;Bob replaces the last '?' in the right half with '7'. num = "93295927".&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num = "16?5?0592?99852?270?94852?5821?863?303??07956??562578340?36?6281545?8682?867?403158?7311994090401?361520?8?181?8?5?1912??586736786656018??4147039?37??652?13182825323368664640397951??499?747492?7160?618?61888381078443?6215195?526542088744?7???563434803?690?7312353?81190762?7818206558023797??4600?7?2?0726268?3294?84668593?93?4275?86546?046?64703?944?2608860?97887?27387??963980505594569?215?116063214573??89383032613351720611698683?85?6408880?4?1?121921229??7?00??997?7618427?90?691??945??592?5433?563???51?132?70232?11993893??9399628?9884??8238087987892442?968020?052?881788?31956843855?73?3?501796465409700142??90552?594?084687556239093350764??07551???37?46?85?0?11823??087842521862230819711211?98?8?6?343786343474249?901???71?54??397448634???96??75440209987096739?8?09998278??11?203924746???047880944?0?9??312381?29?80667?5?52?7?5850???73?3?80806235573?02?41?605749?46118???1??512????39?51230??411193?0982131497873115360408265208??35?37?2557172941?57?3??9036688122?500?164052?889649284069121?913?5??4?9?983?0?825490?4555594?54429459231994??3?984?934504846?8?1521?196??1653303?916056447?9?36?746665603?581109763292?4?9?55239747???008703538648686868740?3?262?127?667?0027??56465142?1??9524168?1809713?00??5603?818?666?99610?8783004661?6?45?6088079780?9?73929?44807?192158?56?3521396?7???58?9705336?2?6?33?7254236629272543060090974?9?5116??011???2?37?87?860?0?26?984?8168?213757??67623030?744?5827454?387204?04988?1687338660208?6493?0337684854228?94075283086641467477156??26?276376630??1?56579415963?6?98??1562??843?2?3516?99151776?5306?8181234203894?4882459?7765?6815?21??3??489480?8239125848?625?7096?622?6?4?6?265560?0005738?74?1786171851816??38079752007905890?0643124??8?6??40206?262?21?2473282183985?938?3488?51883?12??8??251?7540453?1?97291177797304474?5?33?932491??9139?01?830?785?9041?497236260938474555400?6?0150115?2170987731414??8?8?707188?77982?758154303??66?67?8?140?26?0?420?52?517?273382?5??20182?7?802?235?0042509175051542?01?7027307?0919?1141168760538?925?2795???7018?6?455815710776?160237?362198081905?757844?4626?3591?3?915?9?77389101?1?81779?39728096?0460080?5?87?80010?117??9395??12167450??3475531078328?1?25?27755167?067?5742?518733799?67588366725789?18?48?430?85907156?70?163?0364?01?9292?969?87030825?178?5??16465544?1347?461?297457?393630?277??71?0??6999?8?747??5154642642177694236??702541?036008?2?9773?98?17908498?6180?606634316?02802?130448915?01509?711?713?72321??18404??24???6362427???423??3?0????515347?911221?6?95??8??9?7909?3781544427?28????8??57?81?315?24665077513???6?51?813133?2?132?0886?79686?97612991289?303?5??646??1?63???6186156452?698?90040???08831?930389?204?2?1007?9?2205007474?976?095?92??5??17?06????5948?79?066737?2?788?3103980?582???82?2??2?4042?56766214?204?89?0?66682845161?789649?50?2764247348?036321?2245?27527930?34??79025???809502116?9540666?0561?57?0245??78008409?73?1??4??0236277?360?67094605394349650?78?2?57140960?30119?3??35?03??4486965?619570997001?7628??874079502996746151?62652120?12?44981?89846?80?21112208813233967125?9?5???312?1490983265?53?41?9?78304186230218093?20120603?1707?896555302767?7394?61684?89?3042?4871615019?7?865157?0?1?61632296?9?2509929281?138719249?756?434896??0?335011073?8?14455?02694?5181?5?15?74705689?2423?0??81??4?7512?44?09074123?91060?9193776?769?7?729?908627?56?831342875433?1638????068647?817977041271257?1??46????2179137?69799986?6118?5703?21?5906?363924?7674581?628304971828293?999?84?51?19227639363?4?67?12?2930126488061?060?7?41?69040805533?2?553??0?86?08?5480??7060645692071577493241620355960???579?6?3184095649?138542?774416430006?35120392"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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; num = "1?2"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; invalid length, not even&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; num = "????"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num = "0000"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num = "9?9?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num = "??99"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num = "123456"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num = "??????????"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&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;= num.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;num.length&lt;/code&gt; is &lt;strong&gt;even&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;num&lt;/code&gt; consists of only digits and &lt;code&gt;'?'&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;Bob can always make the total sum of both sides equal in mod 9.&lt;/li&gt;
&lt;li&gt;Why does the difference between the number of question marks on the left and right side matter?&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We analyze the game as a zero-sum optimization where each side has a sum we want to make unequal (Alice) or equal (Bob). By treating unknown digits as having an expected value of 4.5 when replaced optimally, we can reduce the game to comparing fixed sums and the number of question marks on each half.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Compute the difference between the sum of known digits in the left half and the right half.&lt;/li&gt;
&lt;li&gt;Count the number of question marks in each half.&lt;/li&gt;
&lt;li&gt;On each &lt;code&gt;turn&lt;/code&gt;, a player can choose a &lt;code&gt;'?'&lt;/code&gt; and set it to any digit &lt;code&gt;0–9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since each &lt;code&gt;'?'&lt;/code&gt; can be thought of as contributing an average of &lt;code&gt;4.5&lt;/code&gt; in optimal play, the game outcome depends on:

&lt;ul&gt;
&lt;li&gt;The known sum difference.&lt;/li&gt;
&lt;li&gt;The difference in the count of &lt;code&gt;'?'&lt;/code&gt; on left vs right side.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If the total possible adjustment range (&lt;code&gt;9 × number&lt;/code&gt; of &lt;code&gt;'?'&lt;/code&gt;) can make the sums equal, Bob can force equality; otherwise Alice can force inequality.&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/001927-sum-game" rel="noopener noreferrer"&gt;1927. Sum Game&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 String $num
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&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="cd"&gt;/**
 * @param String $c
 * @return Float
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getExpectation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;float&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;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"5023"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"25??"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"?3295???"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="nv"&gt;$num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"16?5?0592?99852?270?94852?5821?863?303??07956??562578340?36?6281545?8682?867?403158?7311994090401?361520?8?181?8?5?1912??586736786656018??4147039?37??652?13182825323368664640397951??499?747492?7160?618?61888381078443?6215195?526542088744?7???563434803?690?7312353?81190762?7818206558023797??4600?7?2?0726268?3294?84668593?93?4275?86546?046?64703?944?2608860?97887?27387??963980505594569?215?116063214573??89383032613351720611698683?85?6408880?4?1?121921229??7?00??997?7618427?90?691??945??592?5433?563???51?132?70232?11993893??9399628?9884??8238087987892442?968020?052?881788?31956843855?73?3?501796465409700142??90552?594?084687556239093350764??07551???37?46?85?0?11823??087842521862230819711211?98?8?6?343786343474249?901???71?54??397448634???96??75440209987096739?8?09998278??11?203924746???047880944?0?9??312381?29?80667?5?52?7?5850???73?3?80806235573?02?41?605749?46118???1??512????39?51230??411193?0982131497873115360408265208??35?37?2557172941?57?3??9036688122?500?164052?889649284069121?913?5??4?9?983?0?825490?4555594?54429459231994??3?984?934504846?8?1521?196??1653303?916056447?9?36?746665603?581109763292?4?9?55239747???008703538648686868740?3?262?127?667?0027??56465142?1??9524168?1809713?00??5603?818?666?99610?8783004661?6?45?6088079780?9?73929?44807?192158?56?3521396?7???58?9705336?2?6?33?7254236629272543060090974?9?5116??011???2?37?87?860?0?26?984?8168?213757??67623030?744?5827454?387204?04988?1687338660208?6493?0337684854228?94075283086641467477156??26?276376630??1?56579415963?6?98??1562??843?2?3516?99151776?5306?8181234203894?4882459?7765?6815?21??3??489480?8239125848?625?7096?622?6?4?6?265560?0005738?74?1786171851816??38079752007905890?0643124??8?6??40206?262?21?2473282183985?938?3488?51883?12??8??251?7540453?1?97291177797304474?5?33?932491??9139?01?830?785?9041?497236260938474555400?6?0150115?2170987731414??8?8?707188?77982?758154303??66?67?8?140?26?0?420?52?517?273382?5??20182?7?802?235?0042509175051542?01?7027307?0919?1141168760538?925?2795???7018?6?455815710776?160237?362198081905?757844?4626?3591?3?915?9?77389101?1?81779?39728096?0460080?5?87?80010?117??9395??12167450??3475531078328?1?25?27755167?067?5742?518733799?67588366725789?18?48?430?85907156?70?163?0364?01?9292?969?87030825?178?5??16465544?1347?461?297457?393630?277??71?0??6999?8?747??5154642642177694236??702541?036008?2?9773?98?17908498?6180?606634316?02802?130448915?01509?711?713?72321??18404??24???6362427???423??3?0????515347?911221?6?95??8??9?7909?3781544427?28????8??57?81?315?24665077513???6?51?813133?2?132?0886?79686?97612991289?303?5??646??1?63???6186156452?698?90040???08831?930389?204?2?1007?9?2205007474?976?095?92??5??17?06????5948?79?066737?2?788?3103980?582???82?2??2?4042?56766214?204?89?0?66682845161?789649?50?2764247348?036321?2245?27527930?34??79025???809502116?9540666?0561?57?0245??78008409?73?1??4??0236277?360?67094605394349650?78?2?57140960?30119?3??35?03??4486965?619570997001?7628??874079502996746151?62652120?12?44981?89846?80?21112208813233967125?9?5???312?1490983265?53?41?9?78304186230218093?20120603?1707?896555302767?7394?61684?89?3042?4871615019?7?865157?0?1?61632296?9?2509929281?138719249?756?434896??0?335011073?8?14455?02694?5181?5?15?74705689?2423?0??81??4?7512?44?09074123?91060?9193776?769?7?729?908627?56?831342875433?1638????068647?817977041271257?1??46????2179137?69799986?6118?5703?21?5906?363924?7674581?628304971828293?999?84?51?19227639363?4?67?12?2930126488061?060?7?41?69040805533?2?553??0?86?08?5480??7060645692071577493241620355960???579?6?3184095649?138542?774416430006?35120392"&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;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"1?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: invalid length, not even&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"????"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0000"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"9?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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"??99"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"123456"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;sumGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"??????????"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="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;Let &lt;code&gt;leftSum&lt;/code&gt; = sum of known digits in the left half, &lt;code&gt;rightSum&lt;/code&gt; = sum of known digits in right half.&lt;/li&gt;
&lt;li&gt;Let &lt;code&gt;leftQ&lt;/code&gt; = number of &lt;code&gt;'?'&lt;/code&gt; in left half, &lt;code&gt;rightQ&lt;/code&gt; = number of &lt;code&gt;'?'&lt;/code&gt; in right half.&lt;/li&gt;
&lt;li&gt;Define &lt;code&gt;diff = leftSum - rightSum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each &lt;code&gt;'?'&lt;/code&gt; can be replaced by a digit &lt;code&gt;0–9&lt;/code&gt;, which changes the sum difference by some value between &lt;code&gt;-9&lt;/code&gt; and &lt;code&gt;+9&lt;/code&gt; per &lt;code&gt;'?'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Bob wants to make final &lt;code&gt;diff = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The maximum possible adjustment Bob can make is by using the question marks to close the gap. Since Alice moves first and plays optimally to keep diff &lt;code&gt;≠ 0&lt;/code&gt;, the key insight:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;leftQ == rightQ&lt;/code&gt;, only the known sum diff matters.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;leftQ != rightQ&lt;/code&gt;, the player who moves last on the side with more &lt;code&gt;'?'&lt;/code&gt; can decide the outcome.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;With optimal play, the game reduces to:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;diff == 0&lt;/code&gt; and &lt;code&gt;leftQ == rightQ&lt;/code&gt;, Bob wins (false).&lt;/li&gt;
&lt;li&gt;Else if the difference between question marks is even and diff can be offset by &lt;code&gt;9 × (difference in '?') / 2&lt;/code&gt;, Bob can force equality.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;However, using the expectation method (each &lt;code&gt;'?' → 4.5&lt;/code&gt;) simplifies the check:

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;expectedDiff = leftSum - rightSum + 4.5 * (leftQ - rightQ)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;expectedDiff == 0&lt;/code&gt;, Bob can force a draw; else Alice can force inequality.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; – single pass through the string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; – only a few integer variables.&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>
