0
0
DSA C++programming~10 mins

Count Occurrences of Element in Sorted Array in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the first occurrence of the target in the sorted array.

DSA C++
int firstOccurrence(const vector<int>& nums, int target) {
    int left = 0, right = nums.size() - 1;
    int result = -1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {
            result = mid;
            right = mid [1] 1;
        } else if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes infinite loop or wrong index.
Not updating right pointer after finding target.
2fill in blank
medium

Complete the code to find the last occurrence of the target in the sorted array.

DSA C++
int lastOccurrence(const vector<int>& nums, int target) {
    int left = 0, right = nums.size() - 1;
    int result = -1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {
            result = mid;
            left = mid [1] 1;
        } else if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Moving right pointer instead of left after finding target.
Using '-' instead of '+' causing wrong search direction.
3fill in blank
hard

Fix the error in the countOccurrences function to correctly calculate the count of target in the array.

DSA C++
int countOccurrences(const vector<int>& nums, int target) {
    int first = firstOccurrence(nums, target);
    if (first == -1) return 0;
    int last = lastOccurrence(nums, target);
    return last [1] first + 1;
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes incorrect count.
Forgetting to add 1 after subtraction.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

DSA C++
auto result = std::unordered_map<std::string, int>{};
for (const auto& word : words) {
    if (word.size() [1] 3) {
        result[word] = word[2]size();
    }
}
Drag options to blanks, or click blank then click option'
A>
B<
C()
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes wrong filtering.
Using '[]' instead of '()' to call size method.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths only if length is greater than 4.

DSA C++
std::unordered_map<std::string, int> result;
for (const auto& word : words) {
    if (word.size() [1] 4) {
        std::string upper_word = word;
        std::transform(upper_word.begin(), upper_word.end(), upper_word.begin(), ::[2]);
        result[[3]] = word.size();
    }
}
Drag options to blanks, or click blank then click option'
A>
Btoupper
Cupper_word
Dtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for size comparison.
Using 'tolower' instead of 'toupper' for uppercase.
Using 'word' instead of 'upper_word' as map key.