<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MD ARIFUL HAQUE</title>
    <description>The latest articles on DEV Community by MD ARIFUL HAQUE (@mdarifulhaque).</description>
    <link>https://dev.to/mdarifulhaque</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F944478%2F148ec474-230c-4c52-8b8e-5aba51bc6d2e.jpeg</url>
      <title>DEV Community: MD ARIFUL HAQUE</title>
      <link>https://dev.to/mdarifulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdarifulhaque"/>
    <language>en</language>
    <item>
      <title>1391. Check if There is a Valid Path in a Grid</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 27 Apr 2026 16:42:46 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1391-check-if-there-is-a-valid-path-in-a-grid-43n2</link>
      <guid>https://dev.to/mdarifulhaque/1391-check-if-there-is-a-valid-path-in-a-grid-43n2</guid>
      <description>&lt;p&gt;1391. Check if There is a Valid Path in a Grid&lt;/p&gt;

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

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

&lt;p&gt;You are given an &lt;code&gt;m x n&lt;/code&gt; &lt;code&gt;grid&lt;/code&gt;. Each cell of &lt;code&gt;grid&lt;/code&gt; represents a street. The street of &lt;code&gt;grid[i][j]&lt;/code&gt; can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; which means a street connecting the left cell and the right cell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2&lt;/code&gt; which means a street connecting the upper cell and the lower cell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3&lt;/code&gt; which means a street connecting the left cell and the lower cell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;4&lt;/code&gt; which means a street connecting the right cell and the lower cell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;5&lt;/code&gt; which means a street connecting the left cell and the upper cell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;6&lt;/code&gt; which means a street connecting the right cell and the upper cell.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;You will initially start at the street of the upper-left cell &lt;code&gt;(0, 0)&lt;/code&gt;. A valid path in the grid is a path that starts from the upper left cell &lt;code&gt;(0, 0)&lt;/code&gt; and ends at the bottom-right cell &lt;code&gt;(m - 1, n - 1)&lt;/code&gt;. &lt;strong&gt;The path should only follow the streets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notice&lt;/strong&gt; that you are &lt;strong&gt;not allowed&lt;/strong&gt; to change any street.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if there is a valid path in the grid or &lt;code&gt;false&lt;/code&gt; otherwise&lt;/em&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [[2,4,3],[6,5,2]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; As shown you can start at cell &lt;code&gt;(0, 0)&lt;/code&gt; and visit all the cells of the grid to reach &lt;code&gt;(m - 1, n - 1)&lt;/code&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.amazonaws.com%2Fuploads%2Farticles%2Fsp7b74uwlqr0b344w5vt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp7b74uwlqr0b344w5vt.png" alt="e2" width="569" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [[1,2,1],[1,2,1]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; As shown you the street at cell &lt;code&gt;(0, 0)&lt;/code&gt; is not connected with any street of any other cell and you will get stuck at cell &lt;code&gt;(0, 0)&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; grid = [[1,1,2]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; You will get stuck at cell &lt;code&gt;(0, 1)&lt;/code&gt; and you cannot reach cell &lt;code&gt;(0, 2)&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;m == grid.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n == grid[i].length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= m, n &amp;lt;= 300&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= grid[i][j] &amp;lt;= 6&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;Start DFS from the node &lt;code&gt;(0, 0)&lt;/code&gt; and follow the path till you stop.&lt;/li&gt;
&lt;li&gt;When you reach a cell and cannot move anymore check that this cell is &lt;code&gt;(m - 1, n - 1)&lt;/code&gt; or &lt;code&gt;not&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The problem requires checking whether there exists a valid path from the &lt;strong&gt;top-left&lt;/strong&gt; cell &lt;code&gt;(0, 0)&lt;/code&gt; to the &lt;strong&gt;bottom-right&lt;/strong&gt; cell &lt;code&gt;(m-1, n-1)&lt;/code&gt; strictly following the predefined connections of each street type. A &lt;strong&gt;DFS (Depth-First Search)&lt;/strong&gt; approach is used to explore connected cells according to street shapes, ensuring movement is only possible when the current street and the neighbor’s street align properly.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grid Traversal with DFS&lt;/strong&gt; Start from the source cell &lt;code&gt;(0, 0)&lt;/code&gt; and recursively explore reachable cells.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Street Connection Mapping&lt;/strong&gt; Each street type (1–6) has specific connection directions:  &lt;code&gt;0=Up&lt;/code&gt;, &lt;code&gt;1=Right&lt;/code&gt;, &lt;code&gt;2=Down&lt;/code&gt;, &lt;code&gt;3=Left&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility Check Between Cells&lt;/strong&gt; When moving from a cell to a neighbor, the neighbor must support the &lt;em&gt;opposite movement direction&lt;/em&gt; relative to the current cell.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visited Tracking&lt;/strong&gt; Prevent revisiting cells to avoid infinite loops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Termination Condition&lt;/strong&gt; Stop and return &lt;code&gt;true&lt;/code&gt; if the bottom-right cell is reached.&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/001391-check-if-there-is-a-valid-path-in-a-grid" rel="noopener noreferrer"&gt;1391. Check if There is a Valid Path in a Grid&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&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="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Directions: up, right, down, left&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$dirs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="c1"&gt;// For each street type (1-6), list of directions it connects to&lt;/span&gt;
    &lt;span class="c1"&gt;// Up:0, Right:1, Down:2, Left:3&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$connections&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;      &lt;span class="c1"&gt;// left and right&lt;/span&gt;
        &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;      &lt;span class="c1"&gt;// up and down&lt;/span&gt;
        &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;      &lt;span class="c1"&gt;// down and left&lt;/span&gt;
        &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;      &lt;span class="c1"&gt;// right and down&lt;/span&gt;
        &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;      &lt;span class="c1"&gt;// up and left&lt;/span&gt;
        &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;       &lt;span class="c1"&gt;// up and right&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// Inverse mapping: from direction to required connection in neighbor&lt;/span&gt;
    &lt;span class="c1"&gt;// If we go from current cell to neighbor in direction d,&lt;/span&gt;
    &lt;span class="c1"&gt;// neighbor must have the opposite direction in its connections&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$opposite&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// opposite[0]=2 (up&amp;lt;-&amp;gt;down), etc.&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * @param Integer[][] $grid
     * @return Boolean
     */&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;hasValidPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$grid&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="mf"&gt;...&lt;/span&gt;
       &lt;span class="mf"&gt;...&lt;/span&gt;
       &lt;span class="mf"&gt;...&lt;/span&gt;
       &lt;span class="cd"&gt;/**
        * go to ./solution.php
        */&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * @param $r
     * @param $c
     * @param $visited
     * @return bool
     */&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$visited&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="mf"&gt;...&lt;/span&gt;
       &lt;span class="mf"&gt;...&lt;/span&gt;
       &lt;span class="mf"&gt;...&lt;/span&gt;
       &lt;span class="cd"&gt;/**
        * go to ./solution.php
        */&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="nv"&gt;$solution&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$solution&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasValidPath&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]]);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$solution&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasValidPath&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$solution&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasValidPath&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]]);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"true"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"false"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Street Connections Array&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each street type, store which directions it connects to.
&lt;/li&gt;
&lt;li&gt;Example:&lt;/li&gt;
&lt;li&gt;Street 1 (left-right) → &lt;code&gt;[3, 1]&lt;/code&gt; (Left, Right)&lt;/li&gt;
&lt;li&gt;Street 2 (up-down) → &lt;code&gt;[0, 2]&lt;/code&gt; (Up, Down)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Opposite Directions Array&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For a given moving direction, the entering direction for the neighbor is opposite.
&lt;/li&gt;
&lt;li&gt;Example: If we go &lt;em&gt;right (1)&lt;/em&gt;, the neighbor must accept entry from &lt;em&gt;left (3)&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;DFS Logic&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If current cell is the destination → return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Mark current cell as visited.&lt;/li&gt;
&lt;li&gt;For each allowed direction of current street:

&lt;ul&gt;
&lt;li&gt;Compute neighbor coordinates.&lt;/li&gt;
&lt;li&gt;If neighbor inside bounds and not visited:

&lt;ul&gt;
&lt;li&gt;Check if neighbor’s connections include the required opposite direction.&lt;/li&gt;
&lt;li&gt;If yes, recursively DFS from neighbor.&lt;/li&gt;
&lt;li&gt;If DFS returns &lt;code&gt;true&lt;/code&gt;, propagate &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If all directions fail, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why DFS Works&lt;/strong&gt; Since the path is deterministic once you choose a valid direction (no cycles possible except back-and-forth which visited prevents), DFS reliably explores all possible connected routes.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m * n)&lt;/strong&gt;&lt;/em&gt;, Each cell is visited at most once, and at each cell, only a constant number of directions is checked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m * n)&lt;/strong&gt;&lt;/em&gt;, For the visited matrix and recursion stack in worst case (if the grid is fully traversable).&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>1559. Detect Cycles in 2D Grid</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 26 Apr 2026 17:45:41 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1559-detect-cycles-in-2d-grid-339n</link>
      <guid>https://dev.to/mdarifulhaque/1559-detect-cycles-in-2d-grid-339n</guid>
      <description>&lt;p&gt;1559. Detect Cycles in 2D Grid&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Depth-First Search&lt;/code&gt;, &lt;code&gt;Breadth-First Search&lt;/code&gt;, &lt;code&gt;Union-Find&lt;/code&gt;, &lt;code&gt;Matrix&lt;/code&gt;, &lt;code&gt;Biweekly Contest 33&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given a 2D array of characters &lt;code&gt;grid&lt;/code&gt; of size &lt;code&gt;m x n&lt;/code&gt;, you need to find if there exists any cycle consisting of the &lt;strong&gt;same value&lt;/strong&gt; in &lt;code&gt;grid&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A cycle is a path of &lt;strong&gt;length 4 or more&lt;/strong&gt; in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the &lt;strong&gt;same value&lt;/strong&gt; of the current cell.&lt;/p&gt;

&lt;p&gt;Also, you cannot move to the cell that you visited in your last move. For example, the cycle&lt;code&gt;(1, 1) -&amp;gt; (1, 2) -&amp;gt; (1, 1)&lt;/code&gt; is invalid because from &lt;code&gt;(1, 2)&lt;/code&gt; we visited &lt;code&gt;(1, 1)&lt;/code&gt; which was the last visited cell.&lt;/p&gt;

