Challenge - 5 Problems
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of counting occurrences using binary search
What is the output of the following C code that counts occurrences of 3 in a sorted array?
DSA C
#include <stdio.h> int firstOccurrence(int arr[], int n, int x) { int low = 0, high = n - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) { result = mid; high = mid - 1; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return result; } int lastOccurrence(int arr[], int n, int x) { int low = 0, high = n - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) { result = mid; low = mid + 1; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return result; } int countOccurrences(int arr[], int n, int x) { int first = firstOccurrence(arr, n, x); if (first == -1) return 0; int last = lastOccurrence(arr, n, x); return last - first + 1; } int main() { int arr[] = {1, 2, 3, 3, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; printf("%d\n", countOccurrences(arr, n, x)); return 0; }
Attempts:
2 left
💡 Hint
Count how many times 3 appears in the array {1, 2, 3, 3, 3, 4, 5}.
✗ Incorrect
The number 3 appears three times consecutively in the array at indices 2, 3, and 4. The code finds the first and last occurrence indices and returns their difference plus one.
❓ Predict Output
intermediateOutput when element is not present
What is the output of this C code when counting occurrences of 6 in the sorted array?
DSA C
int arr[] = {1, 2, 3, 3, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int x = 6; printf("%d\n", countOccurrences(arr, n, x));
Attempts:
2 left
💡 Hint
If the element is not found, the count should be zero.
✗ Incorrect
Since 6 is not in the array, firstOccurrence returns -1, so countOccurrences returns 0.
🧠 Conceptual
advancedWhy binary search is efficient for counting occurrences in sorted arrays
Why is binary search a good choice to count occurrences of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about how binary search divides the search space.
✗ Incorrect
Binary search quickly narrows down the position of the element's first and last occurrence in logarithmic time, which is faster than scanning all elements.
🔧 Debug
advancedIdentify the bug in this counting occurrences code
What error will this code produce when counting occurrences of 3 in the array?
DSA C
#include <stdio.h> int countOccurrences(int arr[], int n, int x) { int count = 0; for (int i = 0; i < n; i++) { if (arr[i] == x) count++; } return count; } int main() { int arr[] = {1, 2, 3, 3, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; printf("%d\n", countOccurrences(arr, n, x)); return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs from i = 0 to i <= n, which accesses arr[n], an invalid index causing runtime error.
🚀 Application
expertCount occurrences in a large sorted array efficiently
You have a sorted array of 1 million integers. Which approach is best to count occurrences of a number efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity for large data.
✗ Incorrect
Binary search finds boundaries in O(log n) time, much faster than scanning or sorting again.
