Intuition
Intuition\nThe problem requires the use of prefix sums. The key idea is as follows:\n\n1. For a num at index i, it will stop contributing to the score after (i+
# Intuition\nThe problem requires the use of prefix sums. The key idea is as follows:\n\n1. For a num at index i, it will stop contributing to the score after (i+1-num+n)%n rotations.\n2. For a num at index i, it will start contributing to the score after (i+1)%n rotations. \n\nIn case the element already contributes to the score, we increment changes[0].\n\nThis way we keep a track of potential scores at each rotation of the array. The score at rotation k is the sum of all the scores from 0 to index k in the change array. We find the maximum score and update the num rotations required accordingly.\n\n Rotate by left as question says\n\n        rotate=>0= [2,3,1,4,0] => [0, 0, 1, 0, 1]\n        idx      = [0,1,2,3,4]\n\n        rotate=>1= [3,1,4,0,2] => [0, 1, 0, 1, 1]\n        idx      = [0,1,2,3,4]\n\n        rotate=>2= [1,4,0,2,3] => [0, 0, 1, 1, 1]\n        idx      = [0,1,2,3,4]\n\n        rotate=>3= [4,0,2,3,1] => [0, 1, 1, 1, 1]\n        idx      = [0,1,2,3,4]\n\n        rotate=>4= [0,2,3,1,4] => [1, 0, 0, 1, 1]\n        idx      = [0,1,2,3,4]\n\n        number                            = 2 3 1 4 0\n        starts contibuting after rotation = 1 2 3 4 4\n        ends contibuting  at rotation     = 4 4 2 0 0\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```golang []\nfunc bestRotation(nums []int) int {\n   n := len(nums)\n\n\tchangeArr := make([]int, n)\n\n\tfor i := 0; i < n; i++ {\n\t\tnum := nums[i]\n\n\t\tlosePointAt := (i + 1 - num + n) % n\n\t\tgainPointAt := (i + 1) % n\n\n\t\t// fmt.Printf("num is %v, i is %v \\n", num, i)\n\t\t// fmt.Printf("losePointAt is %v, gainPointAt is %v \\n", losePointAt, gainPointAt)\n\n\t\t// fmt.Printf("=== \\n")\n\n\t\tchangeArr[losePointAt]--\n\t\tchangeArr[gainPointAt]++\n\n\t\tif losePointAt <= gainPointAt {\n\t\t\tchangeArr[0]++\n\t\t}\n\t}\n\n\t// fmt.Printf("changeArr is %v \\n", changeArr)\n\n\tindex := 0\n\n\tcurrentScore := 0\n\tmaxScore := math.MinInt\n\n\tfor i := 0; i < n; i++ {\n\t\tcurrentScore += changeArr[i]\n\t\tif currentScore > maxScore {\n\t\t\tmaxScore = currentScore\n\t\t\tindex = i\n\t\t}\n\t}\n\n\treturn index \n}\n```
['Go']