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#
using System; class Program { static int CountOccurrences(int[] arr, int x) { int first = Array.BinarySearch(arr, x); if (first < 0) return 0; int count = 0; int index = first; while (index < arr.Length && arr[index] == x) { count++; index++; } return count; } static void Main() { int[] arr = {1, 2, 3, 3, 3, 4, 5}; Console.WriteLine(CountOccurrences(arr, 3)); } }
Attempts:
2 left
💡 Hint
Think about how many times 3 appears consecutively starting from the first found index.
✗ Incorrect
The binary search finds the first occurrence of 3 at index 2. Then the loop counts 3 occurrences at indices 2, 3, and 4.
🧠 Conceptual
intermediateWhy use binary search to count occurrences?
Why is binary search a good choice to count occurrences of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about the speed of finding an element in a sorted list.
✗ Incorrect
Binary search quickly locates an element in O(log n) time, then counting neighbors is done in O(k) where k is occurrences.
❓ Predict Output
advancedOutput of counting occurrences with custom binary search bounds
What is the output of this C# code that counts occurrences of 2 in a sorted array using custom binary search for first and last positions?
DSA C#
using System; class Program { static int FindFirst(int[] arr, int x) { int low = 0, high = arr.Length - 1, result = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == x) { result = mid; high = mid - 1; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return result; } static int FindLast(int[] arr, int x) { int low = 0, high = arr.Length - 1, result = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == x) { result = mid; low = mid + 1; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return result; } static int CountOccurrences(int[] arr, int x) { int first = FindFirst(arr, x); if (first == -1) return 0; int last = FindLast(arr, x); return last - first + 1; } static void Main() { int[] arr = {1, 2, 2, 2, 3, 4, 5}; Console.WriteLine(CountOccurrences(arr, 2)); } }
Attempts:
2 left
💡 Hint
Check how first and last indices of 2 are found and subtracted.
✗ Incorrect
First occurrence of 2 is at index 1, last at index 3, so count is 3 (3 - 1 + 1).
🔧 Debug
advancedIdentify the error in counting occurrences code
What error does the following C# code produce when counting occurrences of 4 in a sorted array?
DSA C#
using System; class Program { static int CountOccurrences(int[] arr, int x) { int first = Array.BinarySearch(arr, x); if (first < 0) return 0; int count = 0; int index = first; while (index < arr.Length && arr[index] == x) { count++; index++; } return count; } static void Main() { int[] arr = {1, 2, 3, 4, 4, 4, 5}; Console.WriteLine(CountOccurrences(arr, 4)); } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully for array bounds.
✗ Incorrect
The loop condition uses index <= arr.Length which allows index to equal arr.Length, causing out-of-bounds access.
🚀 Application
expertCount occurrences in a large sorted array efficiently
You have a very large sorted array with millions of elements. Which approach is best to count occurrences of a number efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity for large sorted data.
✗ Incorrect
Two binary searches run in O(log n) time, much faster than linear scans or sorting for large arrays.
