0
0
DSA C++programming~10 mins

Floor and Ceil 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 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'
A>
B>=
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' causes missing correct floor values.
Not updating result when arr[mid] <= x.
2fill in blank
medium

Complete 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'
A>
B>=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes wrong ceil index.
Not updating result when arr[mid] >= x.
3fill in blank
hard

Fix 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'
A<=
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes skipping valid floor elements.
Not updating result when arr[mid] <= x.
4fill in blank
hard

Fill 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'
A<=
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping floor and ceil conditions.
Not updating floor or ceil indices properly.
5fill in blank
hard

Fill 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'
A<=
B[floor]
C[ceil]
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Accessing arr with wrong indices or missing brackets.