DEV Community

Ertugrul
Ertugrul

Posted on

πŸ—“ Daily LeetCode Progress – Day 20

Problems Solved:

  • #162 Find Peak Element
  • #540 Single Element in a Sorted Array

This continues my daily series (Day 20: Binary Search mastery). Reaching a 20‑day streak πŸ”₯ feels amazing β€” it marks nearly three weeks of consistent problem solving and deeper intuition for logarithmic‑time patterns.


πŸ’‘ What I Learned

Today’s focus was on binary search beyond the basics:

  • For Find Peak Element, I learned how to leverage binary search on an unsorted array by comparing nums[mid] and nums[mid+1]. Depending on the slope, we can always guarantee the peak lies on one side, allowing logarithmic search.
  • For Single Element in a Sorted Array, the key trick is to use the pair alignment property: before the single element, pairs start at even indices; after it, pairs shift. Adjusting mid to be even ensures clean pair checks.

Both problems highlight how binary search can apply to more than just sorted lookups β€” it’s about shrinking the search space with a reliable invariant.


🧩 #162 β€” Find Peak Element

Python (Slope check)

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        while left < right:
            mid = (left + right) // 2
            if nums[mid] < nums[mid + 1]:
                left = mid + 1
            else:
                right = mid
        return left
Enter fullscreen mode Exit fullscreen mode

Why it works: If nums[mid] < nums[mid+1], the slope goes upward, so a peak must exist on the right. Otherwise, the peak is on the left (possibly at mid).

Time: O(log n)
Space: O(1)

C++ (Same slope idea)

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] < nums[mid + 1]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
};
Enter fullscreen mode Exit fullscreen mode

🧩 #540 β€” Single Element in a Sorted Array

Python (Even index pairing)

class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        while left < right:
            mid = (left + right) // 2
            if mid % 2 == 0:
                if nums[mid] == nums[mid + 1]:
                    left = mid + 2
                else:
                    right = mid
            else:
                if nums[mid] == nums[mid - 1]:
                    left = mid + 1
                else:
                    right = mid
        return nums[left]
Enter fullscreen mode Exit fullscreen mode

Why it works: Before the single element, pairs align (even, odd). After it, alignment breaks. Checking nums[mid] with its partner tells us which side to keep.

Time: O(log n)
Space: O(1)

C++ (Adjust mid to even)

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (mid % 2 == 1) mid--; // make mid even
            if (nums[mid] == nums[mid + 1]) {
                left = mid + 2;
            } else {
                right = mid;
            }
        }
        return nums[left];
    }
};
Enter fullscreen mode Exit fullscreen mode

Why it works: By forcing mid to even, we always check pairs (mid, mid+1). If they match, the single lies to the right; otherwise, it’s at or before mid.


πŸ“Έ Achievements

  • Find Peak Element: Understood how slope comparison can adapt binary search for unsorted arrays.

peak python

peak cpp

  • Single Element in Sorted Array: Mastered the pair‑alignment trick β€” a very elegant use of index parity.

single python

single cpp


πŸ“¦ Complexity Recap

  • Find Peak Element: O(log n) time, O(1) space.
  • Single Element in Sorted Array: O(log n) time, O(1) space.

πŸ“£ Join the Journey

Day 20 streak achieved πŸŽ‰. This milestone marks 3 weeks of coding discipline. Each day’s problems are adding new insights into how binary search can be generalized far beyond its textbook form.

Follow along if you’re into algorithmic deep dives and clean implementations in Python and C++.

Links

Top comments (0)