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

1 Answer

0 votes
function countRotations(arr) {
    let min = arr[0], min_index = 0;
    let size = arr.length;
     
    for (let i = 0; i < size; i++) {
        if (min > arr[i]) {
            min = arr[i];
            min_index = i;
        }
    }
     
    return min_index;
}
         
const arr = [23, 17, 19, 15, 4, 6, 8, 9, 11];
 
console.log(countRotations(arr));
 
 
 
 
/*
run:
 
4
 
*/

 



answered Nov 22, 2023 by avibootz
...