Recall & Review
beginner Click to reveal answer
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 quickly find the first and last positions of the element, making counting efficient.
beginner Click to reveal answer
Explain how binary search helps in counting occurrences of an element in a sorted array.
Binary search finds the first and last positions of the element in the array. The count is last position minus first position plus one.
intermediate Click to reveal answer
What is the time complexity of counting occurrences of an element in a sorted array using binary search?
The time complexity is O(log n) because binary search is used twice to find the first and last positions.
beginner Click to reveal answer
Why is a linear scan not efficient for counting occurrences in a large sorted array?
A linear scan checks each element one by one, which takes O(n) time, slower than binary search's O(log n) for large arrays.
beginner Click to reveal answer
What happens if the element is not present in the sorted array when counting occurrences?
Both first and last position searches return -1 or invalid indices, so the count is zero.
What is the first step to count occurrences of an element in a sorted array?
✗ Incorrect
Finding the first occurrence using binary search helps locate where the element starts.
If the first occurrence of an element is at index 3 and last occurrence at index 7, what is the count?
✗ Incorrect
Count = last index - first index + 1 = 7 - 3 + 1 = 5.
What is the time complexity of counting occurrences using binary search twice?
✗ Incorrect
Each binary search takes O(log n), so total is O(log n).
What should you return if the element is not found in the array?
✗ Incorrect
If element is not found, occurrences count is zero.
Why is binary search preferred over linear scan for large sorted arrays?
✗ Incorrect
Binary search is faster because it reduces search space quickly.
Describe the steps to count occurrences of an element in a sorted array using binary search.
Think about how to find boundaries of the element in the array.
You got /4 concepts.
Explain why counting occurrences in a sorted array is more efficient with binary search than with a linear scan.
Compare how many elements each method checks.
You got /4 concepts.
