Skip to main content

Diagnosis Pair

Challenge on lesson: Checkpoint: Algorithms

Two independent problems in Solution: 1. static int lowerBound(int[] sorted, int target) — first index with sorted[i] >= target via the half-open binary search; returning sorted.length means none. (Not indexOf: duplicates must find the FIRST.) 2. static boolean hasMajority(int[] xs) — true when some value occurs more than n/2 times, via one frequency-map pass (no sorting). [1,2,2,2,3], lowerBound(2) → 1 (not 2 — the first). [2,2,1,1], hasMajority → false: 1 appears twice, n/2 = 2, needs MORE than 2 → false. [1,1,1,2] → true: 3 of 4.

Difficulty: intermediate

Back to lesson: Checkpoint: Algorithms