<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: MD ARIFUL HAQUE</title>
    <description>The latest articles on Forem by MD ARIFUL HAQUE (@mdarifulhaque).</description>
    <link>https://forem.com/mdarifulhaque</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F944478%2F148ec474-230c-4c52-8b8e-5aba51bc6d2e.jpeg</url>
      <title>Forem: MD ARIFUL HAQUE</title>
      <link>https://forem.com/mdarifulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mdarifulhaque"/>
    <language>en</language>
    <item>
      <title>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://forem.com/mdarifulhaque/2452-words-within-two-edits-of-dictionary-3d6h</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/1722-minimize-hamming-distance-after-swap-operations-1545</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/2078-two-furthest-houses-with-different-colors-5b28</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/1855-maximum-distance-between-a-pair-of-values-4n6o</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/3783-mirror-distance-of-an-integer-4aa</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/3761-minimum-absolute-distance-between-mirror-pairs-52n7</link>
      <guid>https://forem.com/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://forem.com/mdarifulhaque/3488-closest-equal-element-queries-1262</link>
      <guid>https://forem.com/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>
    <item>
      <title>2515. Shortest Distance to Target String in a Circular Array</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 15 Apr 2026 16:48:14 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/2515-shortest-distance-to-target-string-in-a-circular-array-2oe4</link>
      <guid>https://forem.com/mdarifulhaque/2515-shortest-distance-to-target-string-in-a-circular-array-2oe4</guid>
      <description>&lt;p&gt;2515. Shortest Distance to Target String in a Circular Array&lt;/p&gt;

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

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

&lt;p&gt;You are given a &lt;strong&gt;0-indexed circular&lt;/strong&gt; string array &lt;code&gt;words&lt;/code&gt; and a string &lt;code&gt;target&lt;/code&gt;. A &lt;strong&gt;circular array&lt;/strong&gt; means that the array's end connects to the array's beginning.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formally, the next element of &lt;code&gt;words[i]&lt;/code&gt; is &lt;code&gt;words[(i + 1) % n]&lt;/code&gt; and the previous element of &lt;code&gt;words[i]&lt;/code&gt; is &lt;code&gt;words[(i - 1 + n) % n]&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of &lt;code&gt;words&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Starting from &lt;code&gt;startIndex&lt;/code&gt;, you can move to either the next word or the previous word with &lt;code&gt;1&lt;/code&gt; step at a time.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;shortest&lt;/strong&gt; distance needed to reach the string &lt;code&gt;target&lt;/code&gt;&lt;/em&gt;. If the string &lt;code&gt;target&lt;/code&gt; does not exist in &lt;code&gt;words&lt;/code&gt;, return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 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;We start from index 1 and can reach "hello" by

&lt;ul&gt;
&lt;li&gt;moving 3 units to the right to reach index 4.&lt;/li&gt;
&lt;li&gt;moving 2 units to the left to reach index 4.&lt;/li&gt;
&lt;li&gt;moving 4 units to the right to reach index 0.&lt;/li&gt;
&lt;li&gt;moving 1 unit to the left to reach index 0.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The shortest distance to reach "hello" 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; words = ["a","b","leetcode"], target = "leetcode", startIndex = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;We start from index 0 and can reach "leetcode" by

&lt;ul&gt;
&lt;li&gt;moving 2 units to the right to reach index 2.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;moving 1 unit to the left to reach index 2.&lt;/li&gt;

