Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the floor value index in a sorted array.
DSA C++
int floorIndex(const vector<int>& arr, int x) {
int low = 0, high = arr.size() - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] [1] x) {
result = mid;
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 '<=' causes missing correct floor values.
Not updating result when arr[mid] <= x.
✗ Incorrect
We want to find the floor, so if arr[mid] is less than or equal to x, we update result and move right.
2fill in blank
mediumComplete the code to find the ceil value index in a sorted array.
DSA C++
int ceilIndex(const vector<int>& arr, int x) {
int low = 0, high = arr.size() - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] [1] x) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes wrong ceil index.
Not updating result when arr[mid] >= x.
✗ Incorrect
For ceil, if arr[mid] is greater than or equal to x, we update result and move left. Here, '>' is used to check if arr[mid] is strictly greater than x.
3fill in blank
hardFix the error in the binary search condition to correctly find the floor index.
DSA C++
int floorIndex(const vector<int>& arr, int x) {
int low = 0, high = arr.size() - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] [1] x) {
result = mid;
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 '>' causes skipping valid floor elements.
Not updating result when arr[mid] <= x.
✗ Incorrect
The condition must be arr[mid] <= x to find the floor correctly.
4fill in blank
hardFill both blanks to complete the code that finds floor and ceil indices in a sorted array.
DSA C++
pair<int, int> floorAndCeil(const vector<int>& arr, int x) {
int low = 0, high = arr.size() - 1;
int floor = -1, ceil = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
floor = mid;
ceil = mid;
break;
} else if (arr[mid] [1] x) {
floor = mid;
low = mid + 1;
} else {
ceil = mid;
high = mid - 1;
}
}
return {floor, ceil};
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping floor and ceil conditions.
Not updating floor or ceil indices properly.
✗ Incorrect
For floor, use '<=' to move right and update floor. For ceil, use '>' to move left and update ceil.
5fill in blank
hardFill all three blanks to complete the code that returns a map of floor and ceil values for each query in a sorted array.
DSA C++
map<int, pair<int, int>> floorAndCeilForQueries(const vector<int>& arr, const vector<int>& queries) {
map<int, pair<int, int>> result;
for (int q : queries) {
int low = 0, high = arr.size() - 1;
int floor = -1, ceil = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == q) {
floor = mid;
ceil = mid;
break;
} else if (arr[mid] [1] q) {
floor = mid;
low = mid + 1;
} else {
ceil = mid;
high = mid - 1;
}
}
result[q] = {arr[2], arr[3];
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Accessing arr with wrong indices or missing brackets.
✗ Incorrect
Use '<=' for floor condition. Use floor and ceil indices to access arr elements for values.