Walk through nums = [1, 2, 1, 3, 5, 6, 4].
left = 0, right = 6. mid = 3. nums[3] = 3 < nums[4] = 5, so go right: left = 4.
mid = 5. nums[5] = 6 > nums[6] = 4, so go left: right = 5.
left == right == 5. Return . Index holds value , which is greater than both neighbors ( and ). That's a valid peak.
You halve the search space each step. That gives time. You only use a few variables, so space.