&lt;li&gt;The shortest distance to reach "leetcode" 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; words = ["cat","dog","bird"], target = "fish", startIndex = 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -1&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= words.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= words[i].length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;words[i]&lt;/code&gt; and &lt;code&gt;target&lt;/code&gt; consist of only lowercase English letters.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= startIndex &amp;lt; words.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;You have two options, either move straight to the left or move straight to the right.&lt;/li&gt;
&lt;li&gt;Find the first target word and record the distance.&lt;/li&gt;
&lt;li&gt;Choose the one with the minimum distance.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;This solution finds the minimum steps needed to reach a target string in a circular array starting from a given index. It calculates both clockwise and counterclockwise distances to each occurrence of the target and returns the smallest distance, or -1 if the target doesn't exist.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Circular Distance Calculation&lt;/strong&gt;: Use modulo arithmetic to compute distances in both directions around the circular array&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Target Location&lt;/strong&gt;: Iterate through all indices to find positions where &lt;code&gt;words[i]&lt;/code&gt; equals the target&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distance Comparison&lt;/strong&gt;: For each target occurrence, calculate both forward and backward distances and take the minimum&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Minimum&lt;/strong&gt;: Track the smallest distance across all target occurrences&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Case Handling&lt;/strong&gt;: Return -1 if no target found&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002515-shortest-distance-to-target-string-in-a-circular-array" rel="noopener noreferrer"&gt;2515. Shortest Distance to Target String in a Circular Array&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param String[] $words
 * @param String $target
 * @param Integer $startIndex
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;closestTarget&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;$words&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$startIndex&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;closestTarget&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"i"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"am"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"leetcode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s2"&gt;"hello"&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;closestTarget&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;"leetcode"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s2"&gt;"leetcode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;closestTarget&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"cat"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"dog"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"bird"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s2"&gt;"fish"&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Forward Distance Calculation&lt;/strong&gt;: &lt;code&gt;($i - $startIndex + $n) % $n&lt;/code&gt; computes steps needed moving right (clockwise) from start to target index i&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backward Distance Calculation&lt;/strong&gt;: &lt;code&gt;($startIndex - $i + $n) % $n&lt;/code&gt; computes steps needed moving left (counterclockwise) from start to target index i&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Minimum per Occurrence&lt;/strong&gt;: &lt;code&gt;min($forward, $backward)&lt;/code&gt; gives the shortest path to that specific target occurrence&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Optimization&lt;/strong&gt;: Track the overall minimum distance across all target positions in the array&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Target Found&lt;/strong&gt;: If &lt;code&gt;$minDistance&lt;/code&gt; remains &lt;code&gt;PHP_INT_MAX&lt;/code&gt;, the target doesn't exist, so return -1&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; where n is the length of the words array - single pass to check all indices&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; - only uses a few variables regardless of input size&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>1848. Minimum Distance to the Target Element</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 13 Apr 2026 15:38:35 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/1848-minimum-distance-to-the-target-element-1g90</link>
      <guid>https://forem.com/mdarifulhaque/1848-minimum-distance-to-the-target-element-1g90</guid>
      <description>&lt;p&gt;1848. Minimum Distance to the Target Element&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;Weekly Contest 239&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; &lt;strong&gt;(0-indexed)&lt;/strong&gt; and two integers &lt;code&gt;target&lt;/code&gt; and &lt;code&gt;start&lt;/code&gt;, find an index &lt;code&gt;i&lt;/code&gt; such that &lt;code&gt;nums[i] == target&lt;/code&gt; and &lt;code&gt;abs(i - start)&lt;/code&gt; is &lt;strong&gt;minimized&lt;/strong&gt;. Note that &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;Return &lt;code&gt;abs(i - start)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;guaranteed&lt;/strong&gt; that &lt;code&gt;target&lt;/code&gt; exists in &lt;code&gt;nums&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,3,4,5], target = 5, start = 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;code&gt;nums[4] = 5&lt;/code&gt; is the only value equal to &lt;code&gt;target&lt;/code&gt;, so the answer is &lt;code&gt;abs(4 - 3) = 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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1], target = 1, start = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;nums[0] = 1&lt;/code&gt; is the only value equal to &lt;code&gt;target&lt;/code&gt;, so the answer is &lt;code&gt;abs(0 - 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; nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Every value of &lt;code&gt;nums&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, but&lt;code&gt;nums[0]&lt;/code&gt; minimizes &lt;code&gt;abs(i - start)&lt;/code&gt;, which is &lt;code&gt;abs(0 - 0) = 0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

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

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

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= start &amp;lt; nums.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;target&lt;/code&gt; is in &lt;code&gt;nums&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;Loop in both directions until you find the target element.&lt;/li&gt;
&lt;li&gt;For each index &lt;code&gt;i&lt;/code&gt; such that &lt;code&gt;nums[i] == target calculate abs(i - start)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The solution finds the minimum distance between a given starting index and any occurrence of a target value in an array. It iterates through the entire array once, computing the absolute distance for each matching element and tracking the smallest distance found.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linear scan with distance calculation&lt;/strong&gt;: Iterate through all array elements once, checking each index where &lt;code&gt;nums[i] == target&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Absolute difference computation&lt;/strong&gt;: For each matching element, calculate &lt;code&gt;abs(i - start)&lt;/code&gt; as the distance&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Minimum tracking&lt;/strong&gt;: Maintain a running minimum distance value, updating whenever a smaller distance is found&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early optimization&lt;/strong&gt;: Since we need the absolute minimum, we can't stop early without checking all elements (though theoretically we could optimize by expanding outward from &lt;code&gt;start&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001848-minimum-distance-to-the-target-element" rel="noopener noreferrer"&gt;1848. Minimum Distance to the Target Element&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @param Integer $target
 * @param Integer $start
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getMinDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$start&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;getMinDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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;getMinDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;getMinDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;getMinDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;getMinDistance&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                  &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt;: Start with &lt;code&gt;minDistance&lt;/code&gt; set to &lt;code&gt;PHP_INT_MAX&lt;/code&gt; (largest possible integer).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iteration&lt;/strong&gt;: Loop through the array with both index &lt;code&gt;$i&lt;/code&gt; and value &lt;code&gt;$num&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Condition check&lt;/strong&gt;: When &lt;code&gt;$num == $target&lt;/code&gt;, calculate the absolute difference between current index and start.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comparison&lt;/strong&gt;: If the calculated distance is less than current &lt;code&gt;minDistance&lt;/code&gt;, update &lt;code&gt;minDistance&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return value&lt;/strong&gt;: After checking all elements, return the minimum distance found.&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; - where n is the length of the array, as we potentially need to check every element.&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 using a constant amount of extra space for variables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimality&lt;/strong&gt;: This is optimal for time complexity since we must examine all elements to find the minimum distance when the array is unsorted.&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>1974. Minimum Time to Type Word Using Special Typewriter</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 12 Apr 2026 15:04:41 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/1974-minimum-time-to-type-word-using-special-typewriter-1f2e</link>
      <guid>https://forem.com/mdarifulhaque/1974-minimum-time-to-type-word-using-special-typewriter-1f2e</guid>
      <description>&lt;p&gt;1974. Minimum Time to Type Word Using Special Typewriter&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;Greedy&lt;/code&gt;, &lt;code&gt;Biweekly Contest 59&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There is a special typewriter with lowercase English letters &lt;code&gt;'a'&lt;/code&gt; to &lt;code&gt;'z'&lt;/code&gt; arranged in a &lt;strong&gt;circle&lt;/strong&gt; with a &lt;strong&gt;pointer&lt;/strong&gt;. A character can only be typed if the pointer is pointing to that character. The pointer is &lt;strong&gt;initially&lt;/strong&gt; pointing to the character &lt;code&gt;'a'&lt;/code&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%2Fxhano7peh6y1la3saer6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxhano7peh6y1la3saer6.jpg" alt="chart" width="530" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each second, you may perform one of the following operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move the pointer one character &lt;strong&gt;counterclockwise&lt;/strong&gt; or &lt;strong&gt;clockwise&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Type the character the pointer is &lt;strong&gt;currently&lt;/strong&gt; on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given a string &lt;code&gt;word&lt;/code&gt;, return &lt;em&gt;the &lt;strong&gt;minimum&lt;/strong&gt; number of seconds to type out the characters in &lt;code&gt;word&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; word = "abc"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The characters are printed as follows:

&lt;ul&gt;
&lt;li&gt;Type the character 'a' in 1 second since the pointer is initially on 'a'.&lt;/li&gt;
&lt;li&gt;Move the pointer clockwise to 'b' in 1 second.&lt;/li&gt;
&lt;li&gt;Type the character 'b' in 1 second.&lt;/li&gt;
&lt;li&gt;Move the pointer clockwise to 'c' in 1 second.&lt;/li&gt;
&lt;li&gt;Type the character 'c' in 1 second.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "bza"&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;

&lt;ul&gt;
&lt;li&gt;The characters are printed as follows:

&lt;ul&gt;
&lt;li&gt;Move the pointer clockwise to 'b' in 1 second.&lt;/li&gt;
&lt;li&gt;Type the character 'b' in 1 second.&lt;/li&gt;
&lt;li&gt;Move the pointer counterclockwise to 'z' in 2 seconds.&lt;/li&gt;
&lt;li&gt;Type the character 'z' in 1 second.&lt;/li&gt;
&lt;li&gt;Move the pointer clockwise to 'a' in 1 second.&lt;/li&gt;
&lt;li&gt;Type the character 'a' in 1 second.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "zjpc"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 34&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The characters are printed as follows:

&lt;ul&gt;
&lt;li&gt;Move the pointer counterclockwise to 'z' in 1 second.&lt;/li&gt;
&lt;li&gt;Type the character 'z' in 1 second.&lt;/li&gt;
&lt;li&gt;Move the pointer clockwise to 'j' in 10 seconds.&lt;/li&gt;
&lt;li&gt;Type the character 'j' in 1 second.&lt;/li&gt;
&lt;li&gt;Move the pointer clockwise to 'p' in 6 seconds.&lt;/li&gt;
&lt;li&gt;Type the character 'p' in 1 second.&lt;/li&gt;
&lt;li&gt;Move the pointer counterclockwise to 'c' in 13 seconds.&lt;/li&gt;
&lt;li&gt;Type the character 'c' in 1 second.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= word.length &amp;lt;= 300&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word&lt;/code&gt; consists of uppercase 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;There are only two possible directions you can go when you move to the next letter.&lt;/li&gt;
&lt;li&gt;When moving to the next letter, you will always go in the direction that takes the least amount of time.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The problem simulates a circular typewriter starting at &lt;code&gt;'a'&lt;/code&gt;. Each movement costs 1 second per step (clockwise or counterclockwise), and typing the current character costs 1 second. The goal is to minimize total time to type a given word by always choosing the shorter direction between consecutive characters.&lt;/p&gt;

&lt;p&gt;The solution calculates the clockwise and counterclockwise distances between the current and target character on a circular alphabet (26 letters) and adds the typing time (1 second) for each character.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Character to index mapping&lt;/strong&gt;: Convert each character to a number from 0 to 25 based on its position in the alphabet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Circular distance&lt;/strong&gt;: For each step, compute both clockwise and counterclockwise distances. The minimal movement time is the smaller of the two.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typing time&lt;/strong&gt;: Add 1 second for typing the character after moving.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterate through the word&lt;/strong&gt;: Keep track of the current character (starting with &lt;code&gt;'a'&lt;/code&gt;), update it after each step.&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/001974-minimum-time-to-type-word-using-special-typewriter" rel="noopener noreferrer"&gt;1974. Minimum Time to Type Word Using Special Typewriter&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 $word
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minTimeToType&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;$word&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;minTimeToType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abc"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minTimeToType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bza"&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;minTimeToType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"zjpc"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 34&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;currentChar = 'a'&lt;/code&gt; and &lt;code&gt;totalTime = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each character in &lt;code&gt;word&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;Convert &lt;code&gt;currentChar&lt;/code&gt; and &lt;code&gt;targetChar&lt;/code&gt; to positions 0–25 using ASCII values.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;clockwise = abs(targetPos - currentPos)&lt;/code&gt; — direct forward distance.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;counterclockwise = 26 - clockwise&lt;/code&gt; — the other way around the circle.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;totalTime += min(clockwise, counterclockwise) + 1&lt;/code&gt; (1 second for typing).&lt;/li&gt;
&lt;li&gt;Update &lt;code&gt;currentChar = targetChar&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Return &lt;code&gt;totalTime&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)&lt;/strong&gt;&lt;/em&gt; where n is the length of &lt;code&gt;word&lt;/code&gt; — one pass through the string with constant-time operations.&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>1320. Minimum Distance to Type a Word Using Two Fingers</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 12 Apr 2026 13:25:05 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/1320-minimum-distance-to-type-a-word-using-two-fingers-446g</link>
      <guid>https://forem.com/mdarifulhaque/1320-minimum-distance-to-type-a-word-using-two-fingers-446g</guid>
      <description>&lt;p&gt;1320. Minimum Distance to Type a Word Using Two Fingers&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;String&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Weekly Contest 171&lt;/code&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%2Fbymffqveuehrrne849vk.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%2Fbymffqveuehrrne849vk.png" alt="leetcode_keyboard" width="349" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have a keyboard layout as shown above in the &lt;strong&gt;X-Y&lt;/strong&gt; plane, where each English uppercase letter is located at some coordinate.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, the letter &lt;code&gt;'A'&lt;/code&gt; is located at coordinate &lt;code&gt;(0, 0)&lt;/code&gt;, the letter &lt;code&gt;'B'&lt;/code&gt; is located at coordinate &lt;code&gt;(0, 1)&lt;/code&gt;, the letter &lt;code&gt;'P'&lt;/code&gt; is located at coordinate &lt;code&gt;(2, 3)&lt;/code&gt; and the letter &lt;code&gt;'Z'&lt;/code&gt; is located at coordinate &lt;code&gt;(4, 1)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the string &lt;code&gt;word&lt;/code&gt;, return &lt;em&gt;the minimum total &lt;strong&gt;distance&lt;/strong&gt; to type such string using only two fingers&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;distance&lt;/strong&gt; between coordinates &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;Note&lt;/strong&gt; that the initial positions of your two fingers are considered free so do not count towards your total distance, also your two fingers do not have to start at the first letter or the first two letters.&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; word = "CAKE"&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;Using two fingers, one optimal way to type "CAKE" is:&lt;/li&gt;
