Last active
June 13, 2025 14:12
-
-
Save chimit/d5128cc0e152456bc25a119e4ebe5b44 to your computer and use it in GitHub Desktop.
Find the larges difference between indexes of equal array items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function solution1($arr) { | |
$n = count($arr); | |
$diff = $n; | |
for ($diff = $n; $diff >= 0; $diff--) { | |
for ($i = $n - $diff; $i >= 0; $i--) { | |
$j = $diff + $i - 1; | |
if ($arr[$i] == $arr[$j]) { | |
return $diff - 1; | |
} | |
} | |
} | |
} | |
echo solution([1, 3, 8, 4, 2, 1, 4, 9]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment