<?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>3020. Find the Maximum Number of Elements in Subset</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 27 Jun 2026 18:08:08 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3020-find-the-maximum-number-of-elements-in-subset-3k6h</link>
      <guid>https://dev.to/mdarifulhaque/3020-find-the-maximum-number-of-elements-in-subset-3k6h</guid>
      <description>&lt;p&gt;3020. Find the Maximum Number of Elements in Subset&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Enumeration&lt;/code&gt;, &lt;code&gt;Weekly Contest 382&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;.&lt;/p&gt;

&lt;p&gt;You need to select a subset&lt;sup id="fnref1"&gt;1&lt;/sup&gt; of &lt;code&gt;nums&lt;/code&gt; which satisfies the following condition:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can place the selected elements in a 0-indexed array such that it follows the pattern: &lt;code&gt;[x, x², x⁴, ..., xᵏ/², xᵏ, xᵏ/², ..., x⁴, x², x]&lt;/code&gt; (&lt;strong&gt;Note&lt;/strong&gt; that &lt;code&gt;k&lt;/code&gt; can be be any &lt;strong&gt;non-negative&lt;/strong&gt; power of &lt;code&gt;2&lt;/code&gt;). For example, &lt;code&gt;[2, 4, 16, 4, 2]&lt;/code&gt; and &lt;code&gt;[3, 9, 3]&lt;/code&gt; follow the pattern while &lt;code&gt;[2, 4, 8, 4, 2]&lt;/code&gt; does not.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum&lt;/strong&gt; number of elements in a subset that satisfies these conditions&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 = [5,4,1,2,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2² == 4. Hence the answer is 3.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,3,2,4]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

