How to count the number of times sorted array with distinct integers are circularly rotated in PHP

1 Answer

0 votes
function countRotations($arr) {
    $min = $arr[0];
    $min_index = 0;
    $size = count($arr);
    
    for ($i = 0; $i < $size; $i++) {
        if ($min > $arr[$i]) {
            $min = $arr[$i];
            $min_index = $i;
        }
    }
    
    return $min_index;
}
        
$arr = array(23, 19, 15, 4, 6, 8, 9, 11);
        
echo countRotations($arr);




/*
run:

3

*/

 



answered Nov 21, 2023 by avibootz
...