Skip to content

Instantly share code, notes, and snippets.

@MagedAhmad
Created April 18, 2020 23:29
Show Gist options
  • Select an option

  • Save MagedAhmad/4464f6c0a7b51d550218b60aef2cfda2 to your computer and use it in GitHub Desktop.

Select an option

Save MagedAhmad/4464f6c0a7b51d550218b60aef2cfda2 to your computer and use it in GitHub Desktop.
Hacker rank new year chaos
<?php
function minimumBribes($q) {
$count = 0;
for($i = count($q) -1 ; $i >= 0; $i--) {
if($q[$i] - ($i+1) > 2) {
print "Too chaotic" . "\n";
return;
}
for($j = $q[$i] - 2 > 0 ? $q[$i] - 2: 0 ; $j < $i; $j++) {
if($q[$j] > $q[$i]) {
$count++;
}
}
}
print $count . "\n";
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment