Here is a cleaner and better way to solve the problem. ```lang-Python # Return the smallest index where the element is bigger than `A[start_index]`. # If `len(A)` is returned, no element is bigger than `A[start_index]`. def next_bigger_element(start_index, A): lo, hi = start_index, len(A) while lo + 1 < hi: mid = (lo + hi) // 2 if A[mid] == A[start_index]: lo = mid else: hi = mid return hi def distinct_elements_at_least(k, A): if len(A) == 0: return k <= 0 cur_index = 0 count = 1 # keep finding the next bigger element as long as less than `k` distinct # elements has been found and the end of the array has not been reached. while count < k and A[cur_index] != A[-1]: cur_index = next_bigger_element(cur_index, A) count += 1 return count >= k ``` To find whether `A` contains at least 4 distinct elements, just call `distinct_elements_at_least(4, A)`. This program works correctly for any given number `k`. For example, it can be used to check whether `A` has 0 element <!-- alway true --> or whether `A` has 7 distinct elements. For any fixed `k`, it works in $O(\log n)$ as desired. ------ If you do not mind `import bisect`, you may prefer the following shorter code, since method `next_bigger_element` is no longer needed. ```lang-Python from bisect import bisect_right def distinct_elements_at_least(k, A): if len(A) == 0: return k <= 0 cur = 0 count = 0 while cur < len(A) and count < k: count += 1 cur = bisect_right(A, A[cur], cur + 1) return count >= k ```