0
0
DSA C++programming~20 mins

Count Occurrences of Element in Sorted Array in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of count occurrences function with duplicates
What is the output of the following C++ code that counts occurrences of 3 in a sorted array?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>

int countOccurrences(const std::vector<int>& arr, int x) {
    auto first = std::lower_bound(arr.begin(), arr.end(), x);
    auto last = std::upper_bound(arr.begin(), arr.end(), x);
    return last - first;
}

int main() {
    std::vector<int> data = {1, 2, 3, 3, 3, 4, 5};
    std::cout << countOccurrences(data, 3) << std::endl;
    return 0;
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Use lower_bound and upper_bound to find the range of the element.
Predict Output
intermediate
2:00remaining
Count occurrences of element not present in array
What is the output of this C++ code counting occurrences of 6 in a sorted array?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>

int countOccurrences(const std::vector<int>& arr, int x) {
    auto first = std::lower_bound(arr.begin(), arr.end(), x);
    auto last = std::upper_bound(arr.begin(), arr.end(), x);
    return last - first;
}

int main() {
    std::vector<int> data = {1, 2, 3, 4, 5};
    std::cout << countOccurrences(data, 6) << std::endl;
    return 0;
}
A5
B1
C-1
D0
Attempts:
2 left
💡 Hint
If element is not found, lower_bound and upper_bound will be equal.
🔧 Debug
advanced
2:00remaining
Identify the error in counting occurrences code
What error does the following C++ code produce when counting occurrences of 3 in a sorted array?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>

int countOccurrences(const std::vector<int>& arr, int x) {
    auto first = std::lower_bound(arr.begin(), arr.end(), x);
    auto last = std::upper_bound(arr.begin(), arr.end(), x);
    return first - last;
}

int main() {
    std::vector<int> data = {1, 2, 3, 3, 3, 4, 5};
    std::cout << countOccurrences(data, 3) << std::endl;
    return 0;
}
ANegative number output
BSyntaxError
CCompilation error due to invalid subtraction
DZero output
Attempts:
2 left
💡 Hint
Check the order of subtraction between iterators.
Predict Output
advanced
2:00remaining
Count occurrences with all elements same
What is the output of this C++ code counting occurrences of 7 in an array where all elements are 7?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>

int countOccurrences(const std::vector<int>& arr, int x) {
    auto first = std::lower_bound(arr.begin(), arr.end(), x);
    auto last = std::upper_bound(arr.begin(), arr.end(), x);
    return last - first;
}

int main() {
    std::vector<int> data(10, 7);
    std::cout << countOccurrences(data, 7) << std::endl;
    return 0;
}
A10
B0
C1
D9
Attempts:
2 left
💡 Hint
All elements are the target value.
🧠 Conceptual
expert
2:00remaining
Why use binary search for counting occurrences in sorted array?
Why is binary search preferred over linear search to count occurrences of an element in a sorted array?
ABinary search can count occurrences without checking elements
BBinary search uses less memory than linear search
CBinary search reduces time complexity from O(n) to O(log n) by exploiting sorted order
DBinary search works only on unsorted arrays
Attempts:
2 left
💡 Hint
Think about how sorted order helps search faster.