What if you could count repeated numbers in a huge list without checking each one?
Why Count Occurrences of Element in Sorted Array in DSA C?
Imagine you have a long list of sorted numbers, like a phone book sorted by phone numbers, and you want to find out how many times a specific number appears.
Doing this by checking each number one by one would take a lot of time, especially if the list is very long.
Manually scanning through the entire list to count occurrences is slow and tiring.
It's easy to make mistakes by missing some numbers or counting wrong.
This approach wastes time and effort, especially when the list is huge.
Using a smart method that takes advantage of the list being sorted, we can quickly find where the number starts and ends.
This way, we count the occurrences without checking every single number.
This saves time and reduces errors.
int countOccurrences(int arr[], int size, int target) {
int count = 0;
for (int i = 0; i < size; i++) {
if (arr[i] == target) count++;
}
return count;
}int countOccurrences(int arr[], int size, int target) {
int first = findFirst(arr, size, target);
if (first == -1) return 0;
int last = findLast(arr, size, target);
return last - first + 1;
}This method lets you quickly and accurately count how many times a number appears in a sorted list, even if the list is very large.
Counting how many times a specific product ID appears in a sorted sales record to analyze its popularity.
Manual counting is slow and error-prone.
Using the sorted order helps find occurrences quickly.
This method saves time and improves accuracy.
Practice
Solution
Step 1: Understand binary search efficiency
Binary search divides the search space in half each time, reducing time complexity to O(log n).Step 2: Compare with linear search
Linear search checks each element one by one, taking O(n) time, which is slower for large arrays.Final Answer:
It reduces the search time from linear to logarithmic -> Option CQuick Check:
Binary search = faster search [OK]
- Thinking binary search sorts the array
- Confusing memory usage with speed
- Assuming binary search works on unsorted arrays
Solution
Step 1: Check function return type and parameters
The function should return an int (count), take array, size, and target element as parameters.Step 2: Validate parameter types
Array parameter must be int arr[] or int *arr, size as int n, and target as int x.Final Answer:
int countOccurrences(int arr[], int n, int x); -> Option BQuick Check:
Correct function signature = int countOccurrences(int arr[], int n, int x); [OK]
- Using void return type instead of int
- Passing array as int instead of int[]
- Missing array size parameter
int arr[] = {1, 2, 2, 2, 3, 4, 5};
int n = 7;
int x = 2;
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == x) count++;
}
printf("%d", count);Solution
Step 1: Iterate through the array and count matches
Elements equal to 2 are at indices 1, 2, and 3, so count increments 3 times.Step 2: Print the count
The final count is 3, so printf outputs 3.Final Answer:
3 -> Option DQuick Check:
Count of 2 in array = 3 [OK]
- Counting only first occurrence
- Off-by-one errors in loop
- Printing count before loop ends
int countOccurrences(int arr[], int n, int x) {
int count = 0;
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x) {
count++;
mid++;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return count;
}Solution
Step 1: Analyze loop variables and updates
Incrementing mid does not affect low or high, so loop may become infinite or incorrect.Step 2: Understand binary search update rules
To move search window, low or high must be updated, not mid.Final Answer:
Incrementing mid inside the loop does not change low or high -> Option AQuick Check:
mid++ does not move search window [OK]
- Changing mid instead of low/high
- Wrong loop condition causing infinite loop
- Incorrect return type
arr = {1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5}, write a function that uses binary search to count occurrences of x = 5. What is the count returned?Solution
Step 1: Use binary search to find first occurrence of 5
First 5 is at index 7.Step 2: Use binary search to find last occurrence of 5
Last 5 is at index 10.Step 3: Calculate count as last - first + 1
Count = 10 - 7 + 1 = 4.Final Answer:
4 -> Option AQuick Check:
Count of 5 in array = 4 [OK]
- Counting total elements instead of occurrences
- Off-by-one errors in count calculation
- Using linear search instead of binary search
Ready to prove it?
Pass the test and this pattern moves unknown
