<?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>1096. Brace Expansion II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 25 Sep 2026 20:08:53 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1096-brace-expansion-ii-3eb4</link>
      <guid>https://dev.to/mdarifulhaque/1096-brace-expansion-ii-3eb4</guid>
      <description>&lt;p&gt;1096. Brace Expansion II&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Principal&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Backtracking&lt;/code&gt;, &lt;code&gt;Stack&lt;/code&gt;, &lt;code&gt;Breadth-First Search&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Weekly Contest 142&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Under the grammar given below, strings can represent a set of lowercase words. Let &lt;code&gt;R(expr)&lt;/code&gt; denote the set of words the expression represents.&lt;/p&gt;

&lt;p&gt;The grammar can best be understood through simple examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single letters represent a singleton set containing that word.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;R("a") = {"a"}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;R("w") = {"w"}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;When we take a comma-delimited list of two or more expressions, we take the union of possibilities.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;R("{a,b,c}") = {"a","b","c"}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;R("{{a,b},{b,c}}") = {"a","b","c"}&lt;/code&gt; (notice the final set only contains each word at most once)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;R("{a,b}{c,d}") = {"ac","ad","bc","bd"}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Formally, the three rules for our grammar:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For every lowercase letter &lt;code&gt;x&lt;/code&gt;, we have &lt;code&gt;R(x) = {x}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For expressions &lt;code&gt;e₁, e₂, ... , eₖ&lt;/code&gt; with k &amp;gt;= 2, we have &lt;code&gt;R({e₁, e₂, ...}) = R(e₁) ∪ R(e₂) ∪ ...&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For expressions &lt;code&gt;e₁&lt;/code&gt; and &lt;code&gt;e₂&lt;/code&gt;, we have &lt;code&gt;R(e₁ + e₂) = {a + b for (a, b) in R(e₁) × R(e₂)}&lt;/code&gt;, where &lt;code&gt;+&lt;/code&gt; denotes concatenation, and &lt;code&gt;×&lt;/code&gt; denotes the cartesian product.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given an expression representing a set of words under the given grammar, return &lt;em&gt;the sorted list of words that the expression represents&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; expression = "{a,b}{c,{d,e}}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["ac","ad","ae","bc","bd","be"]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&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; expression = "{{a,z},a{b,c},{ab,z}}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a","ab","ac","z"]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Each distinct word is written only once in the final answer.&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; expression = "a{b,c}{d,e}f{g,h}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["abdfg","abdfh","abefg","abefh","acdfg","acdfh","acefg","acefh"]&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; expression = "a"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a"]&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; expression = {a,b,c}&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a","b","c"]&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; expression = "{{a,b},{b,c}}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a","b","c"]&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; expression = "x{a,b}y"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["xay","xby"]&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; expression = "{{a,b},c{d,e}}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a","b","cd","ce"]&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; expression = "{a,b}{c,d}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["ac","ad","bc","bd"]&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; expression = "{a,b}{c,d}{e,f}"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["ace","acf","ade","adf","bce","bcf","bde","bdf"]
&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;= expression.length &amp;lt;= 60&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expression[i]&lt;/code&gt; consists of &lt;code&gt;'{'&lt;/code&gt;, &lt;code&gt;'}'&lt;/code&gt;, &lt;code&gt;','&lt;/code&gt;or lowercase English letters.&lt;/li&gt;
&lt;li&gt;The given &lt;code&gt;expression&lt;/code&gt; represents a set of words based on the grammar given in the description.&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;You can write helper methods to parse the next &lt;code&gt;"chunk"&lt;/code&gt; of the expression. If you see eg. &lt;code&gt;"a"&lt;/code&gt;, the answer is just the set &lt;code&gt;{a}&lt;/code&gt;. If you see &lt;code&gt;"{"&lt;/code&gt;, you parse until you complete the &lt;code&gt;"}"&lt;/code&gt; (the number of &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt; seen are equal) and that becomes a chunk that you find where the appropriate commas are, and parse each individual expression between the commas.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Similar Questions:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001087-brace-expansion" rel="noopener noreferrer"&gt;1087. Brace Expansion&lt;/a&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 brace expansion by recursively parsing the expression as sets of strings. Concatenation is handled with a Cartesian product, while comma-separated alternatives inside &lt;code&gt;{...}&lt;/code&gt; are merged as a union. Associative arrays are used as sets to remove duplicates immediately, and the final result is sorted.&lt;/p&gt;
&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use a recursive descent parser with a shared index &lt;code&gt;$i&lt;/code&gt; into the expression string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;parseAlternative($i)&lt;/code&gt; parses a concatenation sequence until it sees &lt;code&gt;,&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;, or the end of the string.&lt;/li&gt;
&lt;li&gt;Start each concatenation sequence with &lt;code&gt;['' =&amp;gt; true]&lt;/code&gt;, the identity for concatenation.&lt;/li&gt;
&lt;li&gt;When encountering &lt;code&gt;{&lt;/code&gt;, recursively parse each comma-separated alternative and union them into a &lt;code&gt;$termSet&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When encountering a lowercase letter, treat it as a singleton term.&lt;/li&gt;
&lt;li&gt;Concatenate the current prefix set with the parsed term set using Cartesian product.&lt;/li&gt;
&lt;li&gt;Store generated words as array keys, e.g. &lt;code&gt;$next[$left . $right] = true&lt;/code&gt;, to deduplicate.&lt;/li&gt;
&lt;li&gt;At the top level, call the parser, then &lt;code&gt;sort()&lt;/code&gt; the final unique words.&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/001096-brace-expansion-ii" rel="noopener noreferrer"&gt;1096. Brace Expansion 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="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;braceExpansionII&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;$expression&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Parse a concatenated expression until ',' or '}' or end of string.
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;parseAlternative&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="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;a,b}{c,{d,e}&lt;/span&gt;&lt;span class="si"&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: ["ac","ad","ae","bc","bd","be"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;{a,z},a{b,c},{ab,z}&lt;/span&gt;&lt;span class="si"&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: ["a","ab","ac","z"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;b,c}{d,e}f{g,h&lt;/span&gt;&lt;span class="si"&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: ["abdfg","abdfh","abefg","abefh","acdfg","acdfh","acefg","acefh"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&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: ["a"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;a,b,c&lt;/span&gt;&lt;span class="si"&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: ["a","b","c"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;{a,b},{b,c}&lt;/span&gt;&lt;span class="si"&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: ["a","b","c"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"x&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;a,b&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;y"&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: ["xay","xby"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;{a,b},c{d,e}&lt;/span&gt;&lt;span class="si"&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: ["a","b","cd","ce"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;a,b}{c,d&lt;/span&gt;&lt;span class="si"&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: ["ac","ad","bc","bd"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;braceExpansionII&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;a,b}{c,d}{e,f&lt;/span&gt;&lt;span class="si"&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: ["ace","acf","ade","adf","bce","bcf","bde","bdf"]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;The helper method stops naturally at &lt;code&gt;,&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;, which makes it suitable for parsing one alternative at a time inside braces.&lt;/li&gt;
&lt;li&gt;Nested braces work automatically because &lt;code&gt;{&lt;/code&gt; triggers another recursive call.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;$termSet&lt;/code&gt; represents the union of all alternatives inside a brace group.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;$set&lt;/code&gt; represents all possible concatenations parsed so far in the current sequence.&lt;/li&gt;
&lt;li&gt;Starting with &lt;code&gt;''&lt;/code&gt; ensures the first term is simply concatenated onto an empty prefix.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;word =&amp;gt; true&lt;/code&gt; removes duplicates during both union and concatenation.&lt;/li&gt;
&lt;li&gt;The final &lt;code&gt;array_keys()&lt;/code&gt; converts the set back into a list of words, and &lt;code&gt;sort()&lt;/code&gt; gives the required lexicographical order.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Let &lt;code&gt;n&lt;/code&gt; be the expression length and &lt;code&gt;W&lt;/code&gt; be the number of distinct output words.&lt;/li&gt;
&lt;li&gt;The output size &lt;code&gt;W&lt;/code&gt; can be exponential in &lt;code&gt;n&lt;/code&gt;, since brace expansion can generate many combinations.&lt;/li&gt;
&lt;li&gt;Time: parsing is &lt;code&gt;O(n)&lt;/code&gt;, but combination generation depends on output size. Ignoring string-copy costs, it is roughly &lt;code&gt;O(n * W)&lt;/code&gt;. Including string concatenation costs, up to &lt;code&gt;O(n² * W)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Space: &lt;code&gt;O(n * W)&lt;/code&gt; for stored intermediate/final words, plus &lt;code&gt;O(n)&lt;/code&gt; recursion depth. Including full string storage, up to &lt;code&gt;O(n² * W)&lt;/code&gt; characters.&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>3550. Smallest Index With Digit Sum Equal to Index</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 24 Sep 2026 16:48:46 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3550-smallest-index-with-digit-sum-equal-to-index-3b1i</link>
      <guid>https://dev.to/mdarifulhaque/3550-smallest-index-with-digit-sum-equal-to-index-3b1i</guid>
      <description>&lt;p&gt;3550. Smallest Index With Digit Sum Equal to Index&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 450&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;Return the &lt;strong&gt;smallest&lt;/strong&gt; index &lt;code&gt;i&lt;/code&gt; such that the sum of the digits of &lt;code&gt;nums[i]&lt;/code&gt; is equal to &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If no such index exists, return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,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; For &lt;code&gt;nums[2] = 2&lt;/code&gt;, the sum of digits is 2, which is equal to index &lt;code&gt;i = 2&lt;/code&gt;. Thus, the output is 2.&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,10,11]&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;For &lt;code&gt;nums[1] = 10&lt;/code&gt;, the sum of digits is &lt;code&gt;1 + 0 = 1&lt;/code&gt;, which is equal to index &lt;code&gt;i = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;nums[2] = 11&lt;/code&gt;, the sum of digits is &lt;code&gt;1 + 1 = 2&lt;/code&gt;, which is equal to index &lt;code&gt;i = 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since index 1 is the smallest, the output 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 = [1,2,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; Since no index satisfies the condition, the output is -1.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [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;Example 5:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [9, 10]&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 = [5, 6, 7, 8, 9, 10]&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; nums = [1000]&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;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;= 1000&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 scan the array from left to right, compute the sum of digits for each &lt;code&gt;nums[i]&lt;/code&gt;, and return the first index &lt;code&gt;i&lt;/code&gt; where that digit sum equals &lt;code&gt;i&lt;/code&gt;. If no index satisfies the condition, we return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Iterate through &lt;code&gt;nums&lt;/code&gt; using &lt;code&gt;foreach ($nums as $i =&amp;gt; $num)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each number, compute its digit sum by repeatedly taking &lt;code&gt;% 10&lt;/code&gt; and dividing by &lt;code&gt;10&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compare the computed digit sum with the current index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;i&lt;/code&gt; immediately when a match is found, because scanning left-to-right guarantees the smallest index.&lt;/li&gt;
&lt;li&gt;If the loop finishes without a match, 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/003550-smallest-index-with-digit-sum-equal-to-index" rel="noopener noreferrer"&gt;3550. Smallest Index With Digit Sum Equal to Index&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;smallestIndex&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;smallestIndex&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="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;smallestIndex&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="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;smallestIndex&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: -1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;smallestIndex&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;smallestIndex&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                      &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;smallestIndex&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="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;smallestIndex&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="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;smallestIndex&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Index &lt;code&gt;i&lt;/code&gt; starts from &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For a number like &lt;code&gt;10&lt;/code&gt;, the digit sum is &lt;code&gt;1 + 0 = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;nums[i] = 0&lt;/code&gt;, the digit sum is &lt;code&gt;0&lt;/code&gt;, which is handled correctly because the &lt;code&gt;while ($x &amp;gt; 0)&lt;/code&gt; loop simply does not run.&lt;/li&gt;
&lt;li&gt;Since we return on the first valid index, no extra comparison or storage is needed.&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;code&gt;O(n * d)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;nums&lt;/code&gt; and &lt;code&gt;d&lt;/code&gt; is the maximum number of digits in &lt;code&gt;nums[i]&lt;/code&gt;. Since &lt;code&gt;nums[i] &amp;lt;= 1000&lt;/code&gt;, &lt;code&gt;d &amp;lt;= 4&lt;/code&gt;, so this is effectively &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(1)&lt;/code&gt;, because only a few integer variables 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>1658. Minimum Operations to Reduce X to Zero</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 23 Sep 2026 15:59:06 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1658-minimum-operations-to-reduce-x-to-zero-3bhd</link>
      <guid>https://dev.to/mdarifulhaque/1658-minimum-operations-to-reduce-x-to-zero-3bhd</guid>
      <description>&lt;p&gt;1658. Minimum Operations to Reduce X to Zero&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;Binary Search&lt;/code&gt;, &lt;code&gt;Sliding Window&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Weekly Contest 215&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;x&lt;/code&gt;. In one operation, you can either remove the leftmost or the rightmost element from the array &lt;code&gt;nums&lt;/code&gt; and subtract its value from &lt;code&gt;x&lt;/code&gt;. Note that this &lt;strong&gt;modifies&lt;/strong&gt; the array for future operations.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;minimum number&lt;/strong&gt; of operations to reduce &lt;code&gt;x&lt;/code&gt; to &lt;strong&gt;exactly&lt;/strong&gt; &lt;code&gt;0&lt;/code&gt; if it is possible, otherwise, return &lt;code&gt;-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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,1,4,2,3], x = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The optimal solution is to remove the last two elements to reduce x to zero.&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 = [5,6,7,8,9], x = 4&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 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [3,2,20,1,1,3], x = 10&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 optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.&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], x = 6&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 = [1,2,3], x = 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 6:&lt;/strong&gt;&lt;/p&gt;

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

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

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

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

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

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

&lt;ol&gt;
&lt;li&gt;Think in reverse; instead of finding the minimum prefix + suffix, find the maximum subarray.&lt;/li&gt;
&lt;li&gt;Finding the maximum subarray is standard and can be done greedily.&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 by reversing the problem: removing elements from both ends with sum &lt;code&gt;x&lt;/code&gt; is equivalent to keeping one contiguous middle subarray with sum &lt;code&gt;total - x&lt;/code&gt;. So we find the longest middle subarray whose sum is &lt;code&gt;total - x&lt;/code&gt; using a sliding window. The minimum operations is then &lt;code&gt;n - maxLength&lt;/code&gt;, or &lt;code&gt;-1&lt;/code&gt; if no such subarray exists.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;total = array_sum(nums)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;target = total - x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;target &amp;lt; 0&lt;/code&gt;, return &lt;code&gt;-1&lt;/code&gt; because even removing all elements cannot reduce &lt;code&gt;x&lt;/code&gt; to zero.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;target == 0&lt;/code&gt;, return &lt;code&gt;n&lt;/code&gt; because all elements must be removed.&lt;/li&gt;
&lt;li&gt;Use a sliding window with &lt;code&gt;left&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;, and &lt;code&gt;sum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Expand &lt;code&gt;right&lt;/code&gt; and add &lt;code&gt;nums[right]&lt;/code&gt; to &lt;code&gt;sum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;While &lt;code&gt;sum &amp;gt; target&lt;/code&gt;, shrink the window from &lt;code&gt;left&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;sum == target&lt;/code&gt;, update the maximum middle subarray length.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;n - maxLength&lt;/code&gt; if &lt;code&gt;maxLength &amp;gt; 0&lt;/code&gt;; otherwise 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/001658-minimum-operations-to-reduce-x-to-zero" rel="noopener noreferrer"&gt;1658. Minimum Operations to Reduce X to Zero&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 $x
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minOperations&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="nv"&gt;$x&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;minOperations&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&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;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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;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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&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;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: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&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;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;minOperations&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: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&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="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="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;minOperations&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;15&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;minOperations&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;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: -1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minOperations&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="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;Each operation removes either the leftmost or rightmost element.&lt;/li&gt;
&lt;li&gt;After all operations, the remaining untouched elements form a contiguous subarray in the middle.&lt;/li&gt;
&lt;li&gt;The removed elements must sum to &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Therefore, the kept middle subarray must sum to &lt;code&gt;total - x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Minimizing operations means maximizing the length of this kept middle subarray.&lt;/li&gt;
&lt;li&gt;Since all &lt;code&gt;nums[i]&lt;/code&gt; are positive, a sliding window can efficiently find the longest subarray with the required sum.&lt;/li&gt;
&lt;li&gt;If no such subarray exists, it is impossible to reduce &lt;code&gt;x&lt;/code&gt; to exactly &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Time Complexity: &lt;code&gt;O(n)&lt;/code&gt; — each element is added to and removed from the sliding window at most once.&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;code&gt;O(1)&lt;/code&gt; — only a few variables 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>3535. Find X Value of Array II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 22 Sep 2026 16:04:11 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3535-find-x-value-of-array-ii-45jc</link>
      <guid>https://dev.to/mdarifulhaque/3535-find-x-value-of-array-ii-45jc</guid>
      <description>&lt;p&gt;3535. Find X Value of Array II&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Segment Tree&lt;/code&gt;, &lt;code&gt;Weekly Contest 446&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an array of &lt;strong&gt;positive&lt;/strong&gt; integers &lt;code&gt;nums&lt;/code&gt; and a &lt;strong&gt;positive&lt;/strong&gt; integer &lt;code&gt;k&lt;/code&gt;. You are also given a 2D array &lt;code&gt;queries&lt;/code&gt;, where &lt;code&gt;queries[i] = [indexᵢ, valueᵢ, startᵢ, xᵢ]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are allowed to perform an operation &lt;strong&gt;once&lt;/strong&gt; on &lt;code&gt;nums&lt;/code&gt;, where you can remove any &lt;strong&gt;suffix&lt;/strong&gt; from &lt;code&gt;nums&lt;/code&gt; such that &lt;code&gt;nums&lt;/code&gt; remains &lt;strong&gt;non-empty&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;x-value&lt;/strong&gt; of &lt;code&gt;nums&lt;/code&gt; &lt;strong&gt;for a given&lt;/strong&gt; &lt;code&gt;x&lt;/code&gt; is defined as the number of ways to perform this operation so that the &lt;strong&gt;product&lt;/strong&gt; of the remaining elements leaves a remainder of &lt;code&gt;x&lt;/code&gt; &lt;strong&gt;modulo&lt;/strong&gt; &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For each query in &lt;code&gt;queries&lt;/code&gt; you need to determine the &lt;strong&gt;x-value&lt;/strong&gt; of &lt;code&gt;nums&lt;/code&gt; for &lt;code&gt;xᵢ&lt;/code&gt; after performing the following actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update &lt;code&gt;nums[indexᵢ]&lt;/code&gt; to &lt;code&gt;valueᵢ&lt;/code&gt;. Only this step persists for the rest of the queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remove&lt;/strong&gt; the prefix &lt;code&gt;nums[0..(startᵢ - 1)]&lt;/code&gt; (where &lt;code&gt;nums[0..(-1)]&lt;/code&gt; will be used to represent the &lt;strong&gt;empty&lt;/strong&gt; prefix).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return an array &lt;code&gt;result&lt;/code&gt; of size &lt;code&gt;queries.length&lt;/code&gt; where &lt;code&gt;result[i]&lt;/code&gt; is the answer for the &lt;code&gt;iᵗʰ&lt;/code&gt; query.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;prefix&lt;/strong&gt; of an array is a &lt;strong&gt;subarray[^1]&lt;/strong&gt; that starts from the beginning of the array and extends to any point within it.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;suffix&lt;/strong&gt; of an array is a &lt;strong&gt;subarray[^1]&lt;/strong&gt; that starts at any point within the array and extends to the end of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that the prefix and suffix to be chosen for the operation can be &lt;strong&gt;empty&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that x-value has a &lt;em&gt;different&lt;/em&gt; definition in this version.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;For query 0, &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2, 2, 4, 5]&lt;/code&gt;, and the empty prefix &lt;strong&gt;must&lt;/strong&gt; be removed. The possible operations are:&lt;/li&gt;
