Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr.size() instead of target in the if condition.
Comparing i with target instead of arr[i].
✗ Incorrect
We compare each element arr[i] with the target to find the first occurrence.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of target in the if condition.
Comparing arr.size() with arr[i].
✗ Incorrect
We compare each element arr[i] with the target starting from the end to find the last occurrence.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Updating wrong pointers after comparison.
✗ Incorrect
We compare arr[mid] with the target to find the first occurrence using binary search.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' when updating low pointer.
Comparing arr[mid] with high instead of target.
✗ Incorrect
When arr[mid] equals target, update result and move low pointer to mid + 1 to find last occurrence.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' when updating high pointer in first search.
Mixing up target and mid in comparisons.
✗ Incorrect
Use binary search twice: first to find first occurrence (move high to mid - 1), then last occurrence (move low to mid + 1).