Back to problems

Count Distinct Values in a Sorted Array When K Is Small

Algorithm · Meta · Medium

You are given an integer array numbers, already arranged in nondecreasing order. Calculate and return the count of distinct integers appearing in numbers. Denote the length of the array by n and the number of distinct values by k. The sorted order guarantees that every occurrence of a value forms one consecutive segment. A solution that scans every element is correct, but the intended target is to exploit cases where k is much smaller than n. The target complexity is $$O(k…

Checking your access…