&lt;li&gt;Remove the suffix &lt;code&gt;[2, 4, 5]&lt;/code&gt;. nums becomes &lt;code&gt;[1, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the empty suffix. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2, 2, 4, 5]&lt;/code&gt; with a product 80, which gives remainder 2 when divided by 3.&lt;/li&gt;
&lt;li&gt;For query 1, &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2, 2, 3, 5]&lt;/code&gt;, and the prefix &lt;code&gt;[1, 2, 2]&lt;/code&gt; &lt;strong&gt;must&lt;/strong&gt; be removed. The possible operations are:&lt;/li&gt;
&lt;li&gt;Remove the empty suffix. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[3, 5]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the suffix &lt;code&gt;[5]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[3]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For query 2, &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2, 2, 3, 5]&lt;/code&gt;, and the empty prefix &lt;strong&gt;must&lt;/strong&gt; be removed. The possible operations are:&lt;/li&gt;
&lt;li&gt;Remove the suffix &lt;code&gt;[2, 2, 3, 5]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the suffix &lt;code&gt;[3, 5]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,4,8,16,32], k = 4, queries = [[0,2,0,2],[0,2,0,1]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,0]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For query 0, &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[2, 2, 4, 8, 16, 32]&lt;/code&gt;. The only possible operation is:&lt;/li&gt;
&lt;li&gt;Remove the suffix &lt;code&gt;[2, 4, 8, 16, 32]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For query 1, &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[2, 2, 4, 8, 16, 32]&lt;/code&gt;. There is no possible way to perform the operation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;p&gt;&lt;strong&gt;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], k = 2, queries = [[1,2,0,0],[1,1,0,1]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,3]&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= k &amp;lt;= 5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= queries.length &amp;lt;= 2 * 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;queries[i] == [indexᵢ, valueᵢ, startᵢ, xᵢ]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= indexᵢ &amp;lt;= nums.length - 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= valueᵢ &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= startᵢ &amp;lt;= nums.length - 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= xᵢ &amp;lt;= k - 1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Use a segment tree to efficiently maintain and merge product prefix information for the array &lt;code&gt;nums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In each segment tree node, store a frequency count of prefix product remainders for every &lt;code&gt;x&lt;/code&gt; in the range [0, k - 1].&lt;/li&gt;
&lt;li&gt;For each query, update &lt;code&gt;nums[index]&lt;/code&gt; to &lt;code&gt;value&lt;/code&gt;, then merge the segments corresponding to &lt;code&gt;nums[start..n - 1]&lt;/code&gt; to compute the &lt;code&gt;x&lt;/code&gt;-value for &lt;code&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 maintain a segment tree where each node stores the product modulo &lt;code&gt;k&lt;/code&gt; of its segment and a frequency array of non-empty prefix product remainders. After each point update, a range query over &lt;code&gt;nums[start..n-1]&lt;/code&gt; merges segment nodes in order, and the answer is the frequency of remainder &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Removing prefix &lt;code&gt;nums[0..start-1]&lt;/code&gt; leaves the subarray &lt;code&gt;nums[start..n-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Removing a suffix while keeping the array non-empty is equivalent to choosing a non-empty prefix of this remaining subarray.&lt;/li&gt;
&lt;li&gt;So the task becomes: count non-empty prefixes of &lt;code&gt;nums[start..n-1]&lt;/code&gt; whose product modulo &lt;code&gt;k&lt;/code&gt; equals &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each segment-tree node stores:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prod&lt;/code&gt;: product of the whole segment modulo &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cnt[r]&lt;/code&gt;: number of non-empty prefixes of this segment whose product modulo &lt;code&gt;k&lt;/code&gt; is &lt;code&gt;r&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Merge two adjacent segments &lt;code&gt;L&lt;/code&gt; then &lt;code&gt;R&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;New product = &lt;code&gt;prodL * prodR % k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Prefixes are either from &lt;code&gt;L&lt;/code&gt;, or the whole &lt;code&gt;L&lt;/code&gt; followed by a prefix of &lt;code&gt;R&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For every remainder &lt;code&gt;y&lt;/code&gt; with &lt;code&gt;cntR[y] &amp;gt; 0&lt;/code&gt;, add it to remainder &lt;code&gt;(prodL * y) % k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Build the tree bottom-up.&lt;/li&gt;
&lt;li&gt;For each query:

&lt;ul&gt;
&lt;li&gt;Update &lt;code&gt;nums[index] = value&lt;/code&gt; in the tree and recompute ancestors.&lt;/li&gt;
&lt;li&gt;Query range &lt;code&gt;[start, n - 1]&lt;/code&gt; by merging segment nodes in left-to-right order.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;mergedCnt[x]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003525-find-x-value-of-array-ii" rel="noopener noreferrer"&gt;3535. Find X Value of Array II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="cd"&gt;/**
 * @param $prod
 * @param $cnt
 * @param $i
 * @param $k
 * @return void
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;pull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$prod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$cnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;void&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 $leftProd
 * @param $leftCnt
 * @param $rightProd
 * @param $rightCnt
 * @param $k
 * @return void
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;mergeLeft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$leftProd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$leftCnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$rightProd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$rightCnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;void&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 $nodeProd
 * @param $nodeCnt
 * @param $rightProd
 * @param $rightCnt
 * @param $k
 * @return void
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;mergeRight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$nodeProd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$nodeCnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$rightProd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$rightCnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;void&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;resultArray&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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, 2, 2]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;32&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="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: [1, 0]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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="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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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: [5]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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="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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                  &lt;span class="c1"&gt;// Output: [1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;resultArray&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="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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,3]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;The segment tree avoids recomputing prefix products from scratch for every query.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;k &amp;lt;= 5&lt;/code&gt;, each merge only loops over at most &lt;code&gt;5&lt;/code&gt; remainders, making merges very cheap.&lt;/li&gt;
&lt;li&gt;The update is persistent because the tree is updated before processing the next query.&lt;/li&gt;
&lt;li&gt;The range query preserves order using left and right accumulators:

