Skip to content

Instantly share code, notes, and snippets.

@ValeriiVasyliev
Last active February 22, 2018 03:00
Show Gist options
  • Save ValeriiVasyliev/86a9ed662cd88497cf16fe16510b4f25 to your computer and use it in GitHub Desktop.
Save ValeriiVasyliev/86a9ed662cd88497cf16fe16510b4f25 to your computer and use it in GitHub Desktop.
Highest/Maximum difference between two values in an array (HackerRank Task)
<?php
$a = [5, 10, 8, 7, 6, 5];
$size = sizeof($a)/sizeof($a[0]);
$max_diff = -1;
$min_element = $a[0];
for ($i=1; $i<$size; $i++) {
if ($a[$i] > $min_element) {
$diff = $a[$i] - $min_element;
if ($diff > $max_diff) {
$max_diff = $diff;
}
}
else {
$min_element = $a[$i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment