Last active
February 22, 2018 03:00
-
-
Save ValeriiVasyliev/86a9ed662cd88497cf16fe16510b4f25 to your computer and use it in GitHub Desktop.
Highest/Maximum difference between two values in an array (HackerRank Task)
This file contains 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 | |
$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