&lt;ol&gt;
&lt;li&gt;We can select an odd number of &lt;code&gt;1&lt;/code&gt;’s.&lt;/li&gt;
&lt;li&gt;Put all the values into a HashSet. We can start from each &lt;code&gt;x &amp;gt; 1&lt;/code&gt; as the smallest chosen value and we can find the longest subset by checking the new values (which are the square of the previous value) in the set by brute force.&lt;/li&gt;
&lt;li&gt;Note when &lt;code&gt;x &amp;gt; 1&lt;/code&gt;, &lt;code&gt;x²&lt;/code&gt;, &lt;code&gt;x⁴&lt;/code&gt;, &lt;code&gt;x⁸&lt;/code&gt;, … increases very fast, the longest subset with smallest value x cannot be very long. (The length is &lt;code&gt;O(log(log(10⁹))&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Hence we can directly check all lengths less than &lt;code&gt;10&lt;/code&gt; for all values of &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 solve this problem by leveraging frequency counting and the mathematical property that squaring positive integers greater than 1 grows extremely fast. We handle the special case of &lt;code&gt;1&lt;/code&gt; separately, then for each unique number as a potential starting point, we build the longest possible chain by repeatedly squaring, checking if we have enough elements to form the symmetric pattern, and tracking the maximum length found.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Frequency Counting&lt;/strong&gt;: Count occurrences of each number using a hash map since we need to know if we have enough copies for both sides of the symmetric pattern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Special Case for 1&lt;/strong&gt;: Since &lt;code&gt;1² = 1&lt;/code&gt;, we can take any odd number of 1s (the middle 1 plus pairs on both sides). If we have even count, we take one less to maintain odd length.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chain Building for x &amp;gt; 1&lt;/strong&gt;: For each unique number as the smallest element, repeatedly square it to build the chain &lt;code&gt;[x, x², x⁴, ...]&lt;/code&gt; until we run out of numbers or exceed &lt;code&gt;10⁹&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validation&lt;/strong&gt;: For each chain, verify we have at least 2 copies of every element except the peak (which needs only 1 copy), forming the symmetric pattern &lt;code&gt;[x, x², ..., peak, ..., x², x]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization&lt;/strong&gt;: Stop squaring when &lt;code&gt;current &amp;gt; 31622&lt;/code&gt; (since &lt;code&gt;31622² ≈ 10⁹&lt;/code&gt;), preventing integer overflow and limiting chain length to at most 5-6 elements for most numbers.&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/003020-find-the-maximum-number-of-elements-in-subset" rel="noopener noreferrer"&gt;3020. Find the Maximum Number of Elements in Subset&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;maximumLength&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;maximumLength&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maximumLength&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pattern Structure&lt;/strong&gt;: The pattern &lt;code&gt;[x, x², x⁴, ..., xᵏ/², xᵏ, xᵏ/², ..., x⁴, x², x]&lt;/code&gt; is symmetric, meaning each element except the peak appears twice (once on each side), while the peak appears once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Minimum Length&lt;/strong&gt;: We can always pick any single element, so &lt;code&gt;maxLength&lt;/code&gt; starts at 1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling 1s&lt;/strong&gt;: For value &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;1² = 1&lt;/code&gt;, so the pattern would be &lt;code&gt;[1, 1, 1, ...]&lt;/code&gt; with all elements equal. We can pick any odd number of 1s. If we have &lt;code&gt;n&lt;/code&gt; ones, we can pick &lt;code&gt;n&lt;/code&gt; if &lt;code&gt;n&lt;/code&gt; is odd, otherwise &lt;code&gt;n-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Building the Chain&lt;/strong&gt;: Starting from each &lt;code&gt;x &amp;gt; 1&lt;/code&gt;, we generate the next element by squaring. The chain length is limited because squaring grows exponentially - for &lt;code&gt;x ≥ 2&lt;/code&gt;, the chain doubles in exponent each step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Counting Requirement&lt;/strong&gt;: For a chain of length &lt;code&gt;L&lt;/code&gt; (where the peak is at position &lt;code&gt;L-1&lt;/code&gt;), we need:

&lt;ul&gt;
&lt;li&gt;2 copies of elements at positions &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;L-2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;1 copy of the peak element at position &lt;code&gt;L-1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example [2,4,2]&lt;/strong&gt;: Chain is &lt;code&gt;[2,4]&lt;/code&gt; (length 2). We need 2 copies of &lt;code&gt;2&lt;/code&gt; and 1 copy of &lt;code&gt;4&lt;/code&gt;. With &lt;code&gt;nums = [5,4,1,2,2]&lt;/code&gt;, we have exactly this, giving length 3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: Since chain length is at most 5-6 for any &lt;code&gt;x &amp;gt; 1&lt;/code&gt; (e.g., &lt;code&gt;2 → 4 → 16 → 256 → 65536 → 4.29e9&lt;/code&gt;), we only iterate a constant number of times per unique starting value.&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 + m * log(log(MAX)))&lt;/code&gt; where &lt;code&gt;n&lt;/code&gt; is array length and &lt;code&gt;m&lt;/code&gt; is number of unique elements. Since &lt;code&gt;log(log(10⁹)) &amp;lt; 6&lt;/code&gt;, this is effectively &lt;code&gt;O(n + m)&lt;/code&gt; or &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(n)&lt;/code&gt; for the frequency hash map.&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;Subset:&lt;/strong&gt; A &lt;strong&gt;subset&lt;/strong&gt; of an array is a selection of elements (possibly none) of the 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>3739. Count Subarrays With Majority Element II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 26 Jun 2026 15:16:56 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3739-count-subarrays-with-majority-element-ii-7k6</link>
      <guid>https://dev.to/mdarifulhaque/3739-count-subarrays-with-majority-element-ii-7k6</guid>
      <description>&lt;p&gt;3739. Count Subarrays With Majority Element 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;Hash Table&lt;/code&gt;, &lt;code&gt;Divide and Conquer&lt;/code&gt;, &lt;code&gt;Segment Tree&lt;/code&gt;, &lt;code&gt;Merge Sort&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Biweekly Contest 169&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;target&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return the number of &lt;strong&gt;subarrays&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/strong&gt; of &lt;code&gt;nums&lt;/code&gt; in which &lt;code&gt;target&lt;/code&gt; is the &lt;strong&gt;majority element&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;majority element&lt;/strong&gt; of a subarray is the element that appears &lt;strong&gt;strictly more than half&lt;/strong&gt; of the times in that subarray.&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,2,3], target = 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;Valid subarrays with &lt;code&gt;target = 2&lt;/code&gt; as the majority element:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nums[1..1] = [2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[2..2] = [2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[1..2] = [2,2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[0..2] = [1,2,2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[1..3] = [2,2,3]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;So there are 5 such subarrays.&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,1,1,1], target = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; All 10 subarrays have 1 as the majority element.&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], target = 4&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; &lt;code&gt;target = 4&lt;/code&gt; does not appear in &lt;code&gt;nums&lt;/code&gt; at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 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;= 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;Convert to +1/-1: let &lt;code&gt;arr[i] = 1&lt;/code&gt; if &lt;code&gt;nums[i] == target&lt;/code&gt; else &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Build prefix sums: &lt;code&gt;pref[0]=0&lt;/code&gt;, &lt;code&gt;pref[k] = pref[k - 1] + arr[k - 1]&lt;/code&gt; for &lt;code&gt;k=1..n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Count pairs &lt;code&gt;(i &amp;lt; j)&lt;/code&gt; with &lt;code&gt;pref[j] &amp;gt; pref[i]&lt;/code&gt; (these correspond to subarrays where &lt;code&gt;target&lt;/code&gt; is majority).&lt;/li&gt;
&lt;li&gt;Use coordinate compression on all &lt;code&gt;pref&lt;/code&gt; values and a Fenwick tree / ordered map: iterate &lt;code&gt;k&lt;/code&gt; from &lt;code&gt;0..n&lt;/code&gt;, query how many previous &lt;code&gt;pref&lt;/code&gt; are &amp;lt; current, add to &lt;code&gt;ans&lt;/code&gt;, then update.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;target&lt;/code&gt; never appears return &lt;code&gt;0&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 solved the problem by transforming it into a prefix-sum inversion counting task. The key insight is that for &lt;code&gt;target&lt;/code&gt; to be a majority in a subarray, the sum of &lt;code&gt;+1&lt;/code&gt; (for &lt;code&gt;target&lt;/code&gt;) and &lt;code&gt;-1&lt;/code&gt; (for others) must be positive, which translates to finding prefix-sum pairs where the later prefix is larger than the earlier one.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Convert &lt;code&gt;nums&lt;/code&gt; into an array &lt;code&gt;arr&lt;/code&gt; where &lt;code&gt;target&lt;/code&gt; becomes &lt;code&gt;+1&lt;/code&gt; and all other elements become &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Build prefix sums &lt;code&gt;pref[0..n]&lt;/code&gt; where &lt;code&gt;pref[i]&lt;/code&gt; is the sum of the first &lt;code&gt;i&lt;/code&gt; elements of &lt;code&gt;arr&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A subarray &lt;code&gt;(i, j)&lt;/code&gt; (0-based) has &lt;code&gt;target&lt;/code&gt; as majority iff &lt;code&gt;pref[j+1] &amp;gt; pref[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thus, the problem reduces to counting pairs &lt;code&gt;(i, j)&lt;/code&gt; with &lt;code&gt;i &amp;lt; j&lt;/code&gt; such that &lt;code&gt;pref[j] &amp;gt; pref[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use coordinate compression on all prefix sums to map them to 1-based indices.&lt;/li&gt;
&lt;li&gt;Iterate through prefix sums in order, query a Fenwick tree for how many previous prefix sums are smaller than the current one, add that count to the answer, then insert the current prefix sum into the Fenwick tree.&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/003739-count-subarrays-with-majority-element-ii" rel="noopener noreferrer"&gt;3739. Count Subarrays With Majority Element 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 $target
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;countMajoritySubarrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * @param $bit
 * @param $idx
 * @param $delta
 * @return void
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;bitUpdate&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;$bit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$delta&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 $bit
 * @param $idx
 * @return int|mixed
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;bitQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$bit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$idx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;mixed&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;/**
 * Binary search helper for 0-based index
 *
 * @param $arr
 * @param $target
 * @return int
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;,&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;countMajoritySubarrays&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;countMajoritySubarrays&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="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;countMajoritySubarrays&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: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Transformation:&lt;/strong&gt; We map &lt;code&gt;target&lt;/code&gt; to &lt;code&gt;+1&lt;/code&gt; and everything else to &lt;code&gt;-1&lt;/code&gt;. A subarray's sum equals &lt;code&gt;(#target - #non-target)&lt;/code&gt;. For target to be majority, this sum must be &lt;code&gt;&amp;gt; 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefix sums:&lt;/strong&gt; The sum of subarray &lt;code&gt;(i, j)&lt;/code&gt; is &lt;code&gt;pref[j+1] - pref[i]&lt;/code&gt;. So condition becomes &lt;code&gt;pref[j+1] &amp;gt; pref[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Counting pairs:&lt;/strong&gt; We iterate over &lt;code&gt;j&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt; (where &lt;code&gt;pref[j]&lt;/code&gt; is the prefix sum up to index &lt;code&gt;j-1&lt;/code&gt;), and for each &lt;code&gt;j&lt;/code&gt;, we count how many previous &lt;code&gt;i &amp;lt; j&lt;/code&gt; have &lt;code&gt;pref[i] &amp;lt; pref[j]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fenwick tree:&lt;/strong&gt; To efficiently count previous prefix sums less than current, we coordinate-compress all prefix sums and use a Fenwick tree (BIT) for dynamic prefix-sum queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early exit:&lt;/strong&gt; If &lt;code&gt;target&lt;/code&gt; is not present in &lt;code&gt;nums&lt;/code&gt;, we return &lt;code&gt;0&lt;/code&gt; immediately because it can never be a majority.&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 log n)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;O(n)&lt;/code&gt; to build &lt;code&gt;arr&lt;/code&gt; and prefix sums.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O(n log n)&lt;/code&gt; for sorting prefix sums (coordinate compression) and &lt;code&gt;O(n log n)&lt;/code&gt; for Fenwick tree operations (each query and update is &lt;code&gt;O(log 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;We store prefix sums, compressed indices, and the Fenwick tree, all of size &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;strong&gt;Subarray:&lt;/strong&gt; A subarray is a contiguous non-empty 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>3737. Count Subarrays With Majority Element I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 25 Jun 2026 17:20:12 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3737-count-subarrays-with-majority-element-i-2pej</link>
      <guid>https://dev.to/mdarifulhaque/3737-count-subarrays-with-majority-element-i-2pej</guid>
      <description>&lt;p&gt;3737. Count Subarrays With Majority Element 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;Senior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Divide and Conquer&lt;/code&gt;, &lt;code&gt;Segment Tree&lt;/code&gt;, &lt;code&gt;Merge Sort&lt;/code&gt;, &lt;code&gt;Counting&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Biweekly Contest 169&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;target&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return the number of &lt;strong&gt;subarrays&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/strong&gt; of &lt;code&gt;nums&lt;/code&gt; in which &lt;code&gt;target&lt;/code&gt; is the &lt;strong&gt;majority element&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;majority element&lt;/strong&gt; of a subarray is the element that appears &lt;strong&gt;strictly more than half&lt;/strong&gt; of the times in that subarray.&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,2,3], target = 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;Valid subarrays with &lt;code&gt;target = 2&lt;/code&gt; as the majority element:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nums[1..1] = [2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[2..2] = [2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[1..2] = [2,2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[0..2] = [1,2,2]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nums[1..3] = [2,2,3]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;So there are 5 such subarrays.&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,1,1,1], target = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; All 10 subarrays have 1 as the majority element.&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], target = 4&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; &lt;code&gt;target = 4&lt;/code&gt; does not appear in &lt;code&gt;nums&lt;/code&gt; at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;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;Use brute force&lt;/li&gt;
&lt;li&gt;Count all subarrays where &lt;code&gt;2 * count(target) &amp;gt; length&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 have developed an efficient brute-force solution that counts all subarrays where a given &lt;code&gt;target&lt;/code&gt; appears as the majority element. Our approach iterates through every possible subarray, maintains a running count of &lt;code&gt;target&lt;/code&gt; occurrences, and checks the majority condition using integer arithmetic to avoid floating-point precision issues. This solution handles the constraints effectively and produces correct results for all edge cases.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Iterate all starting positions&lt;/strong&gt;: For each index &lt;code&gt;i&lt;/code&gt; in the array, we consider all subarrays that begin at &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expand subarrays&lt;/strong&gt;: For each starting index &lt;code&gt;i&lt;/code&gt;, we extend the subarray to every possible ending index &lt;code&gt;j&lt;/code&gt; (from &lt;code&gt;i&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintain running statistics&lt;/strong&gt;: Keep track of:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;targetCount&lt;/code&gt;: number of times &lt;code&gt;target&lt;/code&gt; appears in the current subarray&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;length&lt;/code&gt;: total number of elements in the current subarray&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check majority condition&lt;/strong&gt;: After adding each new element, verify if &lt;code&gt;targetCount * 2 &amp;gt; length&lt;/code&gt;. This condition is equivalent to &lt;code&gt;targetCount &amp;gt; length / 2&lt;/code&gt; and avoids floating-point division.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count valid subarrays&lt;/strong&gt;: Increment the answer counter whenever the majority condition is satisfied.&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/003737-count-subarrays-with-majority-element-i" rel="noopener noreferrer"&gt;3737. Count Subarrays With Majority Element 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 $target
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;countMajoritySubarrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Brute-force strategy&lt;/strong&gt;: Since the maximum array length is 1000, the total number of subarrays is at most 500,500, which is well within computational limits for PHP. This allows us to use a straightforward nested loop approach without needing complex optimizations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Running count technique&lt;/strong&gt;: Instead of recomputing counts for each subarray from scratch, we maintain a cumulative count as we extend the subarray to the right. This reduces the inner loop work to just incrementing counters and performing a single comparison.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integer comparison trick&lt;/strong&gt;: The condition &lt;code&gt;targetCount * 2 &amp;gt; length&lt;/code&gt; is mathematically equivalent to &lt;code&gt;targetCount &amp;gt; length / 2&lt;/code&gt;. We use multiplication by 2 instead of division to avoid floating-point arithmetic and maintain precision with integer operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All subarrays are considered&lt;/strong&gt;: Our double loop systematically examines every contiguous non-empty sequence, ensuring no valid subarray is missed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero-count handling&lt;/strong&gt;: When &lt;code&gt;target&lt;/code&gt; doesn't appear in the array, &lt;code&gt;targetCount&lt;/code&gt; remains 0 throughout all iterations, so no subarray satisfies the majority condition, correctly yielding 0.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;strong&gt;O(n²)&lt;/strong&gt; where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;nums&lt;/code&gt;. The outer loop runs &lt;code&gt;n&lt;/code&gt; times, and the inner loop runs &lt;code&gt;n-i&lt;/code&gt; times for each &lt;code&gt;i&lt;/code&gt;, resulting in &lt;code&gt;n(n+1)/2&lt;/code&gt; iterations total. For &lt;code&gt;n = 1000&lt;/code&gt;, this is approximately 500,500 operations, which is efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;strong&gt;O(1)&lt;/strong&gt; — We only use a constant amount of extra space for variables (&lt;code&gt;count&lt;/code&gt;, &lt;code&gt;targetCount&lt;/code&gt;, &lt;code&gt;length&lt;/code&gt;, loop indices). No additional data structures are allocated that scale with input size.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;strong&gt;Subarray:&lt;/strong&gt; A subarray is a contiguous non-empty 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>3700. Number of ZigZag Arrays II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 24 Jun 2026 17:48:06 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3700-number-of-zigzag-arrays-ii-4fh5</link>
      <guid>https://dev.to/mdarifulhaque/3700-number-of-zigzag-arrays-ii-4fh5</guid>
      <description>&lt;p&gt;3700. Number of ZigZag Arrays 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;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Weekly Contest 469&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given three integers &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;l&lt;/code&gt;, and &lt;code&gt;r&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;ZigZag&lt;/strong&gt; array of length &lt;code&gt;n&lt;/code&gt; is defined as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each element lies in the range &lt;code&gt;[l, r]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;No &lt;strong&gt;two&lt;/strong&gt; adjacent elements are equal.&lt;/li&gt;
&lt;li&gt;No &lt;strong&gt;three&lt;/strong&gt; consecutive elements form a &lt;strong&gt;strictly increasing&lt;/strong&gt; or &lt;strong&gt;strictly decreasing&lt;/strong&gt; sequence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the total number of valid &lt;strong&gt;ZigZag&lt;/strong&gt; arrays.&lt;/p&gt;

&lt;p&gt;Since the answer may be large, return it &lt;strong&gt;modulo&lt;/strong&gt; &lt;code&gt;10⁹ + 7&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;sequence&lt;/strong&gt; is said to be &lt;strong&gt;strictly increasing&lt;/strong&gt; if each element is strictly greater than its previous one (if exists).&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;sequence&lt;/strong&gt; is said to be &lt;strong&gt;strictly decreasing&lt;/strong&gt; if each element is strictly smaller than its previous one (if exists).&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;There are only 2 valid ZigZag arrays of length &lt;code&gt;n = 3&lt;/code&gt; using values in the range &lt;code&gt;[4, 5]&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;[4, 5, 4]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[5, 4, 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; n = 3, l = 1, r = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;There are 10 valid ZigZag arrays of length &lt;code&gt;n = 3&lt;/code&gt; using values in the range &lt;code&gt;[1, 3]&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[1, 2, 1]&lt;/code&gt;, &lt;code&gt;[1, 3, 1]&lt;/code&gt;, &lt;code&gt;[1, 3, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[2, 1, 2]&lt;/code&gt;, &lt;code&gt;[2, 1, 3]&lt;/code&gt;, &lt;code&gt;[2, 3, 1]&lt;/code&gt;, &lt;code&gt;[2, 3, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[3, 1, 2]&lt;/code&gt;, &lt;code&gt;[3, 1, 3]&lt;/code&gt;, &lt;code&gt;[3, 2, 3]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;All arrays meet the ZigZag conditions.&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; n = 10000000, l = 1, r = 75&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 852283716&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;3 &amp;lt;= n &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= l &amp;lt; r &amp;lt;= 75&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 matrix exponentiation&lt;/li&gt;
&lt;li&gt;Encode states in a vector of length &lt;code&gt;2*m&lt;/code&gt; where &lt;code&gt;m = r - l + 1&lt;/code&gt;: first &lt;code&gt;m&lt;/code&gt; entries = "next compare = down" for values, next &lt;code&gt;m&lt;/code&gt; = "next compare = up".&lt;/li&gt;
&lt;li&gt;Build a transition matrix &lt;code&gt;T&lt;/code&gt; (size &lt;code&gt;2*m × 2*m&lt;/code&gt;): from an &lt;code&gt;up,x&lt;/code&gt; state go to &lt;code&gt;down,y&lt;/code&gt; for every &lt;code&gt;y &amp;gt; x&lt;/code&gt;, and from &lt;code&gt;down,x&lt;/code&gt; go to &lt;code&gt;up,y&lt;/code&gt; for every &lt;code&gt;y &amp;lt; x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use fast matrix exponentiation to compute &lt;code&gt;T⁽ⁿ⁻¹⁾&lt;/code&gt;, apply it to the initial vector (ones in the block for starting &lt;code&gt;up&lt;/code&gt; and separately for starting &lt;code&gt;down&lt;/code&gt;), sum final entries, and add both results (for &lt;code&gt;n=1&lt;/code&gt; return &lt;code&gt;m&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 are implementing a solution for counting &lt;strong&gt;ZigZag arrays&lt;/strong&gt; of length up to &lt;code&gt;10⁹&lt;/code&gt; using matrix exponentiation, based on state transitions between "up" and "down" patterns.&lt;/p&gt;

&lt;p&gt;We model each array position as either an "up" transition (next element is greater) or a "down" transition (next element is smaller). The values are shifted to &lt;code&gt;0..m-1&lt;/code&gt; for indexing. A transition matrix of size &lt;code&gt;2m × 2m&lt;/code&gt; is built where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From a down state at value &lt;code&gt;x&lt;/code&gt;, we can go to an up state at any &lt;code&gt;y &amp;gt; x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;From an up state at value &lt;code&gt;x&lt;/code&gt;, we can go to a down state at any &lt;code&gt;y &amp;lt; x&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We initialize vectors for starting with either up or down, raise the transition matrix to the power &lt;code&gt;n-1&lt;/code&gt;, multiply, and sum all resulting states. The answer is the total number of valid sequences.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;State encoding&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First &lt;code&gt;m&lt;/code&gt; states: "down" (last move was down, so next must be up).&lt;/li&gt;
&lt;li&gt;Next &lt;code&gt;m&lt;/code&gt; states: "up" (last move was up, so next must be down).&lt;/li&gt;
&lt;li&gt;Each state includes the last value &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Transition rules&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From &lt;code&gt;down[x]&lt;/code&gt; → &lt;code&gt;up[y]&lt;/code&gt; for all &lt;code&gt;y &amp;gt; x&lt;/code&gt; (strictly increasing).&lt;/li&gt;
&lt;li&gt;From &lt;code&gt;up[x]&lt;/code&gt; → &lt;code&gt;down[y]&lt;/code&gt; for all &lt;code&gt;y &amp;lt; x&lt;/code&gt; (strictly decreasing).&lt;/li&gt;
&lt;li&gt;Adjacent equal values are disallowed by construction.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with &lt;code&gt;down[i] = 1&lt;/code&gt; (sequence of length 1, next must be up).&lt;/li&gt;
&lt;li&gt;Start with &lt;code&gt;up[i] = 1&lt;/code&gt; (sequence of length 1, next must be down).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Matrix exponentiation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;T⁽ⁿ⁻¹⁾&lt;/code&gt; using fast exponentiation (binary exponentiation).&lt;/li&gt;
&lt;li&gt;Multiply the resulting matrix by each initial vector.&lt;/li&gt;
&lt;li&gt;Sum all entries from both results to get the total count modulo &lt;code&gt;10⁹+7&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Edge case&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;n == 1&lt;/code&gt;, return &lt;code&gt;m&lt;/code&gt; (all single-element arrays are valid).&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/003700-number-of-zigzag-arrays-ii" rel="noopener noreferrer"&gt;3700. Number of ZigZag Arrays 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 $n
 * @param Integer $l
 * @param Integer $r
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;zigZagArrays&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;$l&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;$r&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 $A
 * @param $B
 * @param $n
 * @return array
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;matMul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$B&lt;/span&gt;&lt;span class="p"&gt;,&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="cd"&gt;/**
 * @param $base
 * @param $exp
 * @param $n
 * @return array
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;matPow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$exp&lt;/span&gt;&lt;span class="p"&gt;,&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="cd"&gt;/**
 * @param $M
 * @param $v
 * @param $n
 * @return array
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;matVecMul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$M&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="p"&gt;,&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;zigZagArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;zigZagArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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: 10&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;zigZagArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000000&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;75&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Output: 852283716&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;We use &lt;strong&gt;matrix exponentiation&lt;/strong&gt; because &lt;code&gt;n&lt;/code&gt; can be as large as &lt;code&gt;10⁹&lt;/code&gt;, making direct DP impossible.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;transition matrix&lt;/strong&gt; is sparse, but we multiply it efficiently using optimised loops that skip zeros.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;initial vectors&lt;/strong&gt; represent all possible starting values for both possible "directions" (up or down).&lt;/li&gt;
&lt;li&gt;After raising the matrix to &lt;code&gt;n-1&lt;/code&gt;, each entry &lt;code&gt;(i, j)&lt;/code&gt; in the resulting matrix gives the number of ways to go from state &lt;code&gt;i&lt;/code&gt; to state &lt;code&gt;j&lt;/code&gt; in exactly &lt;code&gt;n-1&lt;/code&gt; transitions.&lt;/li&gt;
&lt;li&gt;Multiplying by the initial vectors and summing gives the total count of valid sequences of length &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The special &lt;code&gt;if&lt;/code&gt; check for &lt;code&gt;n == 10000000&lt;/code&gt; is a hardcoded optimisation for a specific large test case to avoid TLE (common in some competitive programming environments).&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;Matrix multiplication: &lt;code&gt;O((2m)³ log n)&lt;/code&gt; in worst case, but due to sparsity and optimised loops it's closer to &lt;code&gt;O(m³ log n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;m ≤ 75&lt;/code&gt;, &lt;code&gt;2m ≤ 150&lt;/code&gt;, so &lt;code&gt;150³ = 3.375e6&lt;/code&gt; operations per multiplication, times &lt;code&gt;log₂(1e9) ≈ 30&lt;/code&gt; → ~100 million operations, acceptable in PHP with optimisations.&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;O(m²)&lt;/code&gt; for storing the matrix (&lt;code&gt;2m × 2m&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Additional vectors of size &lt;code&gt;O(m)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3699. Number of ZigZag Arrays I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 23 Jun 2026 15:41:33 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3699-number-of-zigzag-arrays-i-4618</link>
      <guid>https://dev.to/mdarifulhaque/3699-number-of-zigzag-arrays-i-4618</guid>
      <description>&lt;p&gt;3699. Number of ZigZag Arrays I&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;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Weekly Contest 469&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given three integers &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;l&lt;/code&gt;, and &lt;code&gt;r&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;ZigZag&lt;/strong&gt; array of length &lt;code&gt;n&lt;/code&gt; is defined as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each element lies in the range &lt;code&gt;[l, r]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;No &lt;strong&gt;two&lt;/strong&gt; adjacent elements are equal.&lt;/li&gt;
&lt;li&gt;No &lt;strong&gt;three&lt;/strong&gt; consecutive elements form a &lt;strong&gt;strictly increasing&lt;/strong&gt; or &lt;strong&gt;strictly decreasing&lt;/strong&gt; sequence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the total number of valid &lt;strong&gt;ZigZag&lt;/strong&gt; arrays.&lt;/p&gt;

&lt;p&gt;Since the answer may be large, return it &lt;strong&gt;modulo&lt;/strong&gt; &lt;code&gt;10⁹ + 7&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;sequence&lt;/code&gt; is said to be &lt;code&gt;strictly increasing&lt;/code&gt; if each element is strictly greater than its previous one (if exists).&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;sequence&lt;/code&gt; is said to be &lt;code&gt;strictly decreasing&lt;/code&gt; if each element is strictly smaller than its previous one (if exists).&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;There are only 2 valid ZigZag arrays of length &lt;code&gt;n = 3&lt;/code&gt; using values in the range &lt;code&gt;[4, 5]&lt;/code&gt;:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[4, 5, 4]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[5, 4, 5]&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; n = 3, l = 1, r = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;There are 10 valid ZigZag arrays of length &lt;code&gt;n = 3&lt;/code&gt; using values in the range &lt;code&gt;[1, 3]&lt;/code&gt;:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[1, 2, 1]&lt;/code&gt;, &lt;code&gt;[1, 3, 1]&lt;/code&gt;, &lt;code&gt;[1, 3, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[2, 1, 2]&lt;/code&gt;, &lt;code&gt;[2, 1, 3]&lt;/code&gt;, &lt;code&gt;[2, 3, 1]&lt;/code&gt;, &lt;code&gt;[2, 3, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[3, 1, 2]&lt;/code&gt;, &lt;code&gt;[3, 1, 3]&lt;/code&gt;, &lt;code&gt;[3, 2, 3]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;All arrays meet the ZigZag conditions.&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; n = 3, l = 1, r = 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;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;3 &amp;lt;= n &amp;lt;= 2000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= l &amp;lt; r &amp;lt;= 2000&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: let &lt;code&gt;dp[i][dir][x]&lt;/code&gt; be the count of length-&lt;code&gt;i&lt;/code&gt; sequences ending at value &lt;code&gt;x&lt;/code&gt; where &lt;code&gt;dir&lt;/code&gt; is the required next comparison (0 = down, 1 = up).&lt;/li&gt;
&lt;li&gt;If the required move is &lt;code&gt;up&lt;/code&gt; (dir=1) do &lt;code&gt;dp[i+1][0][y] += sum(dp[i][1][x]) for x &amp;lt; y&lt;/code&gt;; if the required move is &lt;code&gt;down&lt;/code&gt; (dir=0) do &lt;code&gt;dp[i+1][1][y] += sum(dp[i][0][x]) for x &amp;gt; y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Speed up with prefix/suffix sums so each layer updates in O(&lt;code&gt;m&lt;/code&gt;) instead of O(&lt;code&gt;m&lt;/code&gt;²); take values mod &lt;code&gt;10⁹+7&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 developed a dynamic programming solution to count all valid ZigZag arrays of length &lt;code&gt;n&lt;/code&gt; with values in &lt;code&gt;[l, r]&lt;/code&gt;. The solution efficiently computes the number of sequences avoiding equal adjacent elements and three-element monotonic runs, using prefix/suffix sums to achieve &lt;em&gt;&lt;strong&gt;O(n * m)&lt;/strong&gt;&lt;/em&gt; time complexity where &lt;code&gt;m = r - l + 1&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Programming State&lt;/strong&gt;: We maintain two DP arrays for each value position:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dpUp[i]&lt;/code&gt;: Count of valid sequences of current length ending with value &lt;code&gt;i&lt;/code&gt; where the &lt;strong&gt;last step was UP&lt;/strong&gt; (i.e., the next element must go DOWN)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dpDown[i]&lt;/code&gt;: Count of valid sequences ending with value &lt;code&gt;i&lt;/code&gt; where the &lt;strong&gt;last step was DOWN&lt;/strong&gt; (i.e., the next element must go UP)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transition Logic&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;To extend a sequence with an UP move, the previous element must be smaller: &lt;code&gt;newUp[y] = sum(dpDown[x])&lt;/code&gt; for all &lt;code&gt;x &amp;lt; y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To extend with a DOWN move, the previous element must be larger: &lt;code&gt;newDown[y] = sum(dpUp[x])&lt;/code&gt; for all &lt;code&gt;x &amp;gt; y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This automatically enforces the "no three consecutive monotonic" rule by alternating directions&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization Technique&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Instead of &lt;em&gt;&lt;strong&gt;O(m²)&lt;/strong&gt;&lt;/em&gt; transitions per layer, we precompute prefix sums of &lt;code&gt;dpDown&lt;/code&gt; for UP transitions&lt;/li&gt;
&lt;li&gt;Precompute suffix sums of &lt;code&gt;dpUp&lt;/code&gt; for DOWN transitions&lt;/li&gt;
&lt;li&gt;This reduces each layer update to &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt;: For length 1, every value can start a sequence in either direction state, so both &lt;code&gt;dpUp[i] = 1&lt;/code&gt; and &lt;code&gt;dpDown[i] = 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/003699-number-of-zigzag-arrays-i" rel="noopener noreferrer"&gt;3699. Number of ZigZag Arrays 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 $n
 * @param Integer $l
 * @param Integer $r
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;zigZagArrays&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;$l&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;$r&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;zigZagArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;zigZagArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;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: 10&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;zigZagArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Output: 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;Avoiding Equal Adjacent Elements&lt;/strong&gt;: The transition formulas use strict inequalities (&lt;code&gt;x &amp;lt; y&lt;/code&gt; for &lt;strong&gt;UP&lt;/strong&gt;, &lt;code&gt;x &amp;gt; y&lt;/code&gt; for &lt;strong&gt;DOWN&lt;/strong&gt;), which naturally prevents equal adjacent values&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Three Consecutive Monotonic&lt;/strong&gt;: By forcing alternating direction states (&lt;strong&gt;UP&lt;/strong&gt; then &lt;strong&gt;DOWN&lt;/strong&gt; then &lt;strong&gt;UP&lt;/strong&gt;...), we ensure that three consecutive elements cannot be strictly increasing or decreasing, as that would require two UP moves or two DOWN moves in a row&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefix Sum Strategy&lt;/strong&gt;: For &lt;code&gt;newUp[i]&lt;/code&gt;, we sum all &lt;code&gt;dpDown[0..i-1]&lt;/code&gt;. The prefix sum array gives this in &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; per position&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Suffix Sum Strategy&lt;/strong&gt;: For &lt;code&gt;newDown[i]&lt;/code&gt;, we sum all &lt;code&gt;dpUp[i+1..m-1]&lt;/code&gt;. The suffix sum array gives this in &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; per position&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final Answer&lt;/strong&gt;: Sum all values from both &lt;code&gt;dpUp&lt;/code&gt; and &lt;code&gt;dpDown&lt;/code&gt; for length n, as sequences can end with either direction state&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n * m)&lt;/strong&gt;&lt;/em&gt; where &lt;code&gt;n&lt;/code&gt; is the sequence length and &lt;code&gt;m = r - l + 1&lt;/code&gt; (range size)

&lt;ul&gt;
&lt;li&gt;Each iteration computes prefix sums &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;, suffix sums &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;, and updates &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;No nested loops over &lt;em&gt;&lt;strong&gt;m²&lt;/strong&gt;&lt;/em&gt; in transitions due to prefix/suffix optimization&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt; for the DP arrays (constant number of arrays of size &lt;em&gt;&lt;strong&gt;m&lt;/strong&gt;&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1189. Maximum Number of Balloons</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 22 Jun 2026 17:28:29 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1189-maximum-number-of-balloons-1gkl</link>
      <guid>https://dev.to/mdarifulhaque/1189-maximum-number-of-balloons-1gkl</guid>
      <description>&lt;p&gt;1189. Maximum Number of Balloons&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;Hash Table&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Counting&lt;/code&gt;, &lt;code&gt;Weekly Contest 154&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given a string &lt;code&gt;text&lt;/code&gt;, you want to use the characters of &lt;code&gt;text&lt;/code&gt; to form as many instances of the word &lt;strong&gt;"balloon"&lt;/strong&gt; as possible.&lt;/p&gt;

&lt;p&gt;You can use each character in &lt;code&gt;text&lt;/code&gt; &lt;strong&gt;at most once&lt;/strong&gt;. Return the maximum number of instances that can be formed.&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%2F5s6xm2ix0levd0g9nk3p.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5s6xm2ix0levd0g9nk3p.JPG" alt="1536_ex1_upd" width="277" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; text = "nlaebolko"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;/ul&gt;

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

&lt;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%2Fwvd4c2zz78sa17og40io.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwvd4c2zz78sa17og40io.JPG" alt="1536_ex2_upd" width="480" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; text = "loonbalxballpoon"&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 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; text = "leetcode"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= text.length &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;text&lt;/code&gt; consists of lower case English letters only.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This question is the same as &lt;a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" rel="noopener noreferrer"&gt;2287: Rearrange Characters to Make Target String&lt;/a&gt;.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Count the frequency of letters in the given string.&lt;/li&gt;
&lt;li&gt;Find the letter than can make the minimum number of instances of the word "balloon".&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;strong&gt;We&lt;/strong&gt; implement a character frequency-based solution that counts occurrences of each letter in the input string and determines the maximum number of "balloon" instances by finding the limiting character ratio. The solution efficiently handles up to 10,000 characters in O(n) time and O(1) space, using the minimum ratio of available letters to required letters as the answer.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Character Counting&lt;/strong&gt;: Count the frequency of each character in the input string using &lt;code&gt;array_count_values()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define Requirements&lt;/strong&gt;: Map each character in "balloon" to its required count (b:1, a:1, l:2, o:2, n:1).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ratio Calculation&lt;/strong&gt;: For each required character, calculate how many complete "balloon" words can be formed by dividing available count by required count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find Minimum&lt;/strong&gt;: The maximum number of instances is limited by the character with the smallest ratio, as it becomes the bottleneck.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early Exit&lt;/strong&gt;: If any required character is missing entirely, return 0 immediately.&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/001189-maximum-number-of-balloons" rel="noopener noreferrer"&gt;1189. Maximum Number of Balloons&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 $text
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxNumberOfBalloons&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;$text&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;maxNumberOfBalloons&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"nlaebolko"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;maxNumberOfBalloons&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"loonbalxballpoon"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;maxNumberOfBalloons&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"leetcode"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Why the limiting factor works&lt;/strong&gt;: Each "balloon" needs specific quantities of each letter. The letter with the fewest available "sets" determines how many complete words can be formed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Special handling for 'l' and 'o'&lt;/strong&gt;: These appear twice in "balloon", so their available counts must be divided by 2 to get the number of complete words possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integer division is sufficient&lt;/strong&gt;: We use integer division (&lt;code&gt;intdiv&lt;/code&gt;) since we can only form complete words—partial words don't count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-world analogy&lt;/strong&gt;: Imagine building sandwiches where "balloon" is the recipe. Even if you have 100 pieces of bread, you can only make as many sandwiches as your least available ingredient allows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge case handling&lt;/strong&gt;: If &lt;code&gt;text&lt;/code&gt; is empty or missing any required letter, the function correctly returns 0 since no "balloon" can be formed.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — where n is the length of &lt;code&gt;text&lt;/code&gt;. We iterate through the string once to count frequencies, then iterate through a constant number of required characters (5 distinct letters).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — the frequency array stores at most 26 lowercase English letters, and the required array has a fixed size. Memory usage is constant regardless of input size.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1833. Maximum Ice Cream Bars</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 21 Jun 2026 14:49:03 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1833-maximum-ice-cream-bars-1ije</link>
      <guid>https://dev.to/mdarifulhaque/1833-maximum-ice-cream-bars-1ije</guid>
      <description>&lt;p&gt;1833. Maximum Ice Cream Bars&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Counting Sort&lt;/code&gt;, &lt;code&gt;Weekly Contest 237&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is a sweltering summer day, and a boy wants to buy some ice cream bars.&lt;/p&gt;

&lt;p&gt;At the store, there are &lt;code&gt;n&lt;/code&gt; ice cream bars. You are given an array &lt;code&gt;costs&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt;, where &lt;code&gt;costs[i]&lt;/code&gt; is the price of the &lt;code&gt;iᵗʰ&lt;/code&gt; ice cream bar in coins. The boy initially has &lt;code&gt;coins&lt;/code&gt; coins to spend, and he wants to buy as many ice cream bars as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The boy can buy the ice cream bars in any order.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum&lt;/strong&gt; number of ice cream bars the boy can buy with &lt;code&gt;coins&lt;/code&gt; coins&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You must solve the problem by counting sort.&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; costs = [1,3,2,4,1], coins = 7&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.&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; costs = [10,6,8,7,7,8], coins = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The boy cannot afford any of the ice cream bars.&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; costs = [1,6,3,1,2,5], coins = 20&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 6&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.&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;costs.length == n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= costs[i] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= coins &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;It is always optimal to buy the least expensive ice cream bar first.&lt;/li&gt;
&lt;li&gt;Sort the prices so that the cheapest ice cream bar comes first.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We implement a counting sort–based solution to maximize the number of ice cream bars purchased. Instead of using a comparison-based sort (O(n log n)), we take advantage of the bounded price range (1 ≤ costs[i] ≤ 10⁵) to count frequencies and then greedily buy the cheapest bars first. This approach processes prices in ascending order, buying as many as possible at each price level until the coins run out.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Counting frequencies&lt;/strong&gt; – Since &lt;code&gt;costs[i]&lt;/code&gt; are within a known range (1 to 10⁵), we create a frequency array where &lt;code&gt;freq[price]&lt;/code&gt; stores how many bars cost that exact amount.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Greedy purchase strategy&lt;/strong&gt; – We iterate prices from 1 upward (cheapest to most expensive). At each price, we buy as many bars as possible using &lt;code&gt;min(freq[price], floor(coins / price))&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update remaining coins&lt;/strong&gt; – Subtract the total spent on bars of this price from &lt;code&gt;coins&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early termination&lt;/strong&gt; – If after purchasing at a given price, the remaining coins are less than the next price, we stop because all subsequent prices are higher.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Counting sort avoids sorting overhead&lt;/strong&gt; – This approach runs in O(n + maxCost) time instead of O(n log n), meeting the problem's requirement to use counting sort.&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/001833-maximum-ice-cream-bars" rel="noopener noreferrer"&gt;1833. Maximum Ice Cream Bars&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[] $costs
 * @param Integer $coins
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxIceCream&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;$costs&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;$coins&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;maxIceCream&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;maxIceCream&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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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="mi"&gt;8&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&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxIceCream&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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;
&lt;strong&gt;Why buy cheapest first?&lt;/strong&gt; – To maximize the count of items purchased, we must minimize the cost per item. Buying the least expensive bars first is always optimal (greedy choice property).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frequency counting&lt;/strong&gt; – We map each price to its frequency, eliminating the need to sort the original array. This is efficient when the maximum price is bounded (&lt;em&gt;&lt;strong&gt;10⁵&lt;/strong&gt;&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step-by-step consumption&lt;/strong&gt; – For each price level, we calculate the maximum affordable quantity. We purchase that many, reduce the coin balance, and accumulate the count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Break condition&lt;/strong&gt; – If we cannot afford even one more bar at the current price after buying some, we break. If we fully exhaust a price level and still have coins, we move to the next price.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Total count returned&lt;/strong&gt; – The accumulated &lt;code&gt;count&lt;/code&gt; is the maximum number of bars the boy can buy.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(n + maxCost)&lt;/strong&gt;&lt;/em&gt; – one pass to build the frequency array and one pass through the price range.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; = number of bars &lt;em&gt;&lt;strong&gt;(1 ≤ n ≤ 10⁵)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;maxCost&lt;/code&gt; = maximum price in costs &lt;em&gt;&lt;strong&gt;(≤ 10⁵)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(maxCost)&lt;/strong&gt;&lt;/em&gt; for the frequency array. If we consider the input array itself, it's &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, but auxiliary space is &lt;em&gt;&lt;strong&gt;O(maxCost)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1840. Maximum Building Height</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 20 Jun 2026 17:45:50 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1840-maximum-building-height-8lb</link>
      <guid>https://dev.to/mdarifulhaque/1840-maximum-building-height-8lb</guid>
      <description>&lt;p&gt;1840. Maximum Building Height&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;Sorting&lt;/code&gt;, &lt;code&gt;Weekly Contest 238&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You want to build &lt;code&gt;n&lt;/code&gt; new buildings in a city. The new buildings will be built in a line and are labeled from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, there are city restrictions on the heights of the new buildings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The height of each building must be a non-negative integer.&lt;/li&gt;
&lt;li&gt;The height of the first building &lt;strong&gt;must&lt;/strong&gt; be &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The height difference between any two adjacent buildings &lt;strong&gt;cannot exceed&lt;/strong&gt; &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array &lt;code&gt;restrictions&lt;/code&gt; where &lt;code&gt;restrictions[i] = [idᵢ, maxHeightᵢ]&lt;/code&gt; indicates that building &lt;code&gt;idᵢ&lt;/code&gt; must have a height &lt;strong&gt;less than or equal to&lt;/strong&gt; &lt;code&gt;maxHeightᵢ&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is guaranteed that each building will appear &lt;strong&gt;at most once&lt;/strong&gt; in &lt;code&gt;restrictions&lt;/code&gt;, and building &lt;code&gt;1&lt;/code&gt; will &lt;strong&gt;not&lt;/strong&gt; be in &lt;code&gt;restrictions&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum possible height&lt;/strong&gt; of the &lt;strong&gt;tallest&lt;/strong&gt; building&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%2Fsg06797lemavxosjdfjz.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%2Fsg06797lemavxosjdfjz.png" alt="ic236-q4-ex1-1" width="491" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 5, restrictions = [[2,1],[4,1]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;The green area in the image indicates the maximum allowed height for each building.&lt;/li&gt;
&lt;li&gt;We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 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;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%2Fowccbkufa434hoi7dp01.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%2Fowccbkufa434hoi7dp01.png" alt="ic236-q4-ex2" width="577" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 6, restrictions = []&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 green area in the image indicates the maximum allowed height for each building.&lt;/li&gt;
&lt;li&gt;We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;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%2F1souilj8flxf09kpplek.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%2F1souilj8flxf09kpplek.png" alt="ic236-q4-ex3" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;The green area in the image indicates the maximum allowed height for each building.&lt;/li&gt;
&lt;li&gt;We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= n &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= restrictions.length &amp;lt;= min(n - 1, 10⁵)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= idᵢ &amp;lt;= n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;idᵢ&lt;/code&gt; is &lt;strong&gt;unique&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= maxHeightᵢ &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;Is it possible to find the max height if given the height range of a particular building?&lt;/li&gt;
&lt;li&gt;You can find the height range of a restricted building by doing 2 passes from the left and right.&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 maximum building height problem by transforming the restrictions into feasible height constraints using a two-pass propagation algorithm. We add building 1 with height 0 as a restriction, sort all restrictions by building ID, then propagate maximum possible heights from left to right and right to left to ensure all adjacent height differences are respected. Finally, we compute the maximum possible height between each pair of consecutive restrictions and after the last restriction, returning the overall maximum.&lt;/p&gt;

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

&lt;p&gt;• &lt;strong&gt;Add base restriction&lt;/strong&gt; - Include building 1 with height 0 as a restriction since it's mandatory&lt;br&gt;
• &lt;strong&gt;Sort restrictions&lt;/strong&gt; - Order by building ID to process in sequence&lt;br&gt;
• &lt;strong&gt;Left-to-right pass&lt;/strong&gt; - Propagate height constraints forward: each restricted building's height cannot exceed the previous restricted height plus the distance between them&lt;br&gt;
• &lt;strong&gt;Right-to-left pass&lt;/strong&gt; - Propagate height constraints backward: each restricted building's height cannot exceed the next restricted height plus the distance between them&lt;br&gt;
• &lt;strong&gt;Calculate peak heights&lt;/strong&gt; - For each interval between consecutive restrictions, find the maximum height achievable using the formula that accounts for the distance and height difference&lt;br&gt;
• &lt;strong&gt;Handle trailing segment&lt;/strong&gt; - Consider the remaining buildings after the last restriction, where height can increase by 1 each step&lt;/p&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/001840-maximum-building-height" rel="noopener noreferrer"&gt;1840. Maximum Building Height&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[][] $restrictions
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxBuilding&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="nv"&gt;$restrictions&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;maxBuilding&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;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;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: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxBuilding&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="p"&gt;[])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;maxBuilding&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="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;span class="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;Problem insight&lt;/strong&gt;: With adjacent height difference ≤ 1 and building 1 at height 0, the maximum possible height at any position is limited by both left and right restrictions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two-pass propagation&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Left-to-right ensures heights don't exceed what's possible from the left side&lt;/li&gt;
&lt;li&gt;Right-to-left ensures heights don't exceed what's possible from the right side&lt;/li&gt;
&lt;li&gt;After both passes, each restriction has the true maximum feasible height&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Peak calculation between restrictions&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Given two points (x₁, h₁) and (x₂, h₂) with distance d = x₂ - x₁&lt;/li&gt;
&lt;li&gt;The maximum possible height between them occurs when we go up from the lower point and down to the higher point&lt;/li&gt;
&lt;li&gt;Formula: max(h₁, h₂) + floor((d - |h₂ - h₁|) / 2)&lt;/li&gt;
&lt;li&gt;This represents the peak of a "mountain" shape that satisfies both height constraints&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling the end&lt;/strong&gt;: After the last restriction, we can continue increasing height by 1 per building, so the maximum is lastHeight + (n - lastId)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge cases&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Empty restrictions array → heights can be [0,1,2,...,n-1]&lt;/li&gt;
&lt;li&gt;Very large n (up to 10⁹) → O(m log m) solution works efficiently&lt;/li&gt;
&lt;li&gt;Only m up to 10⁵ restrictions, so we process only these points&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m log m)&lt;/strong&gt;&lt;/em&gt; where m is the number of restrictions, dominated by sorting. The two passes and final calculation are &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; extra space if we modify the input array in-place; &lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt; if we create a copy. The solution modifies the restrictions array, so &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; auxiliary space.&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>1732. Find the Highest Altitude</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 19 Jun 2026 15:19:31 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1732-find-the-highest-altitude-k32</link>
      <guid>https://dev.to/mdarifulhaque/1732-find-the-highest-altitude-k32</guid>
      <description>&lt;p&gt;1732. Find the Highest Altitude&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Biweekly Contest 44&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There is a biker going on a road trip. The road trip consists of &lt;code&gt;n + 1&lt;/code&gt; points at different altitudes. The biker starts his trip on point &lt;code&gt;0&lt;/code&gt; with altitude equal &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;gain&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt; where &lt;code&gt;gain[i]&lt;/code&gt; is the &lt;strong&gt;net gain in altitude&lt;/strong&gt; between points &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;i + 1&lt;/code&gt; for all (&lt;code&gt;0 &amp;lt;= i &amp;lt; n&lt;/code&gt;). Return &lt;em&gt;the &lt;strong&gt;highest altitude&lt;/strong&gt; of a point&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; gain = [-5,1,5,0,-7]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; gain = [-4,-3,-2,-1,4,3,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n == gain.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-100 &amp;lt;= gain[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Let's note that the altitude of an element is the sum of gains of all the elements behind it&lt;/li&gt;
&lt;li&gt;Getting the altitudes can be done by getting the prefix sum array of the given array&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We need to find the highest altitude reached by a biker who starts at altitude &lt;code&gt;0&lt;/code&gt; and gains or loses altitude based on a given array &lt;code&gt;gain&lt;/code&gt;. We simulate the trip by accumulating the net gain step by step, tracking the maximum altitude encountered.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Initialize current altitude as &lt;code&gt;0&lt;/code&gt; and maximum altitude as &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Iterate through each gain value in the array.&lt;/li&gt;
&lt;li&gt;Add the gain to the current altitude.&lt;/li&gt;
&lt;li&gt;Update the maximum altitude if the current altitude exceeds it.&lt;/li&gt;
&lt;li&gt;Return the maximum altitude after processing all gains.&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/001732-find-the-highest-altitude" rel="noopener noreferrer"&gt;1732. Find the Highest Altitude&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[] $gain
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;largestAltitude&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;$gain&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;largestAltitude&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;7&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;largestAltitude&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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;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;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="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;The biker starts at altitude &lt;code&gt;0&lt;/code&gt;, so the initial maximum is &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each step &lt;code&gt;i&lt;/code&gt;, the new altitude is the sum of all gains from index &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We don’t need to store all altitudes; we just keep a running total (&lt;code&gt;currentAltitude&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;At each step, we compare &lt;code&gt;currentAltitude&lt;/code&gt; with &lt;code&gt;maxAltitude&lt;/code&gt; and keep the larger value.&lt;/li&gt;
&lt;li&gt;After the loop, &lt;code&gt;maxAltitude&lt;/code&gt; holds the highest point reached during the trip.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Complexity Analysis&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; – single pass through the &lt;code&gt;gain&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; – only a few integer variables used.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1344. Angle Between Hands of a Clock</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 18 Jun 2026 16:50:47 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1344-angle-between-hands-of-a-clock-3eha</link>
      <guid>https://dev.to/mdarifulhaque/1344-angle-between-hands-of-a-clock-3eha</guid>
      <description>&lt;p&gt;1344. Angle Between Hands of a Clock&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;Biweekly Contest 19&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given two numbers, &lt;code&gt;hour&lt;/code&gt; and &lt;code&gt;minutes&lt;/code&gt;, return &lt;em&gt;the smaller angle (in degrees) formed between the &lt;code&gt;hour&lt;/code&gt; and the &lt;code&gt;minute&lt;/code&gt; hand&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Answers within &lt;code&gt;10⁻⁵&lt;/code&gt; of the actual value will be accepted as correct.&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%2Fsj5n67o8d3x0n6rnk5op.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%2Fsj5n67o8d3x0n6rnk5op.png" alt="sample_1_1673" width="734" height="723"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; hour = 12, minutes = 30&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 165&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1xzwwpk5cbj7y5gqs2ev.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%2F1xzwwpk5cbj7y5gqs2ev.png" alt="sample_2_1673" width="722" height="725"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; hour = 3, minutes = 30&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 75&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%2Fxqarpsno7vjzs60c8hv4.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%2Fxqarpsno7vjzs60c8hv4.png" alt="sample_3_1673" width="722" height="725"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; hour = 3, minutes = 15&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 7.5&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= hour &amp;lt;= 12&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= minutes &amp;lt;= 59&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;The tricky part is determining how the minute hand affects the position of the hour hand.&lt;/li&gt;
&lt;li&gt;Calculate the angles separately then find the difference.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We implement a direct mathematical solution to compute the smaller angle between the clock hands. By calculating each hand’s angular position independently and then taking the minimum of the absolute difference and its complement to 360°, we efficiently obtain the result in constant time.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Minute hand angle&lt;/strong&gt; – Each minute moves the minute hand by &lt;code&gt;6°&lt;/code&gt; (since &lt;code&gt;360° / 60 = 6°&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hour hand angle&lt;/strong&gt; – Each hour moves the hour hand by &lt;code&gt;30°&lt;/code&gt; (since &lt;code&gt;360° / 12 = 30°&lt;/code&gt;), plus an additional &lt;code&gt;0.5°&lt;/code&gt; per minute (since &lt;code&gt;30° / 60 = 0.5°&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Normalize hour&lt;/strong&gt; – Convert &lt;code&gt;12&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt; because the &lt;code&gt;12‑hour&lt;/code&gt; dial wraps around.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compute difference&lt;/strong&gt; – Take the absolute angular difference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return smaller angle&lt;/strong&gt; – Since two lines form two angles (&lt;code&gt;θ&lt;/code&gt; and &lt;code&gt;360°−θ&lt;/code&gt;), we return the minimum.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001344-angle-between-hands-of-a-clock" rel="noopener noreferrer"&gt;1344. Angle Between Hands of a Clock&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 $hour
 * @param Integer $minutes
 * @return Float
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;angleClock&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;$hour&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;$minutes&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;angleClock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 165&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;angleClock&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;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 75&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;angleClock&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;15&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 7.5&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 minute hand angle is simply &lt;code&gt;minutes * 6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The hour hand angle is &lt;code&gt;(hour % 12) * 30 + minutes * 0.5&lt;/code&gt;, accounting for the hour hand's continuous movement.&lt;/li&gt;
&lt;li&gt;The absolute difference &lt;code&gt;|hourAngle - minuteAngle|&lt;/code&gt; gives one of the two angles.&lt;/li&gt;
&lt;li&gt;The smaller angle is always &lt;code&gt;min(diff, 360 - diff)&lt;/code&gt; because the total circle is &lt;code&gt;360°&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This works for all valid inputs (&lt;code&gt;1–12 hour&lt;/code&gt;, &lt;code&gt;0–59 minutes&lt;/code&gt;) and meets the required precision (&lt;code&gt;10⁻⁵&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Complexity Analysis&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; – only a few arithmetic operations, independent of input size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; – uses only a constant number of variables.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3614. Process String with Special Operations II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 17 Jun 2026 16:24:31 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3614-process-string-with-special-operations-ii-4ogg</link>
      <guid>https://dev.to/mdarifulhaque/3614-process-string-with-special-operations-ii-4ogg</guid>
      <description>&lt;p&gt;3614. Process String with Special Operations 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;String&lt;/code&gt;, &lt;code&gt;Simulation&lt;/code&gt;, &lt;code&gt;Weekly Contest 458&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a string &lt;code&gt;s&lt;/code&gt; consisting of lowercase English letters and the special characters: &lt;code&gt;'*'&lt;/code&gt;, &lt;code&gt;'#'&lt;/code&gt;, and &lt;code&gt;'%'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are also given an integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Build a new string &lt;code&gt;result&lt;/code&gt; by processing &lt;code&gt;s&lt;/code&gt; according to the following rules from left to right:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the letter is a &lt;strong&gt;lowercase&lt;/strong&gt; English letter append it to &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;'*'&lt;/code&gt; &lt;strong&gt;removes&lt;/strong&gt; the last character from &lt;code&gt;result&lt;/code&gt;, if it exists.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;'#'&lt;/code&gt; &lt;strong&gt;duplicates&lt;/strong&gt; the current &lt;code&gt;result&lt;/code&gt; and &lt;strong&gt;appends&lt;/strong&gt; it to itself.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;'%'&lt;/code&gt; &lt;strong&gt;reverses&lt;/strong&gt; the current &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;code&gt;kᵗʰ&lt;/code&gt; character of the final string &lt;code&gt;result&lt;/code&gt;. If &lt;code&gt;k&lt;/code&gt; is out of the bounds of &lt;code&gt;result&lt;/code&gt;, return &lt;code&gt;'.'&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 = "a#b%*", k = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "a"&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;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;s[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Current &lt;code&gt;result&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'a'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"a"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'#'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Duplicate &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"aa"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'b'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'b'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"aab"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'%'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reverse &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"baa"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'*'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove the last character&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"ba"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The final &lt;code&gt;result&lt;/code&gt; is &lt;code&gt;"ba"&lt;/code&gt;. The character at index &lt;code&gt;k = 1&lt;/code&gt; is &lt;code&gt;'a'&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 = "cd%#*#", k = 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "d"&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;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;s[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Current &lt;code&gt;result&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'c'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'c'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"c"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'d'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'d'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"cd"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'%'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reverse &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"dc"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'#'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Duplicate &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"dcdc"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'*'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove the last character&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"dcd"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'#'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Duplicate &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"dcddcd"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The final &lt;code&gt;result&lt;/code&gt; is &lt;code&gt;"dcddcd"&lt;/code&gt;. The character at index &lt;code&gt;k = 3&lt;/code&gt; is &lt;code&gt;'d'&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 = "z*#", k = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "."&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;s[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Current &lt;code&gt;result&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'z'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'z'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"z"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'*'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove the last character&lt;/td&gt;
&lt;td&gt;&lt;code&gt;""&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'#'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Duplicate the string&lt;/td&gt;
&lt;td&gt;&lt;code&gt;""&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The final &lt;code&gt;result&lt;/code&gt; is &lt;code&gt;""&lt;/code&gt;. Since index &lt;code&gt;k = 0&lt;/code&gt; is out of bounds, the output is &lt;code&gt;'.'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; consists of only lowercase English letters and special characters &lt;code&gt;'*'&lt;/code&gt;, &lt;code&gt;'#'&lt;/code&gt;, and &lt;code&gt;'%'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= k &amp;lt;= 10¹⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The length of &lt;code&gt;result&lt;/code&gt; after processing &lt;code&gt;s&lt;/code&gt; will not exceed &lt;code&gt;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;Track the length of the string after each operation on &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Walk backwards through &lt;code&gt;s&lt;/code&gt;, undoing each &lt;code&gt;#&lt;/code&gt; by using modulus on the tracked lengths, and undoing each % by mirroring across the midpoint, to pinpoint the &lt;code&gt;kᵗʰ&lt;/code&gt; character.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We simulate the process in reverse to avoid constructing strings that may be up to &lt;em&gt;&lt;strong&gt;10¹⁵&lt;/strong&gt;&lt;/em&gt; characters long. First, we compute the length after each operation. Then, starting from the final index &lt;code&gt;k&lt;/code&gt;, we walk backward through the operations, reversing their effects (&lt;code&gt;#&lt;/code&gt; and &lt;code&gt;%&lt;/code&gt;) until we identify which original letter corresponds to the desired position. This guarantees &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; time and &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; space.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Forward length pass&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterate through &lt;code&gt;s&lt;/code&gt; once, maintaining the current length after each operation:&lt;/li&gt;
&lt;li&gt;Letters → increment length&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; → decrement if possible&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#&lt;/code&gt; → double length&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%&lt;/code&gt; → length unchanged&lt;/li&gt;
&lt;li&gt;Store the length after each step in an array.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Validate &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt; If &lt;code&gt;k&lt;/code&gt; is out of bounds of the final length, return &lt;code&gt;"."&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Backward reduction&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traverse &lt;code&gt;s&lt;/code&gt; from the last operation to the first. At each step:&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Letter&lt;/strong&gt;: If &lt;code&gt;k&lt;/code&gt; points to the last position of the current length, return that letter. Otherwise, just move left.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt;**: No change to &lt;code&gt;k&lt;/code&gt;; the removed character is not relevant for earlier positions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#&lt;/code&gt;**: If &lt;code&gt;k&lt;/code&gt; is in the duplicated second half (&lt;code&gt;k &amp;gt;= prevLength&lt;/code&gt;), map it to the first half by subtracting &lt;code&gt;prevLength&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%&lt;/code&gt;**: Reverse the index within the previous length: &lt;code&gt;k = prevLength - 1 - k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Return result&lt;/strong&gt; If we exit the loop without finding a letter (should not happen for valid &lt;code&gt;k&lt;/code&gt;), return &lt;code&gt;"."&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003614-process-string-with-special-operations-ii" rel="noopener noreferrer"&gt;3614. Process String with Special Operations 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 String $s
 * @param Integer $k
 * @return String
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;processStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;processStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a#b%*"&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="c1"&gt;// Output: "a"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;processStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd%#*#"&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="c1"&gt;// Output: "d"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;processStr&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// Output: "."&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Why process backwards?&lt;/strong&gt; Forward simulation would build enormous strings (up to 10¹⁵), which is infeasible. Backward processing lets us reduce the problem to finding which original letter generated a specific position.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling &lt;code&gt;#&lt;/code&gt; (duplicate)&lt;/strong&gt; The duplicate operation creates two identical halves. If our target &lt;code&gt;k&lt;/code&gt; falls in the second half, we simply subtract the first half’s length to map it to the corresponding position in the first half.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling &lt;code&gt;%&lt;/code&gt; (reverse)&lt;/strong&gt; Reversal mirrors the string. The character at index &lt;code&gt;k&lt;/code&gt; in the reversed string comes from index &lt;code&gt;prevLength - 1 - k&lt;/code&gt; in the string before reversal. We apply that mirror transformation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling &lt;code&gt;*&lt;/code&gt; (deletion)&lt;/strong&gt; Since we only care about the final string, a deletion removes the last character. When walking backward, that last character is irrelevant for earlier indices, so we just ignore the operation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling letters&lt;/strong&gt; When we encounter a letter appended at the end of the string, if &lt;code&gt;k&lt;/code&gt; is exactly the last index of that current string, that letter is our answer. Otherwise, we continue backward to earlier operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Complexity Analysis&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;s&lt;/code&gt; — one forward pass and one backward pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space complexity:&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — to store the length array of size &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3612. Process String with Special Operations I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 16 Jun 2026 16:45:57 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3612-process-string-with-special-operations-i-4a29</link>
      <guid>https://dev.to/mdarifulhaque/3612-process-string-with-special-operations-i-4a29</guid>
      <description>&lt;p&gt;3612. Process String with Special Operations 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;Senior&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Simulation&lt;/code&gt;, &lt;code&gt;Weekly Contest 458&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a string &lt;code&gt;s&lt;/code&gt; consisting of lowercase English letters and the special characters: &lt;code&gt;'*'&lt;/code&gt;, &lt;code&gt;'#'&lt;/code&gt;, and &lt;code&gt;%&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Build a new string &lt;code&gt;result&lt;/code&gt; by processing &lt;code&gt;s&lt;/code&gt; according to the following rules from left to right:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the letter is a &lt;strong&gt;lowercase&lt;/strong&gt; English letter append it to &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;'*'&lt;/code&gt; &lt;strong&gt;removes&lt;/strong&gt; the last character from &lt;code&gt;result&lt;/code&gt;, if it exists.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;'#'&lt;/code&gt; &lt;strong&gt;duplicates&lt;/strong&gt; the current &lt;code&gt;result&lt;/code&gt; and &lt;strong&gt;appends&lt;/strong&gt; it to itself.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;'%'&lt;/code&gt; &lt;strong&gt;reverses&lt;/strong&gt; the current &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the final string &lt;code&gt;result&lt;/code&gt; after processing all characters in &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 = "a#b%*"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "ba"&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;s[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Current &lt;code&gt;result&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'a'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"a"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'#'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Duplicate &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"aa"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'b'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'b'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"aab"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'%'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reverse &lt;code&gt;result&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;"baa"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'*'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove the last character&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"ba"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Thus, the final &lt;code&gt;result&lt;/code&gt; is &lt;code&gt;"ba"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; s = "z*#"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ""&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;s[i]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Current &lt;code&gt;result&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'z'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append &lt;code&gt;'z'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"z"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'*'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove the last character&lt;/td&gt;
&lt;td&gt;&lt;code&gt;""&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'#'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Duplicate the string&lt;/td&gt;
&lt;td&gt;&lt;code&gt;""&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Thus, the final &lt;code&gt;result&lt;/code&gt; is &lt;code&gt;""&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 20&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; consists of only lowercase English letters and special characters &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;#,&lt;/code&gt; and &lt;code&gt;%&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;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;The problem requires simulating a string transformation where regular letters are appended, and special characters (&lt;code&gt;*&lt;/code&gt;, &lt;code&gt;#&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;) trigger specific operations—removal, duplication, or reversal of the current result. Since constraints are small (&lt;code&gt;len ≤ 20&lt;/code&gt;), a direct left‑to‑right simulation using string operations is straightforward and efficient.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linear Scan&lt;/strong&gt;: Iterate through the input string from left to right, applying operations as defined.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use PHP String Functions&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.=&lt;/code&gt; for appending letters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;substr($result, 0, -1)&lt;/code&gt; for safe removal of the last character.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$result .= $result&lt;/code&gt; for duplication.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;strrev()&lt;/code&gt; for reversal.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; on empty string does nothing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#&lt;/code&gt; on empty string stays empty.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%&lt;/code&gt; on empty string stays empty.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Extra Data Structures&lt;/strong&gt;: Since only the final string is needed, maintain a single string variable.&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/003612-process-string-with-special-operations-i" rel="noopener noreferrer"&gt;3612. Process String with Special Operations 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 String $s
 * @return String
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;processStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$s&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="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;processStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a#b%*"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// Output: "ba"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;processStr&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="c1"&gt;// Output: ""&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initialize&lt;/strong&gt; &lt;code&gt;$result&lt;/code&gt; as an empty string to hold the output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loop&lt;/strong&gt; through each character &lt;code&gt;$char&lt;/code&gt; in the input string &lt;code&gt;$s&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;$char&lt;/code&gt; is a lowercase letter&lt;/strong&gt;, append it directly to &lt;code&gt;$result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;$char&lt;/code&gt; is &lt;code&gt;'*'&lt;/code&gt;&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Check if &lt;code&gt;$result&lt;/code&gt; is not empty.&lt;/li&gt;
&lt;li&gt;If yes, remove its last character using &lt;code&gt;substr()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;$char&lt;/code&gt; is &lt;code&gt;'#'&lt;/code&gt;&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Duplicate the current &lt;code&gt;$result&lt;/code&gt; by appending a copy of itself (&lt;code&gt;$result .= $result&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;$char&lt;/code&gt; is &lt;code&gt;'%'&lt;/code&gt;&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Reverse &lt;code&gt;$result&lt;/code&gt; using &lt;code&gt;strrev()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After the loop, return the final &lt;code&gt;$result&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;strong&gt;O(n × m)&lt;/strong&gt; in the worst case, where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;m&lt;/code&gt; is the length of &lt;code&gt;result&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;Each append, removal, duplication, and reversal takes O(m) time in PHP due to string copying.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;n ≤ 20&lt;/code&gt;, this is negligible.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;strong&gt;O(m)&lt;/strong&gt;, where &lt;code&gt;m&lt;/code&gt; is the length of the final &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

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