def good(mid, m, n, a):
prev = 0
for i in range(n):
lf = a[i]
rf = a[i] + mid
# Check if prev is in the range [lf, rf] or after wrapping around modulo m
if (prev >= lf and prev <= rf) or ((prev + m) >= lf and (prev + m) <= rf):
continue
# If the current element is smaller than the previous one, return True
if lf < prev:
return True
prev = lf
return False
def findMinimum(n: int, m: int, a: list) -> int:
l, r = -1, m
# Binary search to find the minimum number of operations
while l < r - 1:
mid = (l + r) // 2
if good(mid, m, n, a):
l = mid # We need more operations
else:
r = mid # The current mid value is sufficient
return r