0
0
DSA C++programming~10 mins

Count Occurrences of Element in Sorted Array in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Count Occurrences of Element in Sorted Array
Start
Input: Sorted Array, Target Element
Find First Occurrence Index
Find Last Occurrence Index
Calculate Count = Last - First + 1
Return Count
End
We find where the target first appears, then where it last appears, then count how many times it occurs.
Execution Sample
DSA C++
int countOccurrences(int arr[], int n, int x) {
  int first = findFirst(arr, n, x);
  if (first == -1) return 0;
  int last = findLast(arr, n, x);
  return last - first + 1;
}
This code finds the first and last positions of x in a sorted array and returns how many times x appears.
Execution Table
StepOperationIndex CheckedComparison ResultPointer ChangesVisual State
1Find First Occurrencemid=4arr[4]=5 == 5high=4[1,2,3,4,5,5,5,6,7]
2Find First Occurrencemid=1arr[1]=2 < 5low=2[1,2,3,4,5,5,5,6,7]
3Find First Occurrencemid=3arr[3]=4 < 5low=4[1,2,3,4,5,5,5,6,7]
4Find First Occurrencemid=4arr[4]=5 == 5high=4[1,2,3,4,5,5,5,6,7]
5Find First Occurrencemid=4arr[4]=5 == 5high=3[1,2,3,4,5,5,5,6,7]
6First Occurrence Foundindex=4--[1,2,3,4,5,5,5,6,7]
7Find Last Occurrencemid=6arr[6]=5 == 5low=7[1,2,3,4,5,5,5,6,7]
8Find Last Occurrencemid=7arr[7]=6 > 5high=6[1,2,3,4,5,5,5,6,7]
9Find Last Occurrencemid=6arr[6]=5 == 5low=7[1,2,3,4,5,5,5,6,7]
10Last Occurrence Foundindex=6--[1,2,3,4,5,5,5,6,7]
11Calculate Count---Count = 6 - 4 + 1 = 3
12Return Count---3
💡 Search ends when low > high or first/last occurrence found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10Final
low (first)002444444444
high (first)844433333333
mid (first)-41344------
firstIndex-1-1-1-1-14444444
low (last)000000077777
high (last)888888886666
mid (last)-------676--
lastIndex-1-1-1-1-1-1-1-1-1-166
count-----------3
Key Moments - 3 Insights
Why do we update 'high' when arr[mid] == x in findFirst?
Because we want to find the earliest index where x appears, so we move 'high' left to continue searching earlier positions (see steps 1,4,5).
Why do we update 'low' when arr[mid] == x in findLast?
Because we want to find the last occurrence, so we move 'low' right to search later positions (see steps 7,9).
What if the element is not found at all?
The first occurrence search returns -1, so countOccurrences returns 0 immediately (not shown in this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'firstIndex' after step 6?
A4
B3
C-1
D6
💡 Hint
Check the 'firstIndex' variable in variable_tracker after step 6.
At which step does the 'lastIndex' get assigned its final value?
AStep 9
BStep 10
CStep 7
DStep 11
💡 Hint
Look at the execution_table row where 'Last Occurrence Found' is noted.
If the target element was not in the array, what would the count be?
A1
B-1
C0
DArray length
💡 Hint
Refer to the key moment about element not found and the early return in code.
Concept Snapshot
Count Occurrences in Sorted Array:
- Use binary search twice: find first and last occurrence of target.
- For first occurrence: move high when arr[mid] == target.
- For last occurrence: move low when arr[mid] == target.
- Count = lastIndex - firstIndex + 1.
- If not found, count is 0.
Full Transcript
To count how many times an element appears in a sorted array, we find the first and last positions where it appears using binary search. We adjust pointers to narrow down the search: for the first occurrence, we move the high pointer left when we find the target; for the last occurrence, we move the low pointer right. The count is the difference between these positions plus one. If the element is not found, the count is zero.