One Pass Line Sweep
IntuitionEach number has a fixed range in which its shift will be benefitial.
The length of that range can be calculated by (Length of nums - Value of the numbe
# Intuition
Each number has a fixed range in which its shift will be benefitial.
The length of that range can be calculated by (Length of nums - Value of the number) .

The problem is that sometimes, that range can be cut at a certain interval.

[2, 3, 1, 4, 0] , the number one is benefitial 5 - 1 = 4 times , and the range of rotaions on which it is benfitial is from 0 -> 1 and 3 -> 4 inclusive.

As we can see the 2 is missing from our interval , so to get a continuous interval that excludes the second rotation to be able to do an O(n) line sweep , we can say that 1 is benefitial from 3->6 inclusive where 5%5 -> 6%5 will take care of the  0 -> 1 interval.

We can notice that if we start our intervals at the end of the array where all the values have a positive contribution to the score we can get the range of each value and do a line sweep to calculate the score at each rotation

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(n)

# Code
```javascript []
/**
 * @param {number[]} nums
 * @return {number}
 */
var bestRotation = function (nums) {
  let line = Array(nums.length * 2 + 1).fill(0);
  for (let i = 0; i < nums.length; i++) {
    let curr = nums[i];
    let power = nums.length - curr;
    let toEnd = i + 1;
    line[toEnd]++;
    line[toEnd + power]--;
  }
  for(let i = 1 ; i < line.length ; i++){
    line[i]  = line[i] + line[i-1]
  }
  let res = Array(nums.length).fill(0)
  for(let l = 0 ; l < line.length ; l++){
    res[l%nums.length]+=line[l]
  }
  let ans = 0
  let max = res[0]
  for(let i = 1 ; i < res.length ; i++){
    if(res[i] > max){
        ans = i
        max = res[i]
    }
  }
  return ans
};
```
['JavaScript']