Biweekly Contest 113 | Minimum Right Shifts to Sort the Array.
Intuition\nThe problem states that given a 0-indexed array containing distinct positive integers and we are being asked to return the minimum number of right sh
# Intuition\nThe problem states that given a `0-indexed` array containing distinct positive integers and we are being asked to return the minimum number of right shifts required to sort `nums` and `-1` if this is not possible.\n\n# Approach\nInorder to solve this we will:\n- Create a copy `nums` and assign it to variable `newArr`, this is such that we can sort the array without changing the original array.\n- We then check if both arrays `nums` and the `copy` are equal and return `0` if they are.\n- Do a binary search to find the pivot `l`.\n- Then create a new sorted array by readjusting the pivot amd assign it to `newArr`.\n- Check if the readjusted array `arr` is equal to the sorted array `newArr`.\n\n# Complexity\n- Time complexity:\n$$O(nlogn)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nfunc minimumRightShifts(nums []int) int {\n    l, r := 0, len(nums)-1\n\n\tnewArr := append([]int{}, nums...)\n\tsort.Ints(newArr)\n\n\tif reflect.DeepEqual(nums, newArr) {\n\t\treturn 0\n\t}\n\n\tfor l < r {\n\t\tmid := l + (r-l)/2\n\t\tif nums[mid] > nums[r] {\n\t\t\tl = mid + 1\n\t\t} else {\n\t\t\tr = mid\n\t\t}\n\t}\n\n\tarr := append(nums[l:], nums[:l]...)\n\tif reflect.DeepEqual(arr, newArr) {\n\t\treturn len(nums) - l\n\t}\n\n\treturn -1\n}\n```
['Binary Search', 'Go']