What is the main advantage of using a sorted array to count occurrences of an element?
A sorted array allows us to use binary search to find the first and last positions of the element quickly, making the counting efficient.
Click to reveal answer
intermediate
How do you find the first occurrence of an element in a sorted array using binary search?
Modify binary search to continue searching in the left half even after finding the element, to find the earliest index where the element appears.
Click to reveal answer
beginner
How do you calculate the count of an element once you have the first and last occurrence indices?
Count = (last occurrence index - first occurrence index) + 1
Click to reveal answer
beginner
What is the time complexity of counting occurrences of an element in a sorted array using binary search?
O(log n), because binary search is used twice to find first and last occurrences.
Click to reveal answer
beginner
Why is a linear scan not efficient for counting occurrences in a sorted array?
A linear scan takes O(n) time, which is slower than binary search's O(log n), especially for large arrays.
Click to reveal answer
What is the first step to count occurrences of an element in a sorted array?
ASort the array
BCount elements by scanning from start to end
CFind the first occurrence of the element using binary search
DReverse the array
✗ Incorrect
Since the array is already sorted, the first step is to find the first occurrence using binary search.
If the first occurrence of element 5 is at index 2 and last occurrence is at index 4, what is the count?
A3
B4
C2
D5
✗ Incorrect
Count = (4 - 2) + 1 = 3
What is the time complexity of counting occurrences using binary search in a sorted array of size n?
AO(log n)
BO(n)
CO(n log n)
DO(1)
✗ Incorrect
Binary search runs in O(log n), so counting occurrences using two binary searches is O(log n).
Which of these is NOT a correct step in counting occurrences in a sorted array?
AFind last occurrence using binary search
BShuffle the array before searching
CCalculate count by subtracting indices
DFind first occurrence using binary search
✗ Incorrect
Shuffling the array breaks sorting and is not needed.
If the element is not found in the array, what should the count be?
A-1
B1
CArray length
D0
✗ Incorrect
If the element is not found, its count is zero.
Explain how to count occurrences of an element in a sorted array using binary search.
Think about how binary search can find boundaries of the element.
You got /4 concepts.
Why is binary search preferred over linear scan for counting occurrences in a sorted array?
Consider time complexity differences.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using a binary search approach to count occurrences of an element in a sorted array?
easy
A. It works only for unsorted arrays.
B. It uses less memory than a linear scan.
C. It reduces the time complexity to O(log n) compared to a linear scan.
D. It counts occurrences without checking the array elements.
Solution
Step 1: Understand binary search efficiency
Binary search divides the search space in half each time, making it faster than checking every element.
Step 2: Compare with linear scan
Linear scan checks each element, taking O(n) time, while binary search takes O(log n) time.
Final Answer:
It reduces the time complexity to O(log n) compared to a linear scan. -> Option C
Quick Check:
Binary search = O(log n) speed [OK]
Hint: Binary search halves search space, speeding up counting [OK]
Common Mistakes:
Thinking binary search works on unsorted arrays
Confusing time complexity with memory usage
Assuming binary search counts without element checks
2. Which of the following C# method signatures correctly represents a function to count occurrences of an integer in a sorted array?
easy
A. bool CountOccurrences(int[] arr, int target)
B. int CountOccurrences(int[] arr, int target)
C. int CountOccurrences(int target, int[] arr)
D. void CountOccurrences(int[] arr, int target)
Solution
Step 1: Identify return type for count
The function should return an integer representing the count, so return type must be int.
Step 2: Check parameter order and types
Common convention is array first, then target element; parameters types must match int array and int target.
Final Answer:
int CountOccurrences(int[] arr, int target) -> Option B
Quick Check:
Return count as int, array then target [OK]
Hint: Count returns int; array parameter comes before target [OK]
Common Mistakes:
Using void return type instead of int
Swapping parameter order incorrectly
Returning bool instead of count
3. What will be the output of the following C# code snippet?
int[] arr = {1, 2, 2, 2, 3, 4, 5};
int target = 2;
int count = CountOccurrences(arr, target);
Console.WriteLine(count);
int CountOccurrences(int[] nums, int x) {
int first = Array.BinarySearch(nums, x);
if (first < 0) return 0;
int count = 1;
int left = first - 1;
while (left >= 0 && nums[left] == x) {
count++;
left--;
}
int right = first + 1;
while (right < nums.Length && nums[right] == x) {
count++;
right++;
}
return count;
}
medium
A. 3
B. 2
C. 1
D. 0
Solution
Step 1: Find first occurrence index using BinarySearch
BinarySearch finds one occurrence of 2 at index 2.
Step 2: Count occurrences left and right of index 2
Left side: index 1 and 0 checked; index 1 is 2 (count=2), index 0 is 1 (stop). Right side: index 3 is 2 (count=3), index 4 is 3 (stop).
Final Answer:
3 -> Option A
Quick Check:
Occurrences of 2 = 3 [OK]
Hint: BinarySearch finds one index; count neighbors equal to target [OK]
Common Mistakes:
Assuming BinarySearch returns first occurrence always
Counting only one side neighbors
Returning 0 if not found without checking neighbors
4. Identify the bug in the following C# method that counts occurrences of a target in a sorted array:
int CountOccurrences(int[] nums, int target) {
int first = Array.BinarySearch(nums, target);
if (first < 0) return 0;
int count = 1;
int left = first - 1;
while (left > 0 && nums[left] == target) {
count++;
left--;
}
int right = first + 1;
while (right < nums.Length && nums[right] == target) {
count++;
right++;
}
return count;
}
medium
A. The method should return -1 if target not found instead of 0.
B. The right boundary check should be 'right <= nums.Length' instead of 'right < nums.Length'.
C. The count variable should start at 0, not 1.
D. The left boundary check should be 'left >= 0' instead of 'left > 0'.
Solution
Step 1: Analyze left boundary condition in while loop
The loop checks 'left > 0', so it skips index 0, missing a possible occurrence at start.
Step 2: Correct boundary condition to include index 0
Changing condition to 'left >= 0' ensures index 0 is checked for target equality.
Final Answer:
The left boundary check should be 'left >= 0' instead of 'left > 0'. -> Option D
Quick Check:
Check array start index 0 with '>= 0' [OK]
Hint: Use '>= 0' to include first array index in left scan [OK]
Common Mistakes:
Using 'left > 0' misses first element
Returning -1 instead of 0 when not found
Starting count at 0 instead of 1
Using wrong right boundary condition
5. Given a sorted array with possible duplicates, how can you efficiently count occurrences of a target element using two binary searches in C#?
hard
A. Find the first and last occurrence indices of the target using binary search, then calculate count as last - first + 1.
B. Use a single binary search to find any occurrence, then count neighbors linearly.
C. Sort the array again and count occurrences with a linear scan.
D. Use a hash map to store counts of all elements, then retrieve target count.
Solution
Step 1: Use binary search to find first occurrence of target
Modify binary search to find the lowest index where target appears.
Step 2: Use binary search to find last occurrence of target
Modify binary search to find the highest index where target appears.
Step 3: Calculate count from indices
Count = last occurrence index - first occurrence index + 1.
Final Answer:
Find the first and last occurrence indices of the target using binary search, then calculate count as last - first + 1. -> Option A
Quick Check:
Count = last - first + 1 using two binary searches [OK]
Hint: Two binary searches find boundaries; subtract indices for count [OK]
Common Mistakes:
Using linear scan after one binary search
Sorting array again unnecessarily
Using hash map instead of binary search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.