Introduction
This is a sorted array frequency problem that can appear in coding assessments and online assessments such as an Amazon OA.
You are given a sorted array B of size N. Every distinct element occurs exactly K times except for one element, whose frequency is less than K.
The task is to find that element.
Because the array is sorted, all occurrences of the same element are located together. This allows us to process the array sequentially and keep track of the current element and its frequency.
For example:
Here:
Therefore, the answer is:
because 3 occurs fewer than K = 3 times.
Question
You are given a sorted array B of size N.
Every element in the array occurs exactly K times except one element, which occurs fewer than K times.
Find that element.
It is guaranteed that there is exactly one element whose frequency is less than K.
Constraints
Since B[i] can be as large as 10^10, a Java long should be used to safely store the array values.
Example
Consider:
Frequency table:
Only 3 occurs fewer than K times.
Therefore:
Understanding the Sorted Array
The most important property of this problem is that the array is sorted.
Because of that, identical elements always appear consecutively.
For example:
We never have to search the entire array to count the occurrences of an element.
We can simply scan from left to right.
When the value changes, the frequency of the previous value is known.
For example:
When we encounter 2, we know that:
We can compare this frequency with K.
If it is less than K, then 1 is the required answer.
Approach: Track the Current Element and Its Frequency
We can maintain two variables:
where:
candrepresents the current element.countrepresents how many times that element has appeared so far.
Initially:
Then we scan the remaining elements.
There are three important situations.
Case 1: Same Element
If:
then the current element is still being counted.
So:
Case 2: Element Changes and Frequency Is K
If:
then the previous element occurred exactly K times.
Therefore, we can start counting the new element:
Case 3: Element Changes and Frequency Is Less Than K
If:
then the previous element is the unique element whose frequency is less than K.
So we can immediately stop.
Java Solution
The same logic can be implemented more cleanly as follows:
Dry Run
Consider:
Initially:
Now process the array.
Step 1
Current value:
It is the same as cand.
Step 2
Current value:
The value changed.
Before moving to 2, we check the frequency of 1:
Since:
we have found the answer.
Therefore:
Another Example
Consider:
We start with:
After processing the three 2s:
The next value is 3.
Since:
we move to the next candidate:
After processing the second 3:
The next value is 4.
Now:
because:
Therefore:
is the required answer.
Important Edge Case: Answer at the End
There is one important case to handle.
Suppose the incomplete element is the last element in the array.
For example:
When the loop finishes, there is no next element that causes the value to change.
So the condition:
will never execute for the final 2.
We therefore need one final check after the loop:
This handles the case where the answer is at the end.
Complexity Analysis
We scan the array only once.
For every element, we perform constant-time operations.
Therefore:
Time Complexity:
Space Complexity:
This is efficient enough for:
and does not require an additional HashMap or frequency array.
Why Does the Sorted Property Matter?
Without the sorted property, the same element could appear at different positions.
For example:
In that case, simply maintaining the frequency of the current consecutive value would not work.
We would need another approach such as a HashMap.
But because the input is sorted:
all occurrences of an element form one continuous block.
That is what allows us to solve the problem with:
A Simpler Alternative
Because the array is sorted, another straightforward solution is to count consecutive equal values.
For example:
This version focuses directly on counting each consecutive group.
The idea is:
Interview Tip
When an interview or OA problem gives you a sorted array, always ask yourself:
What does sorting allow me to avoid?
Here, sorting means all equal values are adjacent.
Instead of using:
or sorting the array again, we can simply maintain a running frequency.
The important pattern is:
This pattern is useful in many array problems involving duplicate or repeated elements.
Conclusion
This Amazon OA-style problem can be solved efficiently by taking advantage of the fact that the input array is sorted.
Since equal elements appear consecutively, we only need to maintain:
Whenever the element changes, we check whether its frequency was less than K.
The solution requires:
The main takeaway is that the sorted property removes the need for additional frequency data structures and lets us find the unique incomplete-frequency element with a single linear scan.