&lt;p&gt;Return &lt;code&gt;true&lt;/code&gt; if any cycle of the same value exists in &lt;code&gt;grid&lt;/code&gt;, otherwise, return &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [["a","a","a","a"],["a","b","b","a"],["a","b","b","a"],["a","a","a","a"]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are two valid cycles shown in different colors in the image below:
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyqkfqinspavr9l91oz5o.png" alt="11" width="225" height="163"&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.amazonaws.com%2Fuploads%2Farticles%2F1tbu3dp5sp42ykxsw0zs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tbu3dp5sp42ykxsw0zs.png" alt="22" width="236" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [["c","c","c","a"],["c","d","c","c"],["c","c","e","c"],["f","c","c","c"]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There is only one valid cycle highlighted in the image below:
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjl76mchwxa0g7rfdvzgo.png" alt="2" width="229" height="157"&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; grid = [["a","b","b"],["b","z","b"],["b","b","a"]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;m == grid.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n == grid[i].length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= m, n &amp;lt;= 500&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;grid&lt;/code&gt; consists only of lowercase English letters.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Keep track of the parent (previous position) to avoid considering an invalid path.&lt;/li&gt;
&lt;li&gt;Use DFS or BFS and keep track of visited cells to see if there is a cycle.&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 detect a cycle where all cells in the cycle have the same character, the cycle length is at least 4, and we cannot instantly go back to the parent cell.&lt;br&gt;
A cycle exists if during DFS/BFS we encounter a cell that is already visited and is &lt;strong&gt;not the parent cell&lt;/strong&gt; — because if it’s visited and not parent, then we’ve found a cycle.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;p&gt;We can solve this with &lt;strong&gt;DFS&lt;/strong&gt;, keeping track of the &lt;code&gt;parent&lt;/code&gt; of each cell in the traversal so we can avoid simply backtracking to the previous cell.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Loop through each cell, if not visited, start DFS.&lt;/li&gt;
&lt;li&gt;In DFS:

&lt;ul&gt;
&lt;li&gt;Mark current cell as visited.&lt;/li&gt;
&lt;li&gt;Explore all four neighbors with the &lt;strong&gt;same character&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If the neighbor is visited and is not the parent cell → cycle found.&lt;/li&gt;
&lt;li&gt;If neighbor not visited, recurse with current cell as parent.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If at any point we find a cycle, return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If no cycles found after all cells, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&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/001559-detect-cycles-in-2d-grid" rel="noopener noreferrer"&gt;1559. Detect Cycles in 2D Grid&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;containsCycle&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;containsCycle&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"e"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;containsCycle&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                         &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DFS function&lt;/strong&gt; Marks current cell as visited, stores its character, and checks all four directions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Neighbor validity&lt;/strong&gt; Ensures neighbor is within bounds and has the same character as the current cell.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cycle check&lt;/strong&gt; If neighbor is visited and is &lt;strong&gt;not&lt;/strong&gt; the parent → cycle exists → return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recursive exploration&lt;/strong&gt; If neighbor is unvisited, recursively call DFS. If any call returns &lt;code&gt;true&lt;/code&gt;, propagate result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main loop&lt;/strong&gt; For each unvisited cell, start DFS. If cycle found, return &lt;code&gt;true&lt;/code&gt; immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No cycle found&lt;/strong&gt; After traversing all cells, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m × n)&lt;/strong&gt;&lt;/em&gt;, Each cell is visited once in DFS (ignoring constant-time neighbor checks).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(m × n)&lt;/strong&gt;&lt;/em&gt;, For the &lt;code&gt;visited&lt;/code&gt; matrix and the recursion stack in worst case (e.g., entire grid same char forming a path).&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>3464. Maximize the Distance Between Points on a Square</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 25 Apr 2026 15:19:22 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3464-maximize-the-distance-between-points-on-a-square-5ced</link>
      <guid>https://dev.to/mdarifulhaque/3464-maximize-the-distance-between-points-on-a-square-5ced</guid>
      <description>&lt;p&gt;3464. Maximize the Distance Between Points on a Square&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;Array&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;, &lt;code&gt;Geometry&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Weekly Contest 438&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer &lt;code&gt;side&lt;/code&gt;, representing the edge length of a square with corners at &lt;code&gt;(0, 0)&lt;/code&gt;, &lt;code&gt;(0, side)&lt;/code&gt;, &lt;code&gt;(side, 0)&lt;/code&gt;, and &lt;code&gt;(side, side)&lt;/code&gt; on a Cartesian plane.&lt;/p&gt;

&lt;p&gt;You are also given a &lt;strong&gt;positive&lt;/strong&gt; integer &lt;code&gt;k&lt;/code&gt; and a 2D integer array &lt;code&gt;points&lt;/code&gt;, where &lt;code&gt;points[i] = [xi, yi]&lt;/code&gt; represents the coordinate of a point lying on the &lt;strong&gt;boundary&lt;/strong&gt; of the square.&lt;/p&gt;

&lt;p&gt;You need to select &lt;code&gt;k&lt;/code&gt; elements among &lt;code&gt;points&lt;/code&gt; such that the &lt;strong&gt;minimum&lt;/strong&gt; Manhattan distance between any two points is &lt;strong&gt;maximized&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Return the &lt;code&gt;maximum&lt;/code&gt; possible &lt;code&gt;minimum&lt;/code&gt; Manhattan distance between the selected &lt;code&gt;k&lt;/code&gt; points.&lt;/p&gt;

&lt;p&gt;The Manhattan Distance between two cells &lt;code&gt;(xᵢ, yᵢ)&lt;/code&gt; and &lt;code&gt;(xⱼ, yⱼ)&lt;/code&gt; is &lt;code&gt;|xᵢ - xⱼ| + |yᵢ - yⱼ|&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; side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4&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;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25adm852muhhd14bu9yc.png" alt="4080_example0_revised" width="200" height="200"&gt;
Select all four points.&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; side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrv9vsij6s519wqtdhh3.png" alt="4080_example1_revised" width="211" height="200"&gt;
Select the points &lt;code&gt;(0, 0)&lt;/code&gt;, &lt;code&gt;(2, 0)&lt;/code&gt;, &lt;code&gt;(2, 2)&lt;/code&gt;, and &lt;code&gt;(2, 1)&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; side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpjo4wxyxrgrh0qgkeg5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpjo4wxyxrgrh0qgkeg5s.png" alt="4080_example2_revised" width="219" height="200"&gt;&lt;/a&gt;&lt;br&gt;
Select the points &lt;code&gt;(0, 0)&lt;/code&gt;, &lt;code&gt;(0, 1)&lt;/code&gt;, &lt;code&gt;(0, 2)&lt;/code&gt;, &lt;code&gt;(1, 2)&lt;/code&gt;, and &lt;code&gt;(2, 2)&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; side = 40, points = [[0,14],[0,40],[32,0],[0,30],[34,40]], k = 4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 26&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= side &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;4 &amp;lt;= points.length &amp;lt;= min(4 * side, 15 * 10³)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;points[i] == [xi, yi]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The input is generated such that:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;points[i]&lt;/code&gt; lies on the boundary of the square.&lt;/li&gt;
&lt;li&gt;All &lt;code&gt;points[i]&lt;/code&gt; are &lt;strong&gt;unique&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;4 &amp;lt;= k &amp;lt;= min(25, points.length)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Can we use binary search for this problem?&lt;/li&gt;
&lt;li&gt;Think of the coordinates on a straight line in clockwise order.&lt;/li&gt;
&lt;li&gt;Binary search on the minimum Manhattan distance &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;During the binary search, for each coordinate, find the immediate next coordinate with distance &amp;gt;= &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Greedily select up to &lt;code&gt;k&lt;/code&gt; coordinates.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;This problem asks us to select &lt;code&gt;k&lt;/code&gt; points from a given set of points on the boundary of a square to &lt;strong&gt;maximize the minimum Manhattan distance&lt;/strong&gt; between any two selected points.&lt;br&gt;&lt;br&gt;
We solve it by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Transforming the problem into a 1D ordering&lt;/strong&gt; around the square perimeter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binary searching&lt;/strong&gt; on the possible minimum distance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Greedy checking&lt;/strong&gt; whether a given distance is feasible with at least &lt;code&gt;k&lt;/code&gt; points.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Binary Search on the answer&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The answer lies between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;side&lt;/code&gt; (max possible Manhattan dist on the boundary).
&lt;/li&gt;
&lt;li&gt;We binary search for the maximum &lt;code&gt;d&lt;/code&gt; such that we can pick &lt;code&gt;k&lt;/code&gt; points with all pairwise distances ≥ &lt;code&gt;d&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Flatten the square perimeter into a 1D sequence&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Points on the boundary are ordered clockwise starting from the left edge.
&lt;/li&gt;
&lt;li&gt;This converts perimeter positions into a linear coordinate for easier traversal.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Feasibility check for a given &lt;code&gt;d&lt;/code&gt;&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We greedily try to form the longest sequence of points where consecutive Manhattan distances are at least &lt;code&gt;d&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;If the longest sequence length ≥ &lt;code&gt;k&lt;/code&gt;, then &lt;code&gt;d&lt;/code&gt; is feasible.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Greedy sequence building&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We maintain sequences (segments) starting at earlier points, and for each new point, we try to extend the longest possible segment ending at it, ensuring the jump distance meets &lt;code&gt;d&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003464-maximize-the-distance-between-points-on-a-square" rel="noopener noreferrer"&gt;3464. Maximize the Distance Between Points on a Square&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 $side
 * @param Integer[][] $points
 * @param Integer $k
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxDistance&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;$side&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;$points&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Returns true if we can select k points such that the minimum Manhattan
 * distance between any two consecutive chosen points is at least d.
 *
 * @param array $ordered
 * @param int $k
 * @param int $d
 * @return bool
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;isValidDistance&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;$ordered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$d&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Returns the ordered points on the perimeter of a square of side length side,
 * starting from left, top, right, and bottom boundaries.
 *
 * @param int $side
 * @param array $points
 * @return array
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getOrderedPoints&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;$side&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;$points&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;maxDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                           &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="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;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="mi"&gt;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: 26&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;p&gt;&lt;strong&gt;Ordering the points&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split points by which side of the square they lie on: left, top, right, bottom.&lt;/li&gt;
&lt;li&gt;Sort each side appropriately to get clockwise order.&lt;/li&gt;
&lt;li&gt;Merge them: left (bottom to top), top (left to right), right (top to bottom), bottom (right to left).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Binary search range&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;low = 0&lt;/code&gt;, &lt;code&gt;high = side&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;While &lt;code&gt;low &amp;lt; high&lt;/code&gt;, test &lt;code&gt;mid = (low + high + 1) / 2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Feasibility check for distance &lt;code&gt;d&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with the first point as a sequence of length 1.&lt;/li&gt;
&lt;li&gt;For each new point &lt;code&gt;(x, y)&lt;/code&gt;, try to extend all existing sequences where the distance from that sequence’s end to &lt;code&gt;(x, y)&lt;/code&gt; ≥ &lt;code&gt;d&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose the longest extension.&lt;/li&gt;
&lt;li&gt;Track the maximum sequence length seen.&lt;/li&gt;
&lt;li&gt;If at any point we can form length ≥ &lt;code&gt;k&lt;/code&gt;, return true.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Manhattan distance in 1D&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since points are in order around the perimeter, the Manhattan distance between two points in this 1D flattened representation is simply the smaller of:

&lt;ul&gt;
&lt;li&gt;the direct distance along the perimeter,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;side*4 - direct distance&lt;/code&gt; (going the other way around).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The implementation uses absolute coordinate differences directly, so it works even if points wrap across the start/end.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Greedy choice&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For a candidate &lt;code&gt;d&lt;/code&gt;, we try to maximize the number of points we can pick with gaps ≥ &lt;code&gt;d&lt;/code&gt; without skipping too many points.&lt;/li&gt;
&lt;li&gt;This is like a longest path problem in a DAG where edges exist if distance ≥ &lt;code&gt;d&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Sorting: &lt;code&gt;O(n log n)&lt;/code&gt; where &lt;code&gt;n = len(points)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Binary search: &lt;code&gt;O(log side)&lt;/code&gt; iterations.&lt;/li&gt;
&lt;li&gt;Each feasibility check: &lt;code&gt;O(n²)&lt;/code&gt; in worst case due to linear scan over sequences.&lt;/li&gt;
&lt;li&gt;Total: &lt;code&gt;O(n² log side)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; for storing ordered points and sequence data.&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>2833. Furthest Point From Origin</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 24 Apr 2026 17:41:47 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2833-furthest-point-from-origin-4a43</link>
      <guid>https://dev.to/mdarifulhaque/2833-furthest-point-from-origin-4a43</guid>
      <description>&lt;p&gt;2833. Furthest Point From Origin&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Counting&lt;/code&gt;, &lt;code&gt;Weekly Contest 360&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a string &lt;code&gt;moves&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt; consisting only of characters &lt;code&gt;'L'&lt;/code&gt;, &lt;code&gt;'R'&lt;/code&gt;, and &lt;code&gt;'_'&lt;/code&gt;. The string represents your movement on a number line starting from the origin &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;iᵗʰ&lt;/code&gt; move, you can choose one of the following directions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;move to the left if &lt;code&gt;moves[i] = 'L'&lt;/code&gt; or &lt;code&gt;moves[i] = '_'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;move to the right if &lt;code&gt;moves[i] = 'R'&lt;/code&gt; or &lt;code&gt;moves[i] = '_'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;distance from the origin&lt;/strong&gt; of the &lt;strong&gt;furthest&lt;/strong&gt; point you can get to after &lt;code&gt;n&lt;/code&gt; moves&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; moves = "L_RL__R"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".&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; moves = "&lt;em&gt;R&lt;/em&gt;&lt;em&gt;LL&lt;/em&gt;"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".&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; moves = "_______"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 7&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; moves = "R"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; moves = "_"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= moves.length == n &amp;lt;= 50&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;moves&lt;/code&gt; consists only of characters &lt;code&gt;'L'&lt;/code&gt;, &lt;code&gt;'R'&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;In an optimal answer, all occurrences of &lt;code&gt;'_’&lt;/code&gt; will be replaced with the same character.&lt;/li&gt;
&lt;li&gt;Replace all characters of &lt;code&gt;'_’&lt;/code&gt; with the character that occurs the most.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The goal is to find the furthest possible distance from the origin after processing a series of moves where &lt;code&gt;'L'&lt;/code&gt; and &lt;code&gt;'R'&lt;/code&gt; are fixed, and &lt;code&gt;'_'&lt;/code&gt; can be chosen as either &lt;code&gt;'L'&lt;/code&gt; or &lt;code&gt;'R'&lt;/code&gt;. The solution calculates the net position range based on fixed moves and then uses all underscores to maximize distance in one direction.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Count the number of &lt;code&gt;'L'&lt;/code&gt;, &lt;code&gt;'R'&lt;/code&gt;, and &lt;code&gt;'_'&lt;/code&gt; in the input string.&lt;/li&gt;
&lt;li&gt;Compute the net position after all fixed moves: &lt;code&gt;net = count('R') - count('L')&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The underscores can be used entirely as &lt;code&gt;'R'&lt;/code&gt; (to increase net position) or as &lt;code&gt;'L'&lt;/code&gt; (to decrease net position).&lt;/li&gt;
&lt;li&gt;Calculate the two possible final positions:

&lt;ul&gt;
&lt;li&gt;All underscores as &lt;code&gt;'R'&lt;/code&gt;: &lt;code&gt;maxPos = net + underscoreCount&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;All underscores as &lt;code&gt;'L'&lt;/code&gt;: &lt;code&gt;minPos = net - underscoreCount&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The answer is the maximum absolute value of these two positions, representing the furthest distance from origin.&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/002833-furthest-point-from-origin" rel="noopener noreferrer"&gt;2833. Furthest Point From Origin&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 $moves
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;furthestDistanceFromOrigin&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;$moves&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;furthestDistanceFromOrigin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"L_RL__R"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;furthestDistanceFromOrigin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"_R__LL_"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;furthestDistanceFromOrigin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"_______"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// Output: 7&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;furthestDistanceFromOrigin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"R"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;furthestDistanceFromOrigin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"_"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// Output: 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;p&gt;&lt;strong&gt;Step 1 — Count moves:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$r = substr_count($moves, 'R')&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$l = substr_count($moves, 'L')&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$underscore = substr_count($moves, '_')&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This gives us the fixed net movement and flexibility.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 2 — Net from fixed moves:&lt;/strong&gt; &lt;code&gt;$net = $r - $l&lt;/code&gt; represents the position after fixed moves only.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 3 — Two extreme final positions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If all &lt;code&gt;'_'&lt;/code&gt; move right: &lt;code&gt;$maxPos = $net + $underscore&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If all &lt;code&gt;'_'&lt;/code&gt; move left: &lt;code&gt;$minPos = $net - $underscore&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 4 — Furthest point:&lt;/strong&gt; The furthest distance from origin is the larger absolute value of &lt;code&gt;$maxPos&lt;/code&gt; and &lt;code&gt;$minPos&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, Counting takes linear time based on string length.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt;, Only a few integer variables are used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Algorithmic efficiency:&lt;/strong&gt; Optimal — no sorting or extra data structures needed.&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>2615. Sum of Distances</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 23 Apr 2026 14:35:33 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2615-sum-of-distances-549i</link>
      <guid>https://dev.to/mdarifulhaque/2615-sum-of-distances-549i</guid>
      <description>&lt;p&gt;2615. Sum of Distances&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;Prefix Sum&lt;/code&gt;, &lt;code&gt;Weekly Contest 340&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a &lt;strong&gt;0-indexed&lt;/strong&gt; integer array &lt;code&gt;nums&lt;/code&gt;. There exists an array &lt;code&gt;arr&lt;/code&gt; of length &lt;code&gt;nums.length&lt;/code&gt;, where &lt;code&gt;arr[i]&lt;/code&gt; is the sum of &lt;code&gt;|i - j|&lt;/code&gt; over all &lt;code&gt;j&lt;/code&gt; such that &lt;code&gt;nums[j] == nums[i]&lt;/code&gt; and &lt;code&gt;j != i&lt;/code&gt;. If there is no such &lt;code&gt;j&lt;/code&gt;, set &lt;code&gt;arr[i]&lt;/code&gt; to be &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the array &lt;code&gt;arr&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;When &lt;code&gt;i = 0&lt;/code&gt;, &lt;code&gt;nums[0] == nums[2]&lt;/code&gt; and &lt;code&gt;nums[0] == nums[3]&lt;/code&gt;. Therefore, &lt;code&gt;arr[0] = |0 - 2| + |0 - 3| = 5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;i = 1&lt;/code&gt;, &lt;code&gt;arr[1] = 0&lt;/code&gt; because there is no other index with value &lt;code&gt;3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;i = 2&lt;/code&gt;, &lt;code&gt;nums[2] == nums[0]&lt;/code&gt; and &lt;code&gt;nums[2] == nums[3]&lt;/code&gt;. Therefore, &lt;code&gt;arr[2] = |2 - 0| + |2 - 3| = 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;i = 3&lt;/code&gt;, &lt;code&gt;nums[3] == nums[0]&lt;/code&gt; and &lt;code&gt;nums[3] == nums[2]&lt;/code&gt;. Therefore, &lt;code&gt;arr[3] = |3 - 0| + |3 - 2| = 4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;i = 4&lt;/code&gt;, &lt;code&gt;arr[4] = 0&lt;/code&gt; because there is no other index with value &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [0,5,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [0,0,0]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since each element in nums is distinct, &lt;code&gt;arr[i] = 0&lt;/code&gt; for all &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;p&gt;Note: This question is the same as &lt;a href="https://leetcode.com/problems/intervals-between-identical-elements/description/" rel="noopener noreferrer"&gt;2121: Intervals Between Identical Elements&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;Can we use the prefix sum here?&lt;/li&gt;
&lt;li&gt;For each number &lt;code&gt;x&lt;/code&gt;, collect all the indices where &lt;code&gt;x&lt;/code&gt; occurs, and calculate the prefix sum of the array.&lt;/li&gt;
&lt;li&gt;For each occurrence of &lt;code&gt;x&lt;/code&gt;, the indices to the right will be regular subtraction while the indices to the left will be reversed subtraction.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The solution computes, for each index &lt;code&gt;i&lt;/code&gt;, the sum of absolute differences &lt;code&gt;|i - j|&lt;/code&gt; for all &lt;code&gt;j ≠ i&lt;/code&gt; where &lt;code&gt;nums[j] == nums[i]&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
It groups indices by value, then uses &lt;strong&gt;prefix sums&lt;/strong&gt; to efficiently calculate the total distance to all other same-valued indices in &lt;strong&gt;O(n)&lt;/strong&gt; time.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Group indices by value&lt;/strong&gt; — create a mapping from each unique number to a list of indices where it appears.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For each group of indices&lt;/strong&gt; (already sorted by the order they appear in &lt;code&gt;nums&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compute the &lt;strong&gt;prefix sums&lt;/strong&gt; of the indices.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For each index &lt;code&gt;pos&lt;/code&gt; at position &lt;code&gt;j&lt;/code&gt; in the group:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split the group into &lt;strong&gt;left&lt;/strong&gt; and &lt;strong&gt;right&lt;/strong&gt; parts relative to &lt;code&gt;pos&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use the formula:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr[pos] = (pos * leftCount - leftSum) + (rightSum - pos * rightCount)
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;leftCount = j&lt;/code&gt;, &lt;code&gt;rightCount = k - j - 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;leftSum&lt;/code&gt; = sum of indices to the left of &lt;code&gt;pos&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rightSum&lt;/code&gt; = sum of indices to the right of &lt;code&gt;pos&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This avoids an &lt;strong&gt;O(k²)&lt;/strong&gt; per-group computation.&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/002615-sum-of-distances" rel="noopener noreferrer"&gt;2615. Sum of Distances&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;distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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: [5,0,3,4,0]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// Output: [0,0,0]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 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;p&gt;&lt;strong&gt;Why grouping?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The problem only requires distances to indices with the same value. Grouping allows us to process all same-value indices together.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Why prefix sums?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
If we have sorted indices &lt;code&gt;[i₀, i₁, ..., iₖ₋₁]&lt;/code&gt; for a value, then for index &lt;code&gt;iⱼ&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distance to all left indices = &lt;code&gt;iⱼ * j - sum(i₀..iⱼ₋₁)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Distance to all right indices = &lt;code&gt;sum(iⱼ₊₁..iₖ₋₁) - iⱼ * (k - j - 1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Prefix sums let us compute &lt;code&gt;sum(i₀..iⱼ₋₁)&lt;/code&gt; and &lt;code&gt;sum(iⱼ₊₁..iₖ₋₁)&lt;/code&gt; in &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Why efficient?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
Each index is processed once, and prefix sums are computed in &lt;strong&gt;O(k)&lt;/strong&gt; per group, so total &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — We iterate through the array to group indices, then process each group with prefix sums. Each index’s distance is computed in constant time after prefix sums.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — For storing the groups of indices and prefix sums.&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>2452. Words Within Two Edits of Dictionary</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 22 Apr 2026 16:51:19 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2452-words-within-two-edits-of-dictionary-3d6h</link>
      <guid>https://dev.to/mdarifulhaque/2452-words-within-two-edits-of-dictionary-3d6h</guid>
      <description>&lt;p&gt;2452. Words Within Two Edits of Dictionary&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;String&lt;/code&gt;, &lt;code&gt;Trie&lt;/code&gt;, &lt;code&gt;Biweekly Contest 90&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two string arrays, &lt;code&gt;queries&lt;/code&gt; and &lt;code&gt;dictionary&lt;/code&gt;. All words in each array comprise of lowercase English letters and have the same length.&lt;/p&gt;

&lt;p&gt;In one &lt;strong&gt;edit&lt;/strong&gt; you can take a word from &lt;code&gt;queries&lt;/code&gt;, and change any letter in it to any other letter. Find all words from &lt;code&gt;queries&lt;/code&gt; that, after a &lt;strong&gt;maximum&lt;/strong&gt; of two edits, equal some word from &lt;code&gt;dictionary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;a list of all words from &lt;code&gt;queries&lt;/code&gt;, that match with some word from &lt;code&gt;dictionary&lt;/code&gt; after a maximum of &lt;strong&gt;two edits&lt;/strong&gt;&lt;/em&gt;. Return &lt;em&gt;the words in the &lt;strong&gt;same order&lt;/strong&gt; they appear in &lt;code&gt;queries&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; ["word","note","wood"]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".&lt;/li&gt;
&lt;li&gt;Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".&lt;/li&gt;
&lt;li&gt;It would take more than 2 edits for "ants" to equal a dictionary word.&lt;/li&gt;
&lt;li&gt;"wood" can remain unchanged (0 edits) and match the corresponding dictionary word.&lt;/li&gt;
&lt;li&gt;Thus, we return ["word","note","wood"].&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; queries = ["yes"], dictionary = ["not"]&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; Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.&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;= queries.length, dictionary.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n == queries[i].length == dictionary[j].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;All &lt;code&gt;queries[i]&lt;/code&gt; and &lt;code&gt;dictionary[j]&lt;/code&gt; are composed of lowercase English letters.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Try brute-forcing the problem.&lt;/li&gt;
&lt;li&gt;For each word in queries, try comparing to each word in dictionary.&lt;/li&gt;
&lt;li&gt;If there is a maximum of two edit differences, the word should be present in answer.&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 identifying which words from &lt;code&gt;queries&lt;/code&gt; can be transformed into &lt;strong&gt;any&lt;/strong&gt; word from &lt;code&gt;dictionary&lt;/code&gt; with &lt;strong&gt;at most two letter changes&lt;/strong&gt; (edits). The brute-force approach compares each query word with each dictionary word, counting character mismatches, and stops early if more than two mismatches are found.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Brute-force comparison&lt;/strong&gt; between each query and each dictionary word.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Character-by-character mismatch counting&lt;/strong&gt; for each pair.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early termination&lt;/strong&gt; if mismatch count exceeds 2.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Order preservation&lt;/strong&gt; by processing queries in their original order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002452-words-within-two-edits-of-dictionary" rel="noopener noreferrer"&gt;2452. Words Within Two Edits of Dictionary&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[] $queries
 * @param String[] $dictionary
 * @return String[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;twoEditWords&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$queries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$dictionary&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;twoEditWords&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"word"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"note"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"ants"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"wood"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"wood"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"joke"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"moat"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// Output: ["word","note","wood"]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;twoEditWords&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"yes"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"not"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                         &lt;span class="c1"&gt;// Output: []&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1 — Initialize result array&lt;/strong&gt;: An empty array &lt;code&gt;$result&lt;/code&gt; is created to store query words that meet the condition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2 — Iterate through queries&lt;/strong&gt;: For each &lt;code&gt;$query&lt;/code&gt;, assume initially it’s not matched (&lt;code&gt;$matched = false&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3 — Compare with dictionary&lt;/strong&gt;: Loop through &lt;code&gt;$dictionary&lt;/code&gt;. For each &lt;code&gt;$dictWord&lt;/code&gt;, compare characters with &lt;code&gt;$query&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 4 — Count differences&lt;/strong&gt;: Track &lt;code&gt;$diffCount&lt;/code&gt; for positions where characters differ.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 5 — Early stop&lt;/strong&gt;: If &lt;code&gt;$diffCount &amp;gt; 2&lt;/code&gt;, break out of the inner loop for that dictionary word.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 6 — Match found&lt;/strong&gt;: If &lt;code&gt;$diffCount &amp;lt;= 2&lt;/code&gt;, set &lt;code&gt;$matched = true&lt;/code&gt; and break out of the dictionary loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 7 — Add to result&lt;/strong&gt;: If &lt;code&gt;$matched&lt;/code&gt;, append &lt;code&gt;$query&lt;/code&gt; to &lt;code&gt;$result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 8 — Return result&lt;/strong&gt;: After processing all queries, return &lt;code&gt;$result&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;O(q . d . n)&lt;/em&gt;&lt;/strong&gt; where &lt;strong&gt;&lt;em&gt;q&lt;/em&gt;&lt;/strong&gt; = number of queries, &lt;strong&gt;&lt;em&gt;d&lt;/em&gt;&lt;/strong&gt; = size of dictionary, &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt; = length of each word.&lt;/li&gt;
&lt;li&gt;In worst case, each comparison checks all &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt; characters (up to 100), and &lt;strong&gt;&lt;em&gt;q, d ≤ 100&lt;/em&gt;&lt;/strong&gt;, so at most &lt;strong&gt;&lt;em&gt;100 . 100 . 100 = 10⁶&lt;/em&gt;&lt;/strong&gt; operations, which is acceptable.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;O(q)&lt;/em&gt;&lt;/strong&gt; for the result array (excluding input storage).&lt;/li&gt;
&lt;li&gt;No additional data structures proportional to input size beyond the result.&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>1722. Minimize Hamming Distance After Swap Operations</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 21 Apr 2026 16:36:08 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1722-minimize-hamming-distance-after-swap-operations-1545</link>
      <guid>https://dev.to/mdarifulhaque/1722-minimize-hamming-distance-after-swap-operations-1545</guid>
      <description>&lt;p&gt;1722. Minimize Hamming Distance After Swap Operations&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Depth-First Search&lt;/code&gt;, &lt;code&gt;Union-Find&lt;/code&gt;, &lt;code&gt;Weekly Contest 223&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two integer arrays, &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt;, both of length &lt;code&gt;n&lt;/code&gt;. You are also given an array &lt;code&gt;allowedSwaps&lt;/code&gt; where each &lt;code&gt;allowedSwaps[i] = [aᵢ, bᵢ]&lt;/code&gt; indicates that you are allowed to swap the elements at index &lt;code&gt;aᵢ&lt;/code&gt; and index &lt;code&gt;bᵢ&lt;/code&gt; (&lt;strong&gt;0-indexed&lt;/strong&gt;) of array &lt;code&gt;source&lt;/code&gt;. Note that you can swap elements at a specific pair of indices &lt;strong&gt;multiple&lt;/strong&gt; times and in any order.&lt;/p&gt;

&lt;p&gt;The Hamming distance of two arrays of the same length, &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt;, is the number of positions where the elements are different. Formally, it is the number of indices &lt;code&gt;i&lt;/code&gt; for &lt;code&gt;0 &amp;lt;= i &amp;lt;= n-1&lt;/code&gt; where &lt;code&gt;source[i] != target[i]&lt;/code&gt; (&lt;strong&gt;0-indexed&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;minimum Hamming distance&lt;/strong&gt; of &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; after performing &lt;strong&gt;any&lt;/strong&gt; amount of swap operations on array &lt;code&gt;source&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;source can be transformed the following way:&lt;/li&gt;
&lt;li&gt;Swap indices 0 and 1: source = [&lt;strong&gt;2&lt;/strong&gt;,&lt;strong&gt;1&lt;/strong&gt;,3,4]&lt;/li&gt;
&lt;li&gt;Swap indices 2 and 3: source = [2,1,&lt;strong&gt;4&lt;/strong&gt;,&lt;strong&gt;3&lt;/strong&gt;]&lt;/li&gt;
&lt;li&gt;The Hamming distance of source and target is 1 as they differ in 1 position: index 3.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []&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 no allowed swaps.&lt;/li&gt;
&lt;li&gt;The Hamming distance of source and target is 2 as they differ in 2 positions: index 1 and index 2.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n == source.length == target.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= source[i], target[i] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= allowedSwaps.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;allowedSwaps[i].length == 2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= aᵢ, bᵢ &amp;lt;= n - 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;aᵢ != bᵢ&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 source array can be imagined as a graph where each index is a node and each &lt;code&gt;allowedSwaps[i]&lt;/code&gt; is an edge.&lt;/li&gt;
&lt;li&gt;Nodes within the same component can be freely swapped with each other.&lt;/li&gt;
&lt;li&gt;For each component, find the number of common elements. The elements that are not in common will contribute to the total Hamming distance.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;This problem is about minimizing the Hamming distance between two arrays by swapping elements in &lt;code&gt;source&lt;/code&gt; using allowed swaps.&lt;br&gt;&lt;br&gt;
The key insight is that indices connected through allowed swaps form components where any permutation of values is possible.&lt;br&gt;&lt;br&gt;
Thus, within each component, we can rearrange &lt;code&gt;source&lt;/code&gt; values freely to match as many &lt;code&gt;target&lt;/code&gt; values as possible.&lt;br&gt;&lt;br&gt;
The solution uses a &lt;strong&gt;Union-Find (Disjoint Set Union)&lt;/strong&gt; data structure to group indices into connected components, then counts mismatches per component.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Union-Find (DSU)&lt;/strong&gt; is used to group indices that are directly or indirectly connected via &lt;code&gt;allowedSwaps&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each component, collect all &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;Count frequency of each value in &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; within the component.&lt;/li&gt;
&lt;li&gt;For each value, the number of matches = &lt;code&gt;min(source_count, target_count)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Hamming distance for the component = &lt;code&gt;component_size - matches&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sum over all components to get total minimum Hamming distance.&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/001722-minimize-hamming-distance-after-swap-operations" rel="noopener noreferrer"&gt;1722. Minimize Hamming Distance After Swap Operations&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[] $source
 * @param Integer[] $target
 * @param Integer[][] $allowedSwaps
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minimumHammingDistance&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;$source&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;$target&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;$allowedSwaps&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;minimumHammingDistance&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="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;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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumHammingDistance&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="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="p"&gt;[])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;minimumHammingDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="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="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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;p&gt;&lt;strong&gt;Graph interpretation&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each index is a node, each allowed swap is an undirected edge.
&lt;/li&gt;
&lt;li&gt;Nodes in the same connected component can be rearranged arbitrarily.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Union-Find&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merges indices connected by swaps.
&lt;/li&gt;
&lt;li&gt;After processing all swaps, &lt;code&gt;find(i)&lt;/code&gt; gives the root of the component containing &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Group indices&lt;/strong&gt;: Use a map from root → list of indices.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Count matches per component&lt;/strong&gt;: Since we can permute freely, the best we can do is match as many &lt;code&gt;target&lt;/code&gt; values as possible from the multiset of &lt;code&gt;source&lt;/code&gt; values in that component.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Compute mismatches&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;component_size - matches&lt;/code&gt; gives the number of positions that cannot be fixed in that component.
&lt;/li&gt;
&lt;li&gt;Summing over all components gives the minimum possible Hamming distance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Building DSU: &lt;code&gt;O(α(n) * m)&lt;/code&gt; where &lt;code&gt;m&lt;/code&gt; = &lt;code&gt;len(allowedSwaps)&lt;/code&gt; and &lt;code&gt;α&lt;/code&gt; is inverse Ackermann (near constant).&lt;/li&gt;
&lt;li&gt;Grouping indices: &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Counting frequencies per component: total &lt;code&gt;O(n)&lt;/code&gt; across all components.&lt;/li&gt;
&lt;li&gt;Overall: &lt;strong&gt;O(n + m * α(n))&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;DSU arrays: &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Component grouping: &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Frequency maps per component: total &lt;code&gt;O(n)&lt;/code&gt; across all components.&lt;/li&gt;
&lt;li&gt;Overall: &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2078. Two Furthest Houses With Different Colors</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 20 Apr 2026 17:31:14 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2078-two-furthest-houses-with-different-colors-5b28</link>
      <guid>https://dev.to/mdarifulhaque/2078-two-furthest-houses-with-different-colors-5b28</guid>
      <description>&lt;p&gt;2078. Two Furthest Houses With Different Colors&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;Greedy&lt;/code&gt;, &lt;code&gt;Weekly Contest 268&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are &lt;code&gt;n&lt;/code&gt; houses evenly lined up on the street, and each house is beautifully painted. You are given a &lt;strong&gt;0-indexed&lt;/strong&gt; integer array &lt;code&gt;colors&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt;, where &lt;code&gt;colors[i]&lt;/code&gt; represents the color of the &lt;code&gt;iᵗʰ&lt;/code&gt; house.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;maximum&lt;/strong&gt; distance between &lt;strong&gt;two&lt;/strong&gt; houses with &lt;strong&gt;different&lt;/strong&gt; colors.&lt;/p&gt;

&lt;p&gt;The distance between the &lt;code&gt;iᵗʰ&lt;/code&gt; and &lt;code&gt;jᵗʰ&lt;/code&gt; houses is &lt;code&gt;abs(i - j)&lt;/code&gt;, where &lt;code&gt;abs(x)&lt;/code&gt; is the absolute value of &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

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

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

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

&lt;ul&gt;
&lt;li&gt;In the above image, color 1 is blue, and color 6 is red.&lt;/li&gt;
&lt;li&gt;The furthest two houses with different colors are house 0 and house 3.&lt;/li&gt;
&lt;li&gt;House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.&lt;/li&gt;
&lt;li&gt;Note that houses 3 and 6 can also produce the optimal answer.&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.amazonaws.com%2Fuploads%2Farticles%2Foosxb3wu5ken4ameeo29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foosxb3wu5ken4ameeo29.png" alt="eg2" width="426" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.&lt;/li&gt;
&lt;li&gt;The furthest two houses with different colors are house 0 and house 4.&lt;/li&gt;
&lt;li&gt;House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.&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; colors = [&lt;strong&gt;0&lt;/strong&gt;,&lt;strong&gt;1&lt;/strong&gt;]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;The furthest two houses with different colors are house 0 and house 1.&lt;/li&gt;
&lt;li&gt;House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.&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;n == colors.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= n &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= colors[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Test data are generated such that &lt;strong&gt;at least&lt;/strong&gt; two houses have different colors.&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 constraints are small. Can you try the combination of every two houses?&lt;/li&gt;
&lt;li&gt;Greedily, the maximum distance will come from either the pair of the leftmost house and possibly some house on the right with a different color, or the pair of the rightmost house and possibly some house on the left with a different color.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The problem asks for the &lt;strong&gt;maximum distance&lt;/strong&gt; between two houses with different colors.&lt;br&gt;&lt;br&gt;
A brute-force solution (checking all pairs) would be &lt;em&gt;&lt;strong&gt;O(n²)&lt;/strong&gt;&lt;/em&gt;, but an &lt;strong&gt;&lt;em&gt;O(n)&lt;/em&gt; greedy approach&lt;/strong&gt; works by focusing only on the leftmost and rightmost houses.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Observation&lt;/strong&gt;: The maximum distance will always involve either the leftmost house (index &lt;code&gt;0&lt;/code&gt;) paired with the farthest house to the right that has a different color, or the rightmost house (index &lt;code&gt;n-1&lt;/code&gt;) paired with the farthest house to the left that has a different color.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why greedy works&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If the leftmost and rightmost houses have different colors, the distance &lt;code&gt;n-1&lt;/code&gt; is already maximal.&lt;/li&gt;
&lt;li&gt;If they have the same color, the farthest house with a different color from the leftmost house must be somewhere on the right side, and from the rightmost house on the left side.&lt;/li&gt;
&lt;li&gt;The optimal distance will be one of these two candidates, because any interior pair would have a smaller span than extending to one of the ends.&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/002078-two-furthest-houses-with-different-colors" rel="noopener noreferrer"&gt;2078. Two Furthest Houses With Different Colors&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[] $colors
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxDistance&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;$colors&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;maxDistance&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;6&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: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;maxDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="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;Case 1 – Start from leftmost house&lt;/strong&gt;: Scan from the right end toward the left, stop at the first house with a color different from &lt;code&gt;colors[0]&lt;/code&gt;. Compute distance and update &lt;code&gt;maxDist&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 2 – Start from rightmost house&lt;/strong&gt;: Scan from the left end toward the right, stop at the first house with a color different from &lt;code&gt;colors[n-1]&lt;/code&gt;. Compute distance and update &lt;code&gt;maxDist&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why both cases are needed&lt;/strong&gt;: The farthest different-colored house from the left end might be closer to the right end than the farthest different-colored house from the right end. We must consider both to cover the true maximum.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return the maximum of the two candidates&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; – at most two linear scans.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; – only a few variables used.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1855. Maximum Distance Between a Pair of Values</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 19 Apr 2026 14:04:18 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/1855-maximum-distance-between-a-pair-of-values-4n6o</link>
      <guid>https://dev.to/mdarifulhaque/1855-maximum-distance-between-a-pair-of-values-4n6o</guid>
      <description>&lt;p&gt;1855. Maximum Distance Between a Pair of Values&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;Two Pointers&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;, &lt;code&gt;Weekly Contest 240&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two &lt;strong&gt;non-increasing 0-indexed&lt;/strong&gt; integer arrays &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A pair of indices &lt;code&gt;(i, j)&lt;/code&gt;, where &lt;code&gt;0 &amp;lt;= i &amp;lt; nums1.length&lt;/code&gt; and &lt;code&gt;0 &amp;lt;= j &amp;lt; nums2.length&lt;/code&gt;, is &lt;strong&gt;valid&lt;/strong&gt; if both &lt;code&gt;i &amp;lt;= j&lt;/code&gt; and &lt;code&gt;nums1[i] &amp;lt;= nums2[j]&lt;/code&gt;. The &lt;strong&gt;distance&lt;/strong&gt; of the pair is &lt;code&gt;j - i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum distance&lt;/strong&gt; of any &lt;strong&gt;valid&lt;/strong&gt; pair &lt;code&gt;(i, j)&lt;/code&gt;. If there are no valid pairs, return &lt;code&gt;0&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;An array &lt;code&gt;arr&lt;/code&gt; is &lt;strong&gt;non-increasing&lt;/strong&gt; if &lt;code&gt;arr[i-1] &amp;gt;= arr[i]&lt;/code&gt; for every &lt;code&gt;1 &amp;lt;= i &amp;lt; arr.length&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; nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,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;The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).&lt;/li&gt;
&lt;li&gt;The maximum distance is 2 with pair (2,4).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;The valid pairs are (0,0), (0,1), and (1,1).&lt;/li&gt;
&lt;li&gt;The maximum distance is 1 with pair (0,1).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]&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 valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).&lt;/li&gt;
&lt;li&gt;The maximum distance is 2 with pair (2,4).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums1.length, nums2.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums1[i], nums2[j] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt; are &lt;strong&gt;non-increasing&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Since both arrays are sorted in a non-increasing way this means that for each value in the first array. We can find the farthest value smaller than it using binary search.&lt;/li&gt;
&lt;li&gt;There is another solution using a two pointers approach since the first array is non-increasing the farthest &lt;code&gt;j&lt;/code&gt; such that &lt;code&gt;nums2[j] ≥ nums1[i]&lt;/code&gt; is at least as far as the farthest &lt;code&gt;j&lt;/code&gt; such that &lt;code&gt;nums2[j] ≥ nums1[i-1]&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 need to find the maximum &lt;code&gt;j - i&lt;/code&gt; such that &lt;code&gt;i &amp;lt;= j&lt;/code&gt; and &lt;code&gt;nums1[i] &amp;lt;= nums2[j]&lt;/code&gt;, given both arrays are &lt;strong&gt;non-increasing&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
The solution uses a &lt;strong&gt;two-pointer technique&lt;/strong&gt; to efficiently find the farthest valid &lt;code&gt;j&lt;/code&gt; for each &lt;code&gt;i&lt;/code&gt;, leveraging the sorted property to avoid nested loops.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Two-pointer traversal&lt;/strong&gt;: One pointer &lt;code&gt;i&lt;/code&gt; for &lt;code&gt;nums1&lt;/code&gt;, one pointer &lt;code&gt;j&lt;/code&gt; for &lt;code&gt;nums2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Greedy expansion of &lt;code&gt;j&lt;/code&gt;&lt;/strong&gt;: If &lt;code&gt;nums1[i] &amp;lt;= nums2[j]&lt;/code&gt;, we have a valid pair, so we try to increase &lt;code&gt;j&lt;/code&gt; to maximize distance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adjusting &lt;code&gt;i&lt;/code&gt; when condition fails&lt;/strong&gt;: If &lt;code&gt;nums1[i] &amp;gt; nums2[j]&lt;/code&gt;, we move &lt;code&gt;i&lt;/code&gt; forward (since arrays are non-increasing, &lt;code&gt;nums1[i]&lt;/code&gt; will decrease, possibly satisfying condition later).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintain &lt;code&gt;j &amp;gt;= i&lt;/code&gt;&lt;/strong&gt;: When moving &lt;code&gt;i&lt;/code&gt; forward, we ensure &lt;code&gt;j&lt;/code&gt; is at least &lt;code&gt;i&lt;/code&gt; to keep &lt;code&gt;j - i&lt;/code&gt; non-negative.&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/001855-maximum-distance-between-a-pair-of-values" rel="noopener noreferrer"&gt;1855. Maximum Distance Between a Pair of Values&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums1
 * @param Integer[] $nums2
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;maxDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums2&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;maxDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;maxDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;maxDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 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;Start with &lt;code&gt;i = 0&lt;/code&gt;, &lt;code&gt;j = 0&lt;/code&gt;, &lt;code&gt;ans = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;While &lt;code&gt;i &amp;lt; n&lt;/code&gt; and &lt;code&gt;j &amp;lt; m&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If &lt;code&gt;nums1[i] &amp;lt;= nums2[j]&lt;/code&gt;&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;It's a valid pair, so update &lt;code&gt;ans = max(ans, j - i)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Move &lt;code&gt;j&lt;/code&gt; forward to try to get a larger &lt;code&gt;j&lt;/code&gt; for same &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Else (&lt;code&gt;nums1[i] &amp;gt; nums2[j]&lt;/code&gt;)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Condition fails, so move &lt;code&gt;i&lt;/code&gt; forward (since &lt;code&gt;nums1[i]&lt;/code&gt; will decrease, may become &lt;code&gt;&amp;lt;=&lt;/code&gt; later).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;j &amp;lt; i&lt;/code&gt;, set &lt;code&gt;j = i&lt;/code&gt; to maintain &lt;code&gt;j &amp;gt;= i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;Return &lt;code&gt;ans&lt;/code&gt;.&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n + m)&lt;/strong&gt;&lt;/em&gt;, Each pointer moves at most &lt;code&gt;n + m&lt;/code&gt; times.&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>3783. Mirror Distance of an Integer</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 18 Apr 2026 14:32:03 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3783-mirror-distance-of-an-integer-4aa</link>
      <guid>https://dev.to/mdarifulhaque/3783-mirror-distance-of-an-integer-4aa</guid>
      <description>&lt;p&gt;3783. Mirror Distance of an Integer&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Weekly Contest 481&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;Define its &lt;strong&gt;mirror distance&lt;/strong&gt; as: &lt;code&gt;abs(n - reverse(n))&lt;/code&gt; where &lt;code&gt;reverse(n)&lt;/code&gt; is the integer formed by reversing the digits of &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return an integer denoting the mirror distance of &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;abs(x)&lt;/code&gt; denotes the absolute value of &lt;code&gt;x&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; n = 25&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 27&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;reverse(25) = 52&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thus, the answer is &lt;code&gt;abs(25 - 52) = 27&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 = 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 9&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;reverse(10) = 01&lt;/code&gt; which is 1.&lt;/li&gt;
&lt;li&gt;Thus, the answer is &lt;code&gt;abs(10 - 1) = 9&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 7&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;ul&gt;
&lt;li&gt;
&lt;code&gt;reverse(7) = 7&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thus, the answer is &lt;code&gt;abs(7 - 7) = 0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; n = 100&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 99&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n &amp;lt;= 10⁹&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;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 function calculates the &lt;strong&gt;mirror distance&lt;/strong&gt; of an integer by reversing its digits, then returning the absolute difference between the original number and its reverse.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Convert the integer to its digit-reversed form without converting to a string.&lt;/li&gt;
&lt;li&gt;Use a loop to extract digits from the original number and build the reversed number.&lt;/li&gt;
&lt;li&gt;Return the absolute difference between the original and reversed 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/003783-mirror-distance-of-an-integer" rel="noopener noreferrer"&gt;3783. Mirror Distance of an Integer&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;mirrorDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 27&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;mirrorDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 9&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;mirrorDistance&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: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;mirrorDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 99&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;Reverse the digits&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Repeatedly take the last digit of &lt;code&gt;n&lt;/code&gt; using &lt;code&gt;n % 10&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Append it to &lt;code&gt;reversed&lt;/code&gt; by multiplying &lt;code&gt;reversed&lt;/code&gt; by 10 and adding the digit.&lt;/li&gt;
&lt;li&gt;Remove the last digit from &lt;code&gt;n&lt;/code&gt; using integer division by 10.&lt;/li&gt;
&lt;li&gt;Continue until &lt;code&gt;n&lt;/code&gt; becomes 0.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Handle leading zeros&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The reversal process automatically discards leading zeros because they don’t contribute to the integer value (e.g., &lt;code&gt;10&lt;/code&gt; → &lt;code&gt;1&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Compute mirror distance&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;abs(original - reversed)&lt;/code&gt; to get the positive difference.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(d)&lt;/strong&gt;&lt;/em&gt; where d is the number of digits in &lt;code&gt;n&lt;/code&gt; (at most 10, since n ≤ 10⁹).&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>3761. Minimum Absolute Distance Between Mirror Pairs</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 17 Apr 2026 16:36:06 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3761-minimum-absolute-distance-between-mirror-pairs-52n7</link>
      <guid>https://dev.to/mdarifulhaque/3761-minimum-absolute-distance-between-mirror-pairs-52n7</guid>
      <description>&lt;p&gt;3761. Minimum Absolute Distance Between Mirror Pairs&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Weekly Contest 478&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;A &lt;strong&gt;mirror pair&lt;/strong&gt; is a pair of indices &lt;code&gt;(i, j)&lt;/code&gt; such that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0 &amp;lt;= i &amp;lt; j &amp;lt; nums.length&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reverse(nums[i]) == nums[j]&lt;/code&gt;, where &lt;code&gt;reverse(x)&lt;/code&gt; denotes the integer formed by reversing the digits of &lt;code&gt;x&lt;/code&gt;. Leading zeros are omitted after reversing, for example &lt;code&gt;reverse(120) = 21&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;strong&gt;minimum&lt;/strong&gt; absolute distance between the indices of any mirror pair. The absolute distance between indices &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt; is &lt;code&gt;abs(i - j)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If no mirror pair exists, return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [12,21,45,33,54]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The mirror pairs are:

&lt;ul&gt;
&lt;li&gt;(0, 1) since &lt;code&gt;reverse(nums[0]) = reverse(12) = 21 = nums[1]&lt;/code&gt;, giving an absolute distance &lt;code&gt;abs(0 - 1) = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;(2, 4) since &lt;code&gt;reverse(nums[2]) = reverse(45) = 54 = nums[4]&lt;/code&gt;, giving an absolute distance &lt;code&gt;abs(2 - 4) = 2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The minimum absolute distance among all pairs is 1.&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 = [120,21]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;There is only one mirror pair (0, 1) since &lt;code&gt;reverse(nums[0]) = reverse(120) = 21 = nums[1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The minimum absolute distance is 1.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [21,120]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no mirror pairs in the array.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

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

&lt;ol&gt;
&lt;li&gt;Scan left to right with a hash map: for each &lt;code&gt;nums[i]&lt;/code&gt;, if the map contains key &lt;code&gt;nums[i]&lt;/code&gt; then set &lt;code&gt;ans = min(ans, i - map[nums[i]])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Store/update the current index under key &lt;code&gt;reverse(nums[i])&lt;/code&gt;, so future matches use the most recent index.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;This solution finds the minimum absolute distance between indices of mirror pairs in an array. A mirror pair consists of two numbers where one is the digit-reversed version of the other. The algorithm uses a single pass with a hash map to track the most recent index of each number's reverse, efficiently finding the smallest index gap in &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; time.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single-pass traversal&lt;/strong&gt; with hash map storage&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reverse number computation&lt;/strong&gt; for each array element&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Look for existing matches&lt;/strong&gt; before storing current index&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Track minimum distance&lt;/strong&gt; by comparing current index with previously stored index of the same number&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store current index&lt;/strong&gt; under the reversed number key for future matches&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/003761-minimum-absolute-distance-between-mirror-pairs" rel="noopener noreferrer"&gt;3761. Minimum Absolute Distance Between Mirror Pairs&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;minMirrorPairDistance&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;minAbsoluteDistanceMirrorPairs&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;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;minAbsoluteDistanceMirrorPairs&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;minAbsoluteDistanceMirrorPairs&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;minAbsoluteDistanceMirrorPairs&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="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;Key Insight&lt;/strong&gt;: When scanning left to right, if we encounter a number that matches a previously stored reversed number, they form a mirror pair. The reverse of the previous number equals the current number, and vice versa.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hash Map Strategy&lt;/strong&gt;: Store each number's latest index under the key equal to its reverse. This ensures that when we later see a number that matches a previously stored reverse, we can calculate the distance immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distance Calculation&lt;/strong&gt;: Since we always store the latest index for each reversed number, we minimize the distance for each potential pair. Using &lt;code&gt;abs(i - j)&lt;/code&gt; simplifies to &lt;code&gt;i - j&lt;/code&gt; because &lt;code&gt;i&lt;/code&gt; is always greater than &lt;code&gt;j&lt;/code&gt; during forward traversal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Numbers with trailing zeros (e.g., 120 → 21) are handled correctly by converting to integer after reversal&lt;/li&gt;
&lt;li&gt;Single-element arrays or arrays with no mirror pairs return -1&lt;/li&gt;
&lt;li&gt;Multiple occurrences of the same number are handled by overwriting the map entry, which actually helps find smaller gaps&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; - single pass through the array with &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; operations per element (reversal of digits is &lt;em&gt;&lt;strong&gt;O(log₁₀(num)&lt;/strong&gt;&lt;/em&gt;) which is effectively constant as &lt;em&gt;&lt;strong&gt;numbers ≤ 10⁹&lt;/strong&gt;&lt;/em&gt; have at most 10 digits)&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; - in worst case, the hash map stores a unique entry for each distinct reversed number in the array&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3488. Closest Equal Element Queries</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 16 Apr 2026 17:17:19 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3488-closest-equal-element-queries-1262</link>
      <guid>https://dev.to/mdarifulhaque/3488-closest-equal-element-queries-1262</guid>
      <description>&lt;p&gt;3488. Closest Equal Element Queries&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;Binary Search&lt;/code&gt;, &lt;code&gt;Weekly Contest 441&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a &lt;strong&gt;circular&lt;/strong&gt; array &lt;code&gt;nums&lt;/code&gt; and an array &lt;code&gt;queries&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For each query &lt;code&gt;i&lt;/code&gt;, you have to find the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;minimum&lt;/strong&gt; distance between the element at index &lt;code&gt;queries[i]&lt;/code&gt; and &lt;strong&gt;any&lt;/strong&gt; other index &lt;code&gt;j&lt;/code&gt; in the &lt;strong&gt;circular&lt;/strong&gt; array, where &lt;code&gt;nums[j] == nums[queries[i]]&lt;/code&gt;. If no such index exists, the answer for that query should be &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;em&gt;an array &lt;code&gt;answer&lt;/code&gt; of the &lt;strong&gt;same&lt;/strong&gt; size as &lt;code&gt;queries&lt;/code&gt;, where &lt;code&gt;answer[i]&lt;/code&gt; represents the result for query &lt;code&gt;i&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Query 0: The element at &lt;code&gt;queries[0] = 0&lt;/code&gt; is &lt;code&gt;nums[0] = 1&lt;/code&gt;. The nearest index with the same value is 2, and the distance between them is 2.&lt;/li&gt;
&lt;li&gt;Query 1: The element at &lt;code&gt;queries[1] = 3&lt;/code&gt; is &lt;code&gt;nums[3] = 4&lt;/code&gt;. No other index contains 4, so the result is -1.&lt;/li&gt;
&lt;li&gt;Query 2: The element at &lt;code&gt;queries[2] = 5&lt;/code&gt; is &lt;code&gt;nums[5] = 3&lt;/code&gt;. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: &lt;code&gt;5 -&amp;gt; 6 -&amp;gt; 0 -&amp;gt; 1&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,3,4], queries = [0,1,2,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [-1,-1,-1,-1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Each value in &lt;code&gt;nums&lt;/code&gt; is unique, so no index shares the same value as the queried element. This results in -1 for all queries.&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 = [5,5,5,1,2,5], queries = [0,3,5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1, -1, 1]&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= queries.length &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;0 &amp;lt;= queries[i] &amp;lt; nums.length&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Use a dictionary that maps each unique value in the array to a sorted list of its indices.&lt;/li&gt;
&lt;li&gt;For each query, use binary search on the sorted indices list to find the nearest occurrences of the target value.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The problem asks for the &lt;strong&gt;minimum circular distance&lt;/strong&gt; between a given index and any &lt;strong&gt;other&lt;/strong&gt; index containing the same value.&lt;br&gt;&lt;br&gt;
The solution groups indices by value, uses &lt;strong&gt;binary search&lt;/strong&gt; to find neighbors in the circular array, and computes distances with wrap-around logic.&lt;br&gt;&lt;br&gt;
If a value appears only once, the answer is &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Group indices by value&lt;/strong&gt; using a hash map (dictionary) where keys are array values and values are sorted lists of their indices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For each query index&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Retrieve the sorted list of indices for its value.&lt;/li&gt;
&lt;li&gt;If only one index exists → answer is &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;binary search&lt;/strong&gt; to locate the query index within its value’s index list.&lt;/li&gt;
&lt;li&gt;Check the &lt;strong&gt;previous&lt;/strong&gt; and &lt;strong&gt;next&lt;/strong&gt; indices in the circular list (wrap around using modulo).&lt;/li&gt;
&lt;li&gt;Compute the &lt;strong&gt;circular distance&lt;/strong&gt; between the query index and each candidate.&lt;/li&gt;
&lt;li&gt;Take the &lt;strong&gt;minimum&lt;/strong&gt; of these distances as the answer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return&lt;/strong&gt; an array of answers in the same order as queries.&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/003488-closest-equal-element-queries" rel="noopener noreferrer"&gt;3488. Closest Equal Element Queries&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[] $queries
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;solveQueries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$queries&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Binary search to find the exact position or insertion point
 *
 * @param $arr
 * @param $target
 * @return mixed
 */&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;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;/**
 * Calculate circular distance between two indices
 *
 * @param $i
 * @param $j
 * @param $n
 * @return mixed
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;circularDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$j&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;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="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;solveQueries&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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,-1,3]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;solveQueries&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: [-1,-1,-1,-1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;solveQueries&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: [1, -1, 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;Grouping indices:&lt;/strong&gt; We traverse the array once and store each index in a list corresponding to its value. This allows quick access to all positions of a given value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binary search for neighbors:&lt;/strong&gt; For a query index &lt;code&gt;q&lt;/code&gt;, we find its position in the sorted index list. Then, the nearest other indices with the same value are the ones immediately before and after it in that list (wrapping around at ends).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Circular distance:&lt;/strong&gt; On a circular array of length &lt;code&gt;n&lt;/code&gt;, the distance between indices &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt; is &lt;code&gt;min(|i - j|, n - |i - j|)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling uniqueness:&lt;/strong&gt; If a value appears only once, there is no other index to compare, so we return &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Grouping indices: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Each query: binary search &lt;code&gt;O(log m)&lt;/code&gt; where &lt;code&gt;m&lt;/code&gt; is the frequency of that value
&lt;/li&gt;
&lt;li&gt;Total: &lt;code&gt;O(n + q log n)&lt;/code&gt; worst case, but since &lt;code&gt;q ≤ n&lt;/code&gt;, it’s efficient.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: Hash map storing all indices: &lt;code&gt;O(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>
  </channel>
</rss>