&lt;ul&gt;
&lt;li&gt;Left accumulator collects segments from the left side.&lt;/li&gt;
&lt;li&gt;Right accumulator collects segments from the right side.&lt;/li&gt;
&lt;li&gt;Finally, they are merged in the correct order.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The provided PHP code implements exactly this logic.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Let &lt;code&gt;n = nums.length&lt;/code&gt;, &lt;code&gt;q = queries.length&lt;/code&gt;, and &lt;code&gt;k &amp;lt;= 5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Build: &lt;code&gt;O(n * k)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Each update: &lt;code&gt;O(k log n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Each query: &lt;code&gt;O(k log n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Total time: &lt;code&gt;O(k * (n + q log n))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Space: &lt;code&gt;O(k * n)&lt;/code&gt; for the segment tree frequency arrays.&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>3524. Find X Value of Array I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 21 Sep 2026 10:08:19 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3524-find-x-value-of-array-i-30fk</link>
      <guid>https://dev.to/mdarifulhaque/3524-find-x-value-of-array-i-30fk</guid>
      <description>&lt;p&gt;3524. Find X Value of Array I&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;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Weekly Contest 446&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an array of &lt;strong&gt;positive&lt;/strong&gt; integers &lt;code&gt;nums&lt;/code&gt;, and a &lt;strong&gt;positive&lt;/strong&gt; integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are allowed to perform an operation &lt;strong&gt;once&lt;/strong&gt; on &lt;code&gt;nums&lt;/code&gt;, where in each operation you can remove any &lt;strong&gt;non-overlapping&lt;/strong&gt; prefix and suffix from &lt;code&gt;nums&lt;/code&gt; such that &lt;code&gt;nums&lt;/code&gt; remains &lt;strong&gt;non-empty&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You need to find the &lt;strong&gt;x-value&lt;/strong&gt; of &lt;code&gt;nums&lt;/code&gt;, which is the number of ways to perform this operation so that the &lt;strong&gt;product&lt;/strong&gt; of the remaining elements leaves a remainder of &lt;code&gt;x&lt;/code&gt; when divided by &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return an array &lt;code&gt;result&lt;/code&gt; of size &lt;code&gt;k&lt;/code&gt; where &lt;code&gt;result[x]&lt;/code&gt; is the &lt;strong&gt;x-value&lt;/strong&gt; of &lt;code&gt;nums&lt;/code&gt; for &lt;code&gt;0 &amp;lt;= x &amp;lt;= k - 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;prefix&lt;/strong&gt; of an array is a &lt;strong&gt;subarray&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/strong&gt; that starts from the beginning of the array and extends to any point within it.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;suffix&lt;/strong&gt; of an array is a &lt;strong&gt;subarray&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/strong&gt; that starts at any point within the array and extends to the end of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that the prefix and suffix to be chosen for the operation can be &lt;strong&gt;empty&lt;/strong&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;x = 0&lt;/code&gt;, the possible operations include all possible ways to remove non-overlapping prefix/suffix that do not remove &lt;code&gt;nums[2] == 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;x = 1&lt;/code&gt;, the possible operations are:

&lt;ul&gt;
&lt;li&gt;Remove the empty prefix and the suffix &lt;code&gt;[2, 3, 4, 5]&lt;/code&gt;. nums becomes &lt;code&gt;[1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the prefix &lt;code&gt;[1, 2, 3]&lt;/code&gt; and the suffix &lt;code&gt;[5]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[4]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;x = 2&lt;/code&gt;, the possible operations are:

&lt;ul&gt;
&lt;li&gt;Remove the empty prefix and the suffix &lt;code&gt;[3, 4, 5]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the prefix &lt;code&gt;[1]&lt;/code&gt; and the suffix &lt;code&gt;[3, 4, 5]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the prefix &lt;code&gt;[1, 2, 3]&lt;/code&gt; and the empty suffix. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[4, 5]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the prefix &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt; and the empty suffix. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[5]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,4,8,16,32], k = 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [18,1,2,0]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;x = 0&lt;/code&gt;, the only operations that &lt;strong&gt;do not&lt;/strong&gt; result in &lt;code&gt;x = 0&lt;/code&gt; are:

&lt;ul&gt;
&lt;li&gt;Remove the empty prefix and the suffix &lt;code&gt;[4, 8, 16, 32]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the empty prefix and the suffix &lt;code&gt;[2, 4, 8, 16, 32]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the prefix &lt;code&gt;[1]&lt;/code&gt; and the suffix &lt;code&gt;[4, 8, 16, 32]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[2]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;x = 1&lt;/code&gt;, the only possible operation is:

&lt;ul&gt;
&lt;li&gt;Remove the empty prefix and the suffix &lt;code&gt;[2, 4, 8, 16, 32]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;x = 2&lt;/code&gt;, the possible operations are:

&lt;ul&gt;
&lt;li&gt;Remove the empty prefix and the suffix &lt;code&gt;[4, 8, 16, 32]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Remove the prefix &lt;code&gt;[1]&lt;/code&gt; and the suffix &lt;code&gt;[4, 8, 16, 32]&lt;/code&gt;. &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[2]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;x = 3&lt;/code&gt;, there is no possible way to perform the operation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;ol&gt;
&lt;li&gt;Use dynamic programming.&lt;/li&gt;
&lt;li&gt;Define &lt;code&gt;dp[i][r]&lt;/code&gt; as the count of subarrays ending at index &lt;code&gt;i&lt;/code&gt; whose product modulo &lt;code&gt;k&lt;/code&gt; equals &lt;code&gt;r&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compute &lt;code&gt;dp[i][r]&lt;/code&gt; for each index &lt;code&gt;i&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt; and sum over all indices to get the final counts for each remainder.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We treat every valid operation as choosing exactly one non-empty contiguous subarray to remain after removing a prefix and suffix. We count all such subarrays whose product modulo &lt;code&gt;k&lt;/code&gt; equals each remainder &lt;code&gt;x&lt;/code&gt;. This is done with a rolling DP over remainders modulo &lt;code&gt;k&lt;/code&gt;, since &lt;code&gt;k &amp;lt;= 5&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;A valid operation is equivalent to selecting a non-empty subarray &lt;code&gt;nums[l..r]&lt;/code&gt; to keep.&lt;/li&gt;
&lt;li&gt;So the task becomes: count all non-empty contiguous subarrays by &lt;code&gt;(product of subarray) % k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Process &lt;code&gt;nums&lt;/code&gt; from left to right.&lt;/li&gt;
&lt;li&gt;Maintain &lt;code&gt;dp[r]&lt;/code&gt;: number of subarrays ending at the previous index whose product modulo &lt;code&gt;k&lt;/code&gt; is &lt;code&gt;r&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For the current number &lt;code&gt;num&lt;/code&gt;, let &lt;code&gt;m = num % k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;New subarrays ending here are:

&lt;ul&gt;
&lt;li&gt;The single-element subarray &lt;code&gt;[num]&lt;/code&gt;, giving remainder &lt;code&gt;m&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every previous subarray extended by &lt;code&gt;num&lt;/code&gt;, changing remainder from &lt;code&gt;r&lt;/code&gt; to &lt;code&gt;(r * m) % k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Accumulate these counts into the final answer array.&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/003524-find-x-value-of-array-i" rel="noopener noreferrer"&gt;3524. Find X Value of 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[] $nums
 * @param Integer $k
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;resultArray&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;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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;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: [9,2,4]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;32&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: [18,1,2,0]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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="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="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: [9,6]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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="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,0,1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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="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="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,6,0,0,0]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;resultArray&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;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: [6]&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;Initialize &lt;code&gt;ans&lt;/code&gt; and &lt;code&gt;dp&lt;/code&gt; as arrays of size &lt;code&gt;k&lt;/code&gt; filled with &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each &lt;code&gt;num&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;m = num % k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;next&lt;/code&gt; array of size &lt;code&gt;k&lt;/code&gt; filled with &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add the single-element subarray: &lt;code&gt;next[m] += 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For every remainder &lt;code&gt;r&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;k - 1&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;dp[r] &amp;gt; 0&lt;/code&gt;, extend those subarrays:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;next[(r * m) % k] += dp[r]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Add all counts in &lt;code&gt;next&lt;/code&gt; to &lt;code&gt;ans&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;dp = next&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;ans&lt;/code&gt;, where &lt;code&gt;ans[x]&lt;/code&gt; is the number of valid operations giving remainder &lt;code&gt;x&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;code&gt;O(n * k)&lt;/code&gt;
For each element, we iterate over &lt;code&gt;k&lt;/code&gt; possible remainders. Since &lt;code&gt;k &amp;lt;= 5&lt;/code&gt;, this is effectively &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(k)&lt;/code&gt;
We only store DP and answer arrays of size &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;strong&gt;Subarray:&lt;/strong&gt; A &lt;strong&gt;subarray&lt;/strong&gt; is a contiguous sequence of elements within an array.&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>3498. Reverse Degree of a String</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 20 Sep 2026 17:36:56 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3498-reverse-degree-of-a-string-2cci</link>
      <guid>https://dev.to/mdarifulhaque/3498-reverse-degree-of-a-string-2cci</guid>
      <description>&lt;p&gt;3498. Reverse Degree of a String&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;String&lt;/code&gt;, &lt;code&gt;Simulation&lt;/code&gt;, &lt;code&gt;Biweekly Contest 153&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given a string &lt;code&gt;s&lt;/code&gt;, calculate its &lt;strong&gt;reverse degree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;reverse degree&lt;/strong&gt; is calculated as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each character, multiply its position in the reversed alphabet (&lt;code&gt;'a'&lt;/code&gt; = 26, &lt;code&gt;'b'&lt;/code&gt; = 25, ..., &lt;code&gt;'z'&lt;/code&gt; = 1) with its position in the string (&lt;strong&gt;1-indexed&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Sum these products for all characters in the string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;strong&gt;reverse degree&lt;/strong&gt; of &lt;code&gt;s&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"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 148&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Letter&lt;/th&gt;
&lt;th&gt;Index in Reversed Alphabet&lt;/th&gt;
&lt;th&gt;Index in String&lt;/th&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'b'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'c'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;72&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The reversed degree is &lt;code&gt;26 + 50 + 72 = 148&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "zaza"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 160&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Letter&lt;/th&gt;
&lt;th&gt;Index in Reversed Alphabet&lt;/th&gt;
&lt;th&gt;Index in String&lt;/th&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'z'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;52&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'z'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The reverse degree is &lt;code&gt;1 + 52 + 3 + 104 = 160&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; s = "a"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 26&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 = "z"&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; s = "az"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 28&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 = "za"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 53&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 = "aaaa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 260&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 = "zzzz"&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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; contains 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;Simulate the operations 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 iterate through &lt;code&gt;s&lt;/code&gt;, map each character to its reverse alphabet value, multiply by its 1-indexed position, and sum all products.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Initialize &lt;code&gt;sum = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Traverse &lt;code&gt;s&lt;/code&gt; using a zero-based loop index &lt;code&gt;$i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Convert &lt;code&gt;$i&lt;/code&gt; to the 1-based string position: &lt;code&gt;$i + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Convert the character to its reverse alphabet value:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ord($s[$i]) - ord('a')&lt;/code&gt; gives &lt;code&gt;0&lt;/code&gt; for &lt;code&gt;'a'&lt;/code&gt;, &lt;code&gt;25&lt;/code&gt; for &lt;code&gt;'z'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;26 - value&lt;/code&gt; gives &lt;code&gt;'a' -&amp;gt; 26&lt;/code&gt;, &lt;code&gt;'z' -&amp;gt; 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;positionInString * positionInReversedAlphabet&lt;/code&gt; to &lt;code&gt;$sum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;$sum&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/003498-reverse-degree-of-a-string" rel="noopener noreferrer"&gt;3498. Reverse Degree of a 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
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;reverseDegree&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="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;reverseDegree&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: 148&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;reverseDegree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"zaza"&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: 160&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;reverseDegree&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: 26&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;reverseDegree&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="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;reverseDegree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"az"&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: 28&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;reverseDegree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"za"&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: 53&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;reverseDegree&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: 260&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;reverseDegree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"zzzz"&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="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;code&gt;'a'&lt;/code&gt; has reversed alphabet index &lt;code&gt;26&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'z'&lt;/code&gt; has reversed alphabet index &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;"abc"&lt;/code&gt;: &lt;code&gt;(1 * 26) + (2 * 25) + (3 * 24) = 26 + 50 + 72 = 148&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;"zaza"&lt;/code&gt;: &lt;code&gt;(1 * 1) + (2 * 26) + (3 * 1) + (4 * 26) = 1 + 52 + 3 + 104 = 160&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The solution directly simulates the problem definition, so no sorting or extra mapping array is needed.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Time Complexity: &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n = strlen(s)&lt;/code&gt;, because each character is processed once.&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;code&gt;O(1)&lt;/code&gt;, because only a few integer variables 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>1401. Circle and Rectangle Overlapping</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 19 Sep 2026 12:34:31 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1401-circle-and-rectangle-overlapping-4o07</link>
      <guid>https://dev.to/mdarifulhaque/1401-circle-and-rectangle-overlapping-4o07</guid>
      <description>&lt;p&gt;1401. Circle and Rectangle Overlapping&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;Geometry&lt;/code&gt;, &lt;code&gt;Biweekly Contest 23&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a circle represented as &lt;code&gt;(radius, xCenter, yCenter)&lt;/code&gt; and an axis-aligned rectangle represented as &lt;code&gt;(x1, y1, x2, y2)&lt;/code&gt;, where &lt;code&gt;(x1, y1)&lt;/code&gt; are the coordinates of the bottom-left corner, and &lt;code&gt;(x2, y2)&lt;/code&gt; are the coordinates of the top-right corner of the rectangle.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if the circle and rectangle are overlapped otherwise return &lt;code&gt;false&lt;/code&gt;&lt;/em&gt;. In other words, check if there is any point &lt;code&gt;(xᵢ, yᵢ)&lt;/code&gt; that belongs to the circle and the rectangle at the same time.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwyglkh4ckx92l2d3v54w.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwyglkh4ckx92l2d3v54w.png" alt="sample_4_1728" width="258" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Circle and rectangle share the point (1,0).&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; radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1&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 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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Famnkfr8pj9whs01ic0dd.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Famnkfr8pj9whs01ic0dd.png" alt="sample_2_1728" width="150" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1&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; radius = 2, xCenter = 0, yCenter = 0, x1 = -1, y1 = -1, x2 = 1, y2 = 1&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; radius = 1, xCenter = 10, yCenter = 10, x1 = 0, y1 = 0, x2 = 2, y2 = 2&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 6:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; radius = 5, xCenter = 0, yCenter = 0, x1 = 3, y1 = 4, x2 = 5, y2 = 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; radius = 2, xCenter = 0, yCenter = 0, x1 = 2, y1 = -1, x2 = 4, y2 = 1&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; radius = 1, xCenter = 0, yCenter = 0, x1 = 2, y1 = 0, x2 = 3, y2 = 1&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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= radius &amp;lt;= 2000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10⁴ &amp;lt;= xCenter, yCenter &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10⁴ &amp;lt;= x1 &amp;lt; x2 &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10⁴ &amp;lt;= y1 &amp;lt; y2 &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;Locate the closest point of the square to the circle, you can then find the distance from this point to the center of the circle and check if this is less than or equal to the radius.&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 circle-rectangle overlap problem by finding the point on the axis-aligned rectangle closest to the circle’s center. If that closest point is within the circle’s radius, the two shapes overlap. This gives an &lt;code&gt;O(1)&lt;/code&gt; math/geometry solution.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Clamp the circle center’s x-coordinate to the rectangle’s x-range &lt;code&gt;[x1, x2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Clamp the circle center’s y-coordinate to the rectangle’s y-range &lt;code&gt;[y1, y2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The resulting point &lt;code&gt;(closestX, closestY)&lt;/code&gt; is the nearest point on the rectangle to the circle center.&lt;/li&gt;
&lt;li&gt;Compute the squared distance from the circle center to this closest point.&lt;/li&gt;
&lt;li&gt;If the squared distance is less than or equal to &lt;code&gt;radius²&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt;; otherwise return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001401-circle-and-rectangle-overlapping" rel="noopener noreferrer"&gt;1401. Circle and Rectangle Overlapping&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 $radius
 * @param Integer $xCenter
 * @param Integer $yCenter
 * @param Integer $x1
 * @param Integer $y1
 * @param Integer $x2
 * @param Integer $y2
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;checkOverlap&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;$radius&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;$xCenter&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;$yCenter&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;$x1&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;$y1&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;$x2&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;$y2&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;checkOverlap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="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;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: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;checkOverlap&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="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="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;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;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;checkOverlap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;checkOverlap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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;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;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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;checkOverlap&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;10&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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;checkOverlap&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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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;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;checkOverlap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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;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="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;checkOverlap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;If the circle center is inside the rectangle, the clamped point equals the center, so distance is &lt;code&gt;0&lt;/code&gt;, and the result is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the circle center is outside the rectangle, the closest point lies on an edge or corner of the rectangle.&lt;/li&gt;
&lt;li&gt;Using squared distance avoids floating-point operations and square roots.&lt;/li&gt;
&lt;li&gt;Boundary touching counts as overlap, so the comparison uses &lt;code&gt;&amp;lt;=&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This handles all cases: center inside, circle intersecting an edge, circle intersecting a corner, circle outside, and circle touching exactly.&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;code&gt;O(1)&lt;/code&gt; — only a constant number of arithmetic and comparison operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(1)&lt;/code&gt; — no extra 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>1520. Maximum Number of Non-Overlapping Substrings</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 18 Sep 2026 14:34:01 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1520-maximum-number-of-non-overlapping-substrings-3pa2</link>
      <guid>https://dev.to/mdarifulhaque/1520-maximum-number-of-non-overlapping-substrings-3pa2</guid>
      <description>&lt;p&gt;1520. Maximum Number of Non-Overlapping Substrings&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;Hash Table&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Weekly Contest 198&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given a string &lt;code&gt;s&lt;/code&gt; of lowercase letters, you need to find the maximum number of &lt;strong&gt;non-empty&lt;/strong&gt; substrings of &lt;code&gt;s&lt;/code&gt; that meet the following conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The substrings do not overlap, that is for any two substrings &lt;code&gt;s[i..j]&lt;/code&gt; and &lt;code&gt;s[x..y]&lt;/code&gt;, either &lt;code&gt;j &amp;lt; x&lt;/code&gt; or &lt;code&gt;i &amp;gt; y&lt;/code&gt; is true.&lt;/li&gt;
&lt;li&gt;A substring that contains a certain character &lt;code&gt;c&lt;/code&gt; must also contain all occurrences of &lt;code&gt;c&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Find &lt;em&gt;the maximum number of substrings that meet the above conditions. If there are multiple solutions with the same number of substrings, return the one with minimum total length&lt;/em&gt;. It can be shown that there exists a unique solution of minimum total length.&lt;/p&gt;

&lt;p&gt;Notice that you can return the substrings in &lt;strong&gt;any&lt;/strong&gt; order.&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 = "adefaddaccc"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["e","f","ccc"]&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The following are all the possible substrings that meet the conditions:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    "adefaddaccc"
    "adefadda",
    "ef",
    "e",
    "f",
    "ccc",
]
&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;If we choose the first string, we cannot choose anything else and we'd get only 1. If we choose "adefadda", we are left with "ccc" which is the only one that doesn't overlap, thus obtaining 2 substrings. Notice also, that it's not optimal to choose "ef" since it can be split into two. Therefore, the optimal way is to choose ["e","f","ccc"] which gives us 3 substrings. No other solution of the same number of substrings exist.&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 = "abbaccd"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["d","bb","cc"]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Notice that while the set of substrings ["d","abba","cc"] also has length 3, it's considered incorrect since it has larger total length.&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 = "abac"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["b","c"]&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 = "abc`"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a","b","c"]&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 = "aaaa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["aaaa"]&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 = "ababa"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["ababa"]&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 = "a"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["a"]&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;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; contains 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;Notice that it's impossible for any two valid substrings to overlap unless one is inside another.&lt;/li&gt;
&lt;li&gt;We can start by finding the starting and ending index for each character.&lt;/li&gt;
&lt;li&gt;From these indices, we can form the substrings by expanding each character's range if necessary (if another character exists in the range with smaller/larger starting/ending index).&lt;/li&gt;
&lt;li&gt;Sort the valid substrings by length and greedily take those with the smallest length, discarding the ones that overlap those we took.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We compute the minimal valid interval for each character, expand it until every character inside the interval has all its occurrences included, then greedily select non-overlapping intervals sorted by earliest ending time. Ties are broken by shorter interval length to minimize total length.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Store &lt;code&gt;first[c]&lt;/code&gt;, &lt;code&gt;last[c]&lt;/code&gt;, and all positions for every lowercase letter.&lt;/li&gt;
&lt;li&gt;For each character &lt;code&gt;c&lt;/code&gt;, start with the interval &lt;code&gt;[first[c], last[c]]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Expand the interval until stable:

&lt;ul&gt;
&lt;li&gt;If any character &lt;code&gt;k&lt;/code&gt; appears inside &lt;code&gt;[l, r]&lt;/code&gt;, merge all occurrences of &lt;code&gt;k&lt;/code&gt; into the interval.&lt;/li&gt;
&lt;li&gt;Update &lt;code&gt;l = min(l, first[k])&lt;/code&gt; and &lt;code&gt;r = max(r, last[k])&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Keep only intervals whose left endpoint equals &lt;code&gt;first[c]&lt;/code&gt;; other expanded intervals are duplicates generated by their actual leftmost character.&lt;/li&gt;
&lt;li&gt;Sort valid intervals by:

&lt;ul&gt;
&lt;li&gt;ending index ascending,&lt;/li&gt;
&lt;li&gt;starting index descending when endings are equal, which prefers shorter intervals.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Greedily choose an interval if its start is greater than the end of the last chosen interval.&lt;/li&gt;
&lt;li&gt;Return the chosen substrings using &lt;code&gt;substr&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/001520-maximum-number-of-non-overlapping-substrings" rel="noopener noreferrer"&gt;1520. Maximum Number of Non-Overlapping Substrings&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`php&lt;br&gt;
&amp;lt;?php&lt;br&gt;
/**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt; String $s&lt;/li&gt;
&lt;li&gt;@return String[]
&lt;em&gt;/
function maxNumOfSubstrings(string $s): array
{
...
...
...
/&lt;/em&gt;*

&lt;ul&gt;
&lt;li&gt;go to ./solution.php
*/
}&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;// Test cases&lt;br&gt;
echo maxNumOfSubstrings("adefaddaccc") .  "\n";     // Output: ["e","f","ccc"]&lt;br&gt;
echo maxNumOfSubstrings("abbaccd") .  "\n";         // Output: ["d","bb","cc"]&lt;br&gt;
echo maxNumOfSubstrings("abac") .  "\n";            // Output: ["b","c"]&lt;br&gt;
echo maxNumOfSubstrings("abc") .  "\n";             // Output: ["a","b","c"]&lt;br&gt;
echo maxNumOfSubstrings("aaaa") .  "\n";            // Output: ["aaaa"]&lt;br&gt;
echo maxNumOfSubstrings("ababa") .  "\n";           // Output: ["ababa"]&lt;br&gt;
echo maxNumOfSubstrings("a") .  "\n";               // Output: ["a"]&lt;br&gt;
?&amp;gt;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;A valid substring containing character &lt;code&gt;c&lt;/code&gt; must contain all occurrences of &lt;code&gt;c&lt;/code&gt;, so its minimum possible span is &lt;code&gt;[first[c], last[c]]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If that span contains another character &lt;code&gt;k&lt;/code&gt;, then validity forces all occurrences of &lt;code&gt;k&lt;/code&gt; to be included too. This may expand the interval again.&lt;/li&gt;
&lt;li&gt;Repeating this expansion gives the smallest valid substring that can start at &lt;code&gt;first[c]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The check &lt;code&gt;l === first[c]&lt;/code&gt; avoids adding the same expanded interval multiple times.&lt;/li&gt;
&lt;li&gt;Valid intervals cannot partially overlap; they are either disjoint or nested. Therefore, greedy earliest-finish selection gives the maximum number of non-overlapping substrings.&lt;/li&gt;
&lt;li&gt;For equal ending points, choosing the later starting point gives a shorter substring and does not hurt future choices.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lastEnd&lt;/code&gt; tracks the end of the last chosen substring, ensuring no overlap.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Let &lt;code&gt;n = strlen(s)&lt;/code&gt; and alphabet size &lt;code&gt;Σ = 26&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Preprocessing: &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Expansion for each character: at most &lt;code&gt;O(Σ^3 log n)&lt;/code&gt;, which is effectively constant since &lt;code&gt;Σ = 26&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sorting at most &lt;code&gt;26&lt;/code&gt; intervals: &lt;code&gt;O(Σ log Σ)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Overall time: &lt;code&gt;O(n + Σ^3 log n)&lt;/code&gt;, practically &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Space: &lt;code&gt;O(n)&lt;/code&gt; for storing character positions, plus &lt;code&gt;O(Σ)&lt;/code&gt; extra.&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>1477. Find Two Non-overlapping Sub-arrays Each With Target Sum</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 17 Sep 2026 17:46:29 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1477-find-two-non-overlapping-sub-arrays-each-with-target-sum-5a0d</link>
      <guid>https://dev.to/mdarifulhaque/1477-find-two-non-overlapping-sub-arrays-each-with-target-sum-5a0d</guid>
      <description>&lt;p&gt;1477. Find Two Non-overlapping Sub-arrays Each With Target Sum&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;Binary Search&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Sliding Window&lt;/code&gt;, &lt;code&gt;Biweekly Contest 28&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an array of integers &lt;code&gt;arr&lt;/code&gt; and an integer &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You have to find &lt;strong&gt;two non-overlapping sub-arrays&lt;/strong&gt; of &lt;code&gt;arr&lt;/code&gt; each with a sum equal &lt;code&gt;target&lt;/code&gt;. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is &lt;strong&gt;minimum&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the minimum sum of the lengths of the two required sub-arrays&lt;/em&gt;, or return &lt;em&gt;&lt;code&gt;-1&lt;/code&gt; if you cannot find such two sub-arrays&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; arr = [3,2,2,4,3], target = 3&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; Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [7,3,4,7], target = 7&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; Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [4,3,2,6,2,3,4], target = 6&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; We have only one sub-array of sum = 6.&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; arr = [1,1,1,1], target = 2&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 5:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [1,1,1], target = 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 8:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; arr = [5], target = 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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= arr.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= arr[i] &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= target &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 create two arrays &lt;code&gt;prefix&lt;/code&gt; and &lt;code&gt;suffix&lt;/code&gt; where &lt;code&gt;prefix[i]&lt;/code&gt; is the minimum length of sub-array ends before &lt;code&gt;i&lt;/code&gt; and has &lt;code&gt;sum = k&lt;/code&gt;, &lt;code&gt;suffix[i]&lt;/code&gt; is the minimum length of sub-array starting at or after &lt;code&gt;i&lt;/code&gt; and has &lt;code&gt;sum = k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The answer we are searching for is &lt;code&gt;min(prefix[i] + suffix[i])&lt;/code&gt; for all values of &lt;code&gt;i&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt; where &lt;code&gt;n == arr.length&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If you are still stuck with how to build &lt;code&gt;prefix&lt;/code&gt; and &lt;code&gt;suffix&lt;/code&gt;, you can store for each index &lt;code&gt;i&lt;/code&gt; the length of the sub-array starts at &lt;code&gt;i&lt;/code&gt; and has &lt;code&gt;sum = k&lt;/code&gt; or infinity otherwise, and you can use it to build both &lt;code&gt;prefix&lt;/code&gt; and &lt;code&gt;suffix&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 first finding all subarrays whose sum equals &lt;code&gt;target&lt;/code&gt; using a sliding window, then using prefix and suffix minimum-length arrays to combine two non-overlapping valid subarrays with the smallest total length.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Use a sliding window over the positive integer array to find all contiguous subarrays whose sum equals &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each valid subarray &lt;code&gt;[left, right]&lt;/code&gt;, record its length:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bestEnd[right] = min(bestEnd[right], length)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;bestStart[left] = min(bestStart[left], length)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Build &lt;code&gt;pref[i]&lt;/code&gt;: minimum length of a valid subarray ending at or before index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Build &lt;code&gt;suff[i]&lt;/code&gt;: minimum length of a valid subarray starting at or after index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For every split point &lt;code&gt;i&lt;/code&gt;, combine:

&lt;ul&gt;
&lt;li&gt;one subarray ending at or before &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;one subarray starting at or after &lt;code&gt;i + 1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Return the minimum &lt;code&gt;pref[i] + suff[i + 1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If no such pair exists, 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/001477-find-two-non-overlapping-sub-arrays-each-with-target-sum" rel="noopener noreferrer"&gt;1477. Find Two Non-overlapping Sub-arrays Each With Target Sum&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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;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;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: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;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: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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="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;minSumOfLengths&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="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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 4&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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;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: 4&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 4&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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;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;minSumOfLengths&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="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="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: 4&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minSumOfLengths&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="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// Output: -1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Because all elements are positive, the sliding window sum is monotonic: expanding &lt;code&gt;right&lt;/code&gt; increases the sum, and moving &lt;code&gt;left&lt;/code&gt; decreases it.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;sum == target&lt;/code&gt;, the current window is a valid subarray.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bestEnd[right]&lt;/code&gt; stores the shortest valid subarray ending exactly at &lt;code&gt;right&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bestStart[left]&lt;/code&gt; stores the shortest valid subarray starting exactly at &lt;code&gt;left&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The prefix array allows us to know the best valid subarray that finishes before or at a given position.&lt;/li&gt;
&lt;li&gt;The suffix array allows us to know the best valid subarray that begins after or at a given position.&lt;/li&gt;
&lt;li&gt;Checking every &lt;code&gt;i&lt;/code&gt; ensures the two chosen subarrays do not overlap.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;INF&lt;/code&gt; is used as a sentinel when no valid subarray exists.&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;code&gt;O(n)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Sliding window: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Prefix build: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Suffix build: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Final combination loop: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(n)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Arrays &lt;code&gt;bestEnd&lt;/code&gt;, &lt;code&gt;bestStart&lt;/code&gt;, &lt;code&gt;pref&lt;/code&gt;, and &lt;code&gt;suff&lt;/code&gt; each use &lt;code&gt;O(n)&lt;/code&gt; space.&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>1621. Number of Sets of K Non-Overlapping Line Segments</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 16 Sep 2026 17:55:40 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1621-number-of-sets-of-k-non-overlapping-line-segments-27b5</link>
      <guid>https://dev.to/mdarifulhaque/1621-number-of-sets-of-k-non-overlapping-line-segments-27b5</guid>
      <description>&lt;p&gt;1621. Number of Sets of K Non-Overlapping Line Segments&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;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Combinatorics&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Biweekly Contest 37&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given &lt;code&gt;n&lt;/code&gt; points on a 1-D plane, where the &lt;code&gt;iᵗʰ&lt;/code&gt; point (from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;) is at &lt;code&gt;x = i&lt;/code&gt;, find the number of ways we can draw &lt;strong&gt;exactly&lt;/strong&gt; &lt;code&gt;k&lt;/code&gt; &lt;strong&gt;non-overlapping&lt;/strong&gt; line segments such that each segment covers two or more points. The endpoints of each segment must have &lt;strong&gt;integral coordinates&lt;/strong&gt;. The &lt;code&gt;k&lt;/code&gt; line segments &lt;strong&gt;do not&lt;/strong&gt; have to cover all &lt;code&gt;n&lt;/code&gt; points, and they are &lt;strong&gt;allowed&lt;/strong&gt; to share endpoints.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the number of ways we can draw &lt;code&gt;k&lt;/code&gt; non-overlapping line segments. Since this number can be huge, return it &lt;strong&gt;modulo&lt;/strong&gt; &lt;code&gt;10⁹ + 7&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faz715ux5rh2mv1l4ts0o.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faz715ux5rh2mv1l4ts0o.png" alt="ex1" width="179" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 4, k = 2&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 two line segments are shown in red and blue.&lt;/li&gt;
&lt;li&gt;The image above shows the 5 different ways {(0,2),(2,3)}, {(0,1),(1,3)}, {(0,1),(2,3)}, {(1,2),(2,3)}, {(0,1),(1,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; n = 3, k = 1&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; The 3 ways are {(0,1)}, {(0,2)}, {(1,2)}.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 30, k = 7&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 796297179&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The total number of possible ways to draw 7 line segments is 3796297200. Taking this number modulo 10⁹ + 7 gives us 796297179.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 2, 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 5:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 3, 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 6:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 4, k = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 6&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; n = 1000, k = 999&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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= n &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= k &amp;lt;= n-1&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;Try to use dynamic programming where the current index and remaining number of line segments to form can describe any intermediate state.&lt;/li&gt;
&lt;li&gt;To make the computation of each state in constant time, we could add another flag to the state that indicates whether or not we are in the middle of placing a line (placed start point but no endpoint).&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We count valid sets of exactly &lt;code&gt;k&lt;/code&gt; non-overlapping line segments by reducing the problem to a single binomial coefficient:&lt;br&gt;&lt;br&gt;
&lt;code&gt;C(n + k - 1, 2k) mod 1_000_000_007&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
The given PHP solution precomputes factorials and inverse factorials, then evaluates this combination efficiently.&lt;/p&gt;
&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Represent a valid drawing as a sequence of &lt;code&gt;2k&lt;/code&gt; endpoints:
&lt;code&gt;l1 &amp;lt; r1 &amp;lt;= l2 &amp;lt; r2 &amp;lt;= ... &amp;lt;= lk &amp;lt; rk&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Convert the weak inequalities &lt;code&gt;ri &amp;lt;= li+1&lt;/code&gt; into strict inequalities by shifting alternating endpoints.&lt;/li&gt;
&lt;li&gt;This transforms the problem into choosing &lt;code&gt;2k&lt;/code&gt; strictly increasing values from &lt;code&gt;n + k - 1&lt;/code&gt; possible positions.&lt;/li&gt;
&lt;li&gt;Therefore, the answer is &lt;code&gt;C(n + k - 1, 2k)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compute the binomial coefficient modulo &lt;code&gt;10^9 + 7&lt;/code&gt; using factorials and modular inverses.&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/001621-number-of-sets-of-k-non-overlapping-line-segments" rel="noopener noreferrer"&gt;1621. Number of Sets of K Non-Overlapping Line Segments&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer $n
 * @param Integer $k
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;numberOfSets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="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="cd"&gt;/**
 * @param int $base
 * @param int $exp
 * @param int $mod
 * @return int
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;modPow&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;$base&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;$exp&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;$mod&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;numberOfSets&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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSets&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: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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: 796297179&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSets&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&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSets&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="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: 6&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Let &lt;code&gt;p1, p2, ..., p2k&lt;/code&gt; be the endpoint sequence:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;p1 = l1&lt;/code&gt;, &lt;code&gt;p2 = r1&lt;/code&gt;, &lt;code&gt;p3 = l2&lt;/code&gt;, &lt;code&gt;p4 = r2&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;The constraints become
&lt;code&gt;p1 &amp;lt; p2 &amp;lt;= p3 &amp;lt; p4 &amp;lt;= ... &amp;lt;= p2k-1 &amp;lt; p2k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Define &lt;code&gt;qj = pj + floor((j - 1) / 2)&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;Then &lt;code&gt;q1 &amp;lt; q2 &amp;lt; ... &amp;lt; q2k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The maximum possible value of &lt;code&gt;q2k&lt;/code&gt; is &lt;code&gt;n + k - 2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Thus, we need to choose &lt;code&gt;2k&lt;/code&gt; distinct values from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n + k - 2&lt;/code&gt;, inclusive.&lt;/li&gt;
&lt;li&gt;Number of choices:
&lt;code&gt;C(n + k - 1, 2k)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The code computes:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;N = n + k - 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;R = 2 * k&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;R &amp;gt; N&lt;/code&gt;, return &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Precompute &lt;code&gt;fact[0..N]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compute &lt;code&gt;invFact[N] = modPow(fact[N], MOD - 2, MOD)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Fill &lt;code&gt;invFact&lt;/code&gt; downward.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;fact[N] * invFact[R] * invFact[N - R] % MOD&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;Time Complexity: &lt;code&gt;O(n + k + log MOD)&lt;/code&gt;, dominated by factorial precomputation.&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;code&gt;O(n + k)&lt;/code&gt; for factorial and inverse factorial arrays.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;n &amp;lt;= 1000&lt;/code&gt;, this is easily within limits.&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>2472. Maximum Number of Non-overlapping Palindrome Substrings</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 15 Sep 2026 14:07:00 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2472-maximum-number-of-non-overlapping-palindrome-substrings-2ob1</link>
      <guid>https://dev.to/mdarifulhaque/2472-maximum-number-of-non-overlapping-palindrome-substrings-2ob1</guid>
      <description>&lt;p&gt;2472. Maximum Number of Non-overlapping Palindrome Substrings&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;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Weekly Contest 319&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;Select a set of &lt;strong&gt;non-overlapping&lt;/strong&gt; substrings from the string &lt;code&gt;s&lt;/code&gt; that satisfy the following conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;length&lt;/strong&gt; of each substring is &lt;strong&gt;at least&lt;/strong&gt; &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each substring is a &lt;strong&gt;palindrome&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum&lt;/strong&gt; number of substrings in an optimal selection&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;substring&lt;/strong&gt; is a contiguous sequence of characters within a 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 = "abaccdbbd", k = 3&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;We can select the substrings underlined in s = "&lt;strong&gt;aba&lt;/strong&gt;cc&lt;strong&gt;dbbd&lt;/strong&gt;". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.&lt;/li&gt;
&lt;li&gt;It can be shown that we cannot find a selection with more than two valid substrings.&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 = "adbcda", k = 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There is no palindrome substring of length at least 2 in the string.&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 = "a", 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 4:&lt;/strong&gt;&lt;/p&gt;

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

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

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "abcde", k = 1&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; s = "aabbaa", k = 2&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 11:&lt;/strong&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "abacaba", k = 3&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;= k &amp;lt;= s.length &amp;lt;= 2000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; consists of 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;Try to use dynamic programming to solve the problem.&lt;/li&gt;
&lt;li&gt;let &lt;code&gt;dp[i]&lt;/code&gt; be the answer for the prefix &lt;code&gt;s[0…i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The final answer to the problem will be &lt;code&gt;dp[n-1]&lt;/code&gt;. How do you compute this dp?&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 using &lt;strong&gt;dynamic programming&lt;/strong&gt; combined with &lt;strong&gt;palindrome precomputation&lt;/strong&gt; via the expand-around-center technique. We first identify all palindromic substrings of length at least &lt;code&gt;k&lt;/code&gt;, recording their start and end positions. Then, using DP where &lt;code&gt;dp[i]&lt;/code&gt; represents the maximum number of non-overlapping palindromes in the prefix &lt;code&gt;s[0...i]&lt;/code&gt;, we iteratively build the optimal solution by either skipping the current character or ending a valid palindrome at the current position.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Precompute palindromes:&lt;/strong&gt; Use expand-around-center for both odd and even length centers to find all palindromic substrings with length ≥ &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store palindromes by end index:&lt;/strong&gt; For each ending position &lt;code&gt;i&lt;/code&gt;, store a list of valid start positions &lt;code&gt;j&lt;/code&gt; such that &lt;code&gt;s[j...i]&lt;/code&gt; is a palindrome of length ≥ &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Programming:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Define &lt;code&gt;dp[i]&lt;/code&gt; as the maximum number of non-overlapping valid palindromes in prefix &lt;code&gt;s[0...i-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Base case: &lt;code&gt;dp[0] = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Transition:

&lt;ul&gt;
&lt;li&gt;Option 1: Skip character at &lt;code&gt;i-1&lt;/code&gt;, so &lt;code&gt;dp[i] = dp[i-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Option 2: For each palindrome ending at index &lt;code&gt;i-1&lt;/code&gt; starting at &lt;code&gt;j&lt;/code&gt;, take &lt;code&gt;dp[j] + 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Take the maximum over all options.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return &lt;code&gt;dp[n]&lt;/code&gt;&lt;/strong&gt; as the final 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/002472-maximum-number-of-non-overlapping-palindrome-substrings" rel="noopener noreferrer"&gt;2472. Maximum Number of Non-overlapping Palindrome Substrings&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 Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxPalindromes&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;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Expand around center to find all palindromes with length &amp;gt;= k
 *
 * @param string $s
 * @param int $left
 * @param int $right
 * @param int $k
 * @param array $palindromesEndingAt
 * @param int $n
 * @return void
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;expandAroundCenter&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;$left&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;$right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$palindromesEndingAt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abaccdbbd"&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: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"adbcda"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxPalindromes&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="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;maxPalindromes&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="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;maxPalindromes&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="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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ababa"&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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abccba"&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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"racecar"&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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abcde"&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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aabbaa"&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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aabbaa"&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;maxPalindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abacaba"&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: 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;Why expand-around-center?&lt;/strong&gt; It efficiently finds all palindromes in &lt;code&gt;O(n²)&lt;/code&gt; time by expanding from each possible center (odd and even).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why DP?&lt;/strong&gt; Greedy selection of palindromes doesn't always yield the optimal count because choosing a shorter palindrome early might allow more palindromes later. DP explores all valid combinations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DP state meaning:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dp[i]&lt;/code&gt; = max palindromes in first &lt;code&gt;i&lt;/code&gt; characters (&lt;code&gt;s[0...i-1]&lt;/code&gt;). &lt;/li&gt;
&lt;li&gt;This makes transitions natural: either we don't use the &lt;code&gt;i-1&lt;/code&gt;-th character, or we use a palindrome ending exactly at &lt;code&gt;i-1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-overlapping guarantee:&lt;/strong&gt; When we end a palindrome at &lt;code&gt;i-1&lt;/code&gt; starting at &lt;code&gt;j&lt;/code&gt;, we add 1 to &lt;code&gt;dp[j]&lt;/code&gt;, which only considers characters before index &lt;code&gt;j&lt;/code&gt;. Hence, no overlap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time efficiency:&lt;/strong&gt; Precomputation takes &lt;code&gt;O(n²)&lt;/code&gt;, DP takes &lt;code&gt;O(n²)&lt;/code&gt; in worst case (if many palindromes), but overall feasible for &lt;code&gt;n ≤ 2000&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;/p&gt;

&lt;ul&gt;
&lt;li&gt;Expand-around-center: &lt;code&gt;O(n²)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DP transitions: &lt;code&gt;O(n²)&lt;/code&gt; in worst case (each position can have up to &lt;code&gt;O(n)&lt;/code&gt; palindromes ending at it).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Total: &lt;code&gt;O(n²)&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;palindromesEndingAt&lt;/code&gt;: &lt;code&gt;O(n²)&lt;/code&gt; in worst case (storing all palindromes).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dp&lt;/code&gt; array: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Total: &lt;code&gt;O(n²)&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>836. Rectangle Overlap</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 14 Sep 2026 17:18:57 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/836-rectangle-overlap-4hi4</link>
      <guid>https://dev.to/mdarifulhaque/836-rectangle-overlap-4hi4</guid>
      <description>&lt;p&gt;836. Rectangle Overlap&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;Math&lt;/code&gt;, &lt;code&gt;Geometry&lt;/code&gt;, &lt;code&gt;Weekly Contest 85&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;An axis-aligned rectangle is represented as a list &lt;code&gt;[x1, y1, x2, y2]&lt;/code&gt;, where &lt;code&gt;(x1, y1)&lt;/code&gt; is the coordinate of its bottom-left corner, and &lt;code&gt;(x2, y2)&lt;/code&gt; is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.&lt;/p&gt;

&lt;p&gt;Two rectangles overlap if the area of their intersection is &lt;strong&gt;positive&lt;/strong&gt;. To be clear, two rectangles that only touch at the corner or edges do not overlap.&lt;/p&gt;

&lt;p&gt;Given two axis-aligned rectangles &lt;code&gt;rec1&lt;/code&gt; and &lt;code&gt;rec2&lt;/code&gt;, return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if they overlap, otherwise return &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; rec1 = [0,0,2,2], rec2 = [1,1,3,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 2:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; rec1 = [0,0,1,1], rec2 = [0,0,1,1]&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; rec1 = [0,0,2,2], rec2 = [1,1,1,1]&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 6:&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; rec1 = [0,0,1,2], rec2 = [1,1,2,2]&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; rec1 = [-2,-2,-1,-1], rec2 = [-1,-1,0,0]&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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rec1.length == 4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rec2.length == 4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10⁹ &amp;lt;= rec1[i], rec2[i] &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rec1&lt;/code&gt; and &lt;code&gt;rec2&lt;/code&gt; represent a valid rectangle with a non-zero area.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;We determine whether two axis-aligned rectangles overlap by checking if their projections onto both the x-axis and y-axis overlap with positive length. If both projections overlap, the rectangles intersect with positive area; otherwise, they do not overlap.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check X-axis overlap:&lt;/strong&gt; Compute the intersection of the horizontal ranges &lt;code&gt;[x1, x2]&lt;/code&gt; of both rectangles. The overlap length is positive only when &lt;code&gt;max(rec1[0], rec2[0]) &amp;lt; min(rec1[2], rec2[2])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check Y-axis overlap:&lt;/strong&gt; Compute the intersection of the vertical ranges &lt;code&gt;[y1, y2]&lt;/code&gt; of both rectangles. The overlap length is positive only when &lt;code&gt;max(rec1[1], rec2[1]) &amp;lt; min(rec1[3], rec2[3])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combine results:&lt;/strong&gt; Rectangles overlap if and only if both the x-axis and y-axis overlaps are positive. Return &lt;code&gt;true&lt;/code&gt; when both conditions hold, otherwise &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strict inequality:&lt;/strong&gt; Use &lt;code&gt;&amp;lt;&lt;/code&gt; instead of &lt;code&gt;&amp;lt;=&lt;/code&gt; so that rectangles touching only at edges or corners are not considered overlapping (intersection area must be positive).&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/000836-rectangle-overlap/" rel="noopener noreferrer"&gt;836. Rectangle Overlap&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[] $rec1
 * @param Integer[] $rec2
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;isRectangleOverlap&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;$rec1&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;$rec2&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="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;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="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="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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&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;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;0&lt;/span&gt;&lt;span class="p"&gt;],&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;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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="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="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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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="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="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="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&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;isRectangleOverlap&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;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;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;1&lt;/span&gt;&lt;span class="p"&gt;],&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Each rectangle is defined by its bottom-left &lt;code&gt;(x1, y1)&lt;/code&gt; and top-right &lt;code&gt;(x2, y2)&lt;/code&gt; corners.&lt;/li&gt;
&lt;li&gt;Projecting a rectangle onto the x-axis gives the interval &lt;code&gt;[x1, x2]&lt;/code&gt;; projecting onto the y-axis gives &lt;code&gt;[y1, y2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Two intervals overlap with positive length when the larger of the two lower bounds is strictly less than the smaller of the two upper bounds.&lt;/li&gt;
&lt;li&gt;Applying this logic independently to both axes and combining with logical AND gives the correct overlap condition.&lt;/li&gt;
&lt;li&gt;This avoids computing the actual intersection area and works in constant time.&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;code&gt;O(1)&lt;/code&gt; — only a fixed number of comparisons and arithmetic operations are performed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(1)&lt;/code&gt; — no extra 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>
  </channel>
</rss>
