Easy C++, w/ comments - O(n) time
int bestRotation(vector<int>& A) {\n        int size = A.size(), score = 0;\n        vector<int> bad_intervals(size);\n\n        // for bad intervals for each e
    int bestRotation(vector<int>& A) {\n        int size = A.size(), score = 0;\n        vector<int> bad_intervals(size);\n\n        // for bad intervals for each element from the array\n        for(int i = 0; i < size; i++){\n            int num = A[i];\n            if(num <= i) score++;\n            \n            // if A[i] element ends up between left and right index of given array, it will not score\n            int left = (i - num + 1 + size) % size;\n            int right = (i + 1) % size;\n            \n            // create an interval [left, right] which is not good for A[ith] element\n            bad_intervals[left]--;\n            bad_intervals[right]++;\n            \n            // if the bad interval for A[i] is wrapped around the array\n            // split the interval into two intervals [0, left] and [right, 10];\n            // we do not need to store 10th value for interval - not useful\n            if(left > right)\n                bad_intervals[0]--; \n            \n        }\n        \n        // after this point apply merge-intervals logic\n\t    // see leetcode questions on merging intervals\n\t    // in short, minimum overlap inerval is the answer \n        int k = 0, max_score = score;\n        for(int i = 1; i < size; i++){\n            score += bad_intervals[i];\n            if(max_score < score)\n                max_score = score, k = i;\n        }\n        \n        return k;\n\t}
[]