0
0
DSA C++programming~10 mins

First and Last Occurrence of Element 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 'target' in the array.

DSA C++
int firstOccurrence(vector<int>& arr, int target) {
    for (int i = 0; i < arr.size(); i++) {
        if (arr[i] == [1]) {
            return i;
        }
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Aarr[i]
Btarget
Ci
Darr.size()
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr.size() instead of target in the if condition.
Comparing i with target instead of arr[i].
2fill in blank
medium

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

DSA C++
int lastOccurrence(vector<int>& arr, int target) {
    for (int i = arr.size() - 1; i >= 0; i--) {
        if (arr[i] == [1]) {
            return i;
        }
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Aarr.size()
Bi
Ctarget
Darr[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of target in the if condition.
Comparing arr.size() with arr[i].
3fill in blank
hard

Fix the error in the binary search to find the first occurrence of 'target'.

DSA C++
int firstOccurrenceBinarySearch(vector<int>& arr, int target) {
    int low = 0, high = arr.size() - 1, result = -1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == [1]) {
            result = mid;
            high = mid - 1;
        } else if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return result;
}
Drag options to blanks, or click blank then click option'
Atarget
Bmid
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Updating wrong pointers after comparison.
4fill in blank
hard

Fill both blanks to complete the binary search for the last occurrence of 'target'.

DSA C++
int lastOccurrenceBinarySearch(vector<int>& arr, int target) {
    int low = 0, high = arr.size() - 1, result = -1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == [1]) {
            result = mid;
            low = mid [2] 1;
        } else if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return result;
}
Drag options to blanks, or click blank then click option'
Atarget
Bhigh
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' when updating low pointer.
Comparing arr[mid] with high instead of target.
5fill in blank
hard

Fill all three blanks to create a function that returns both first and last occurrences of 'target'.

DSA C++
pair<int, int> findFirstAndLast(vector<int>& arr, int target) {
    int first = -1, last = -1;
    int low = 0, high = arr.size() - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == [1]) {
            first = mid;
            high = mid [2] 1;
        } else if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    low = 0; high = arr.size() - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == [3]) {
            last = mid;
            low = mid + 1;
        } else if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return {first, last};
}
Drag options to blanks, or click blank then click option'
Atarget
B-
C+
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' when updating high pointer in first search.
Mixing up target and mid in comparisons.