&lt;li&gt;Finger 1 on letter &lt;code&gt;'C' -&amp;gt; cost = 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 1 on letter &lt;code&gt;'A' -&amp;gt; cost = Distance&lt;/code&gt; from letter &lt;code&gt;'C'&lt;/code&gt; to letter &lt;code&gt;'A' = 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 2 on letter &lt;code&gt;'K' -&amp;gt; cost = 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 2 on letter &lt;code&gt;'E' -&amp;gt; cost = Distance&lt;/code&gt; from letter &lt;code&gt;'K'&lt;/code&gt; to letter &lt;code&gt;'E' = 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Total distance = &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "HAPPY"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 6&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;TUsing two fingers, one optimal way to type "HAPPY" is:&lt;/li&gt;
&lt;li&gt;Finger 1 on letter &lt;code&gt;'H' -&amp;gt; cost = 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 1 on letter &lt;code&gt;'A' -&amp;gt; cost = Distance&lt;/code&gt; from letter &lt;code&gt;'H'&lt;/code&gt; to letter &lt;code&gt;'A' = 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 2 on letter &lt;code&gt;'P' -&amp;gt; cost = 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 2 on letter &lt;code&gt;'P' -&amp;gt; cost = Distance&lt;/code&gt; from letter &lt;code&gt;'P'&lt;/code&gt; to letter &lt;code&gt;'P' = 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finger 1 on letter &lt;code&gt;'Y' -&amp;gt; cost = Distance&lt;/code&gt; from letter &lt;code&gt;'A'&lt;/code&gt; to letter &lt;code&gt;'Y' = 4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Total distance = &lt;code&gt;6&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= word.length &amp;lt;= 300&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word&lt;/code&gt; consists of uppercase 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;Use dynamic programming.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dp[i][j][k]&lt;/code&gt;: smallest movements when you have one finger on &lt;code&gt;i-th&lt;/code&gt; char and the other one on &lt;code&gt;j-th&lt;/code&gt; char already having written &lt;code&gt;k&lt;/code&gt; first characters from word.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;This solution uses &lt;strong&gt;dynamic programming&lt;/strong&gt; to minimize the total distance when typing a word with two fingers on a QWERTY-style keyboard.&lt;br&gt;&lt;br&gt;
It keeps track of the position of one finger (the "free" finger) while the other finger types the current character.&lt;br&gt;&lt;br&gt;
The DP state is &lt;code&gt;dp[f]&lt;/code&gt; = minimum total distance so far, with one finger at the last typed character and the other finger at position &lt;code&gt;f&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
At each step, we decide which finger moves to the next character.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Precompute distances&lt;/strong&gt; between all 26 letters based on their (row, column) coordinates on the keyboard.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DP definition&lt;/strong&gt;: &lt;code&gt;dp[f]&lt;/code&gt; = minimum total distance after typing up to the current character, where one finger is at the last typed character and the other finger is at letter index &lt;code&gt;f&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Initial state&lt;/strong&gt;: After typing the first character, one finger is on it, the other can be anywhere (cost 0 for all &lt;code&gt;f&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transition for each new character&lt;/strong&gt; &lt;code&gt;currChar&lt;/code&gt;:

&lt;ol&gt;
&lt;li&gt;Move the finger that was on &lt;code&gt;lastChar&lt;/code&gt; to &lt;code&gt;currChar&lt;/code&gt; → cost = &lt;code&gt;dist[lastChar][currChar]&lt;/code&gt;, other finger stays at &lt;code&gt;f&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Move the finger that was on &lt;code&gt;f&lt;/code&gt; to &lt;code&gt;currChar&lt;/code&gt; → cost = &lt;code&gt;dist[f][currChar]&lt;/code&gt;, other finger becomes &lt;code&gt;lastChar&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update DP&lt;/strong&gt; accordingly, tracking the new last character.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final answer&lt;/strong&gt; = minimum value in &lt;code&gt;dp&lt;/code&gt; after processing all characters.&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/001320-minimum-distance-to-type-a-word-using-two-fingers" rel="noopener noreferrer"&gt;1320. Minimum Distance to Type a Word Using Two Fingers&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 $word
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minimumDistance&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;$word&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;minimumDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"CAKE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/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;minimumDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HAPPY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: 6&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Step 1 — Precompute distances&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each letter is mapped to a coordinate: &lt;code&gt;(row = index / 6, col = index % 6)&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;Manhattan distance between two letters is precomputed and stored in a 26×26 array.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 2 — Initialize DP&lt;/strong&gt;: After first character &lt;code&gt;word[0]&lt;/code&gt;, one finger is on it, the other finger could be anywhere with zero distance so far.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 3 — Iterate through remaining characters&lt;/strong&gt;: For each new character &lt;code&gt;currChar&lt;/code&gt;, compute a new DP table &lt;code&gt;nextDp&lt;/code&gt; with &lt;code&gt;PHP_INT_MAX&lt;/code&gt; initial values.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 4 — Transition logic&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each possible &lt;code&gt;f&lt;/code&gt; (position of the other finger):&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 1&lt;/strong&gt;: Move the finger at &lt;code&gt;lastChar&lt;/code&gt; to &lt;code&gt;currChar&lt;/code&gt; →
&lt;code&gt;nextDp[f] = min(nextDp[f], dp[f] + dist[lastChar][currChar])&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 2&lt;/strong&gt;: Move the finger at &lt;code&gt;f&lt;/code&gt; to &lt;code&gt;currChar&lt;/code&gt; →
&lt;code&gt;nextDp[lastChar] = min(nextDp[lastChar], dp[f] + dist[f][currChar])&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 5 — Update state&lt;/strong&gt;: Set &lt;code&gt;dp = nextDp&lt;/code&gt; and &lt;code&gt;lastChar = currChar&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 6 — Return result&lt;/strong&gt;: Minimum value in &lt;code&gt;dp&lt;/code&gt; after processing all characters.&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;ul&gt;
&lt;li&gt;Precomputation: &lt;code&gt;O(26²) = O(1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DP: &lt;code&gt;O(n × 26)&lt;/code&gt; where &lt;code&gt;n&lt;/code&gt; is word length, because for each of &lt;code&gt;n-1&lt;/code&gt; steps, we iterate over 26 possible finger positions.&lt;/li&gt;
&lt;li&gt;Total: &lt;strong&gt;O(26n) = O(n)&lt;/strong&gt; with a small constant factor.&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;code&gt;dist&lt;/code&gt; array: &lt;code&gt;O(26²) = O(1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DP arrays: &lt;code&gt;O(26)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Total: &lt;strong&gt;O(1)&lt;/strong&gt; extra space beyond input.&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>3741. Minimum Distance Between Three Equal Elements II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 11 Apr 2026 14:33:40 +0000</pubDate>
      <link>https://forem.com/mdarifulhaque/3741-minimum-distance-between-three-equal-elements-ii-2gf4</link>
      <guid>https://forem.com/mdarifulhaque/3741-minimum-distance-between-three-equal-elements-ii-2gf4</guid>
      <description>&lt;p&gt;3741. Minimum Distance Between Three Equal Elements II&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;Weekly Contest 475&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 tuple &lt;code&gt;(i, j, k)&lt;/code&gt; of 3 &lt;strong&gt;distinct&lt;/strong&gt; indices is &lt;strong&gt;good&lt;/strong&gt; if &lt;code&gt;nums[i] == nums[j] == nums[k]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;distance&lt;/strong&gt; of a &lt;strong&gt;good&lt;/strong&gt; tuple is &lt;code&gt;abs(i - j) + abs(j - k) + abs(k - i)&lt;/code&gt;, where &lt;code&gt;abs(x)&lt;/code&gt; denotes the &lt;strong&gt;absolute value&lt;/strong&gt; of &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return an integer denoting the &lt;strong&gt;minimum&lt;/strong&gt; possible &lt;strong&gt;distance&lt;/strong&gt; of a &lt;strong&gt;good&lt;/strong&gt; tuple. If no &lt;strong&gt;good&lt;/strong&gt; tuples exist, return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;The minimum distance is achieved by the good tuple &lt;code&gt;(0, 2, 3)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(0, 2, 3)&lt;/code&gt; is a good tuple because &lt;code&gt;nums[0] == nums[2] == nums[3] == 1&lt;/code&gt;. Its distance is &lt;code&gt;abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6&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,1,2,3,2,1,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 8&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The minimum distance is achieved by the good tuple &lt;code&gt;(2, 4, 6)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(2, 4, 6)&lt;/code&gt; is a good tuple because &lt;code&gt;nums[2] == nums[4] == nums[6] == 2&lt;/code&gt;. Its distance is &lt;code&gt;abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8&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; nums = [1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; -1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no good tuples. Therefore, the answer is -1.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n == nums.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= n&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 distance formula &lt;code&gt;abs(i - j) + abs(j - k) + abs(k - i)&lt;/code&gt; simplifies to &lt;code&gt;2 * (max(i, j, k) - min(i, j, k))&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Group the indices for each unique number. For a number to form a good tuple, it must appear at least 3 times.&lt;/li&gt;
&lt;li&gt;For each number that appears at least 3 times, we want to find three of its indices &lt;code&gt;p &amp;lt; q &amp;lt; r&lt;/code&gt; that minimize &lt;code&gt;r - p&lt;/code&gt;. This is achieved by considering every three consecutive indices in the sorted list of indices.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We are given an array and need to find the minimum distance among three equal elements, where distance is defined as &lt;code&gt;abs(i-j) + abs(j-k) + abs(k-i)&lt;/code&gt;. This simplifies to &lt;code&gt;2 * (max(i,j,k) - min(i,j,k))&lt;/code&gt;. The solution groups indices by value, considers only values with at least three occurrences, and checks consecutive triples in sorted index lists to minimize the range between the smallest and largest index. The final answer is twice that minimum range.&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; Use a hash map to store all indices where each value appears.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filter values with ≥3 occurrences:&lt;/strong&gt; Only those can form a valid triple.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sort indices for each value:&lt;/strong&gt; Since we add indices in increasing order during iteration, they are naturally sorted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slide a window of size 3 over indices:&lt;/strong&gt; For each consecutive triple &lt;code&gt;(p, q, r)&lt;/code&gt; in sorted order, compute &lt;code&gt;r - p&lt;/code&gt; and track the minimum.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compute final distance:&lt;/strong&gt; Multiply the minimum range by 2 to get the required 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/003741-minimum-distance-between-three-equal-elements-ii" rel="noopener noreferrer"&gt;3741. Minimum Distance Between Three Equal Elements II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDistance&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;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: 6&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDistance&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: 8&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumDistance&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Distance simplification:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For three indices &lt;code&gt;i &amp;lt; j &amp;lt; k&lt;/code&gt;, the expression &lt;code&gt;abs(i-j) + abs(j-k) + abs(k-i)&lt;/code&gt; becomes &lt;code&gt;(j-i) + (k-j) + (k-i) = 2*(k-i)&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;So minimizing the sum is equivalent to minimizing &lt;code&gt;k - i&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Why consecutive indices in sorted order?&lt;/strong&gt; For a given sorted list of indices, the smallest &lt;code&gt;k - i&lt;/code&gt; for any triple occurs when we take three consecutive indices. Any triple with a gap between would have a larger spread.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Sliding window of size 3:&lt;/strong&gt; Iterate through &lt;code&gt;indices[0..m-3]&lt;/code&gt; and check &lt;code&gt;(indices[i], indices[i+1], indices[i+2])&lt;/code&gt;. This is efficient (&lt;em&gt;&lt;strong&gt;O(m)&lt;/strong&gt;&lt;/em&gt;) and guarantees finding the minimal range.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Return -1 if no value appears at least 3 times&lt;/strong&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;

&lt;ul&gt;
&lt;li&gt;One pass to group indices into hash map.&lt;/li&gt;
&lt;li&gt;For each value, iterating over its indices in order takes O(m) per value, and sum of all m over values = n.&lt;/li&gt;
&lt;li&gt;Overall linear.&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(1)&lt;/strong&gt;&lt;/em&gt; - Hash map stores up to n indices in total.&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>
