Skip to content

Instantly share code, notes, and snippets.

@chimit
Last active June 13, 2025 14:12
Show Gist options
  • Save chimit/d5128cc0e152456bc25a119e4ebe5b44 to your computer and use it in GitHub Desktop.
Save chimit/d5128cc0e152456bc25a119e4ebe5b44 to your computer and use it in GitHub Desktop.
Find the larges difference between indexes of equal array items
<?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