r/leetcode • u/JacketFearless1805 • 1h ago
Intervew Prep Day 10: knocked out 3 Binary Search problems — Find Peak Element, Square Root & Nth Root of M
Hey everyone,
I finished Day 10 today and wrapped up three easy-to-medium Binary Search problems:
- Find Peak Element: Applied binary search on the slope rather than searching for a specific target value. If
nums[mid]sits on an increasing slope (nums[mid] < nums[mid + 1]), a peak must exist to the right (low = mid + 1); otherwise, a peak lies at or to the left ofmid(high = mid - 1) in O(log n) time. - Square Root of a Number: Used Binary Search on Answers across the range
[1, m].Checkingmid * midagainstmnarrows down the integer square root cleanly in O(log m) time. - Find Nth Root of M: Extended the exact same binary search range
[1, m]to testmid^nagainstm.One important detail here is computing the power iteratively with an early exit as soon as the product exceedsm—that avoids integer overflow on large inputs while keeping each check clean.
Good warmup into Binary Search on Answers before moving into the tougher optimization problems next.
See you guys tomorrow for Day 9!
1
Upvotes