Skip to content

Commit

Permalink
[solved] runtime problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ashraf789 committed Apr 22, 2020
1 parent 3a88815 commit a913086
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Running Time of Algorithms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @author: syed ashraf ullah
* date: 22/04/2020
* problem: https://www.hackerrank.com/challenges/runningtime/problem
*/
// Complete the runningTime function below.
function runningTime($arr) {
$runtimee = 0;
for($i=0;$i<count($arr);$i++){
$val = $arr[$i];
$j = $i-1;
while($j>=0 && $arr[$j] > $val){
$arr[$j+1] = $arr[$j];
$j--;
$runtimee++;
}
$arr[$j+1] = $val;
}

return $runtimee;
}


$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%d\n", $n);

fscanf($stdin, "%[^\n]", $arr_temp);

$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));

$result = runningTime($arr);

fwrite($fptr, $result . "\n");

fclose($stdin);
fclose($fptr);

0 comments on commit a913086

Please sign in to comment.