Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
My solution

Mohammed Dh Abbas

Jun 17, 2024

class Solution: def countRotations(self, arr): length = len(arr) - 1 b, e = 0, length m = (b + e) // 2 # edge case: if the array is fully reversed like [5, 4, 3, 2, 1] if arr[b] > arr[m] > arr[e]: return length while b <= e: m = (b + e) // 2 # check and discard the first half if arr[b] <= arr[m]: # before [b = m + 1] check if we have found the index if m < length and arr[m] > arr[m + 1]: return m + 1 # nothing found just discard b = m + 1 # check and discard the the second half elif arr[m] <= arr[e]: # before [e = m - 1] check if we have found the index if m - 1 >= 0 and arr[m - 1] > arr[m]: return m # nothing found just discard e = m - 1 return 0 # no rotaion at all

0

0

Comments
Comments

On this page