JavaScript Program for Ceiling in a sorted array

An array is a linear data structure that contains objects, and in a sorted array, elements are arranged in increasing order. Given a sorted array and an integer x, we need to find the ceiling of x. The ceiling is the smallest element in the array that is greater than or equal to x. If no such element exists, we return that the ceiling does not exist.

Problem Understanding

Let's understand this with examples:

Example 1

Array = [1, 3, 5, 7, 9]
X = 19

Output: Ceiling does not exist (no element ? 19)

Example 2

Array = [1, 3, 5, 7, 9]
X = 2

Output: 3 (smallest element ? 2)

Approach 1: Linear Search

The simplest approach is to traverse the array from left to right and find the first element greater than or equal to x.

// Function to find ceiling using linear search
function findCeilingLinear(arr, x) {
    // Traverse the array from left to right
    for (let i = 0; i < arr.length; i++) {
        // If current element is >= x, it's our ceiling
        if (arr[i] >= x) {
            return i;
        }
    }
    // No ceiling found
    return -1;
}

// Test the function
const arr = [1, 2, 4, 5, 8, 19, 25];
console.log("Array:", arr);

let num = 15;
let idx = findCeilingLinear(arr, num);
if (idx == -1) {
    console.log(`Ceiling of ${num} does not exist`);
} else {
    console.log(`Ceiling of ${num} is ${arr[idx]} at index ${idx}`);
}

num = 2;
idx = findCeilingLinear(arr, num);
if (idx == -1) {
    console.log(`Ceiling of ${num} does not exist`);
} else {
    console.log(`Ceiling of ${num} is ${arr[idx]} at index ${idx}`);
}
Array: [ 1, 2, 4, 5, 8, 19, 25 ]
Ceiling of 15 is 19 at index 5
Ceiling of 2 is 2 at index 1

Time Complexity: O(N) - we may need to traverse the entire array

Space Complexity: O(1) - no extra space used

Approach 2: Binary Search (Optimized)

Since the array is sorted, we can use binary search to find the ceiling more efficiently by eliminating half the search space in each iteration.

// Function to find ceiling using binary search
function findCeilingBinary(arr, x) {
    let left = 0;
    let right = arr.length - 1;
    let result = -1;
    
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        
        // If middle element is >= x, it could be our answer
        if (arr[mid] >= x) {
            result = mid;
            right = mid - 1; // Look for smaller ceiling on left
        } else {
            left = mid + 1; // Look on right side
        }
    }
    
    return result;
}

// Test the function
const arr2 = [1, 2, 4, 5, 8, 19, 25];
console.log("Array:", arr2);

let testCases = [15, 2, 30, 1];

testCases.forEach(num => {
    let idx = findCeilingBinary(arr2, num);
    if (idx == -1) {
        console.log(`Ceiling of ${num} does not exist`);
    } else {
        console.log(`Ceiling of ${num} is ${arr2[idx]} at index ${idx}`);
    }
});
Array: [ 1, 2, 4, 5, 8, 19, 25 ]
Ceiling of 15 is 19 at index 5
Ceiling of 2 is 2 at index 1
Ceiling of 30 does not exist
Ceiling of 1 is 1 at index 0

Time Complexity: O(log N) - binary search reduces search space by half

Space Complexity: O(1) - iterative approach uses constant space

Comparison

Approach Time Complexity Space Complexity Best For
Linear Search O(N) O(1) Small arrays
Binary Search O(log N) O(1) Large sorted arrays

Conclusion

We implemented two approaches to find the ceiling in a sorted array. Binary search is preferred for large datasets due to its O(log N) time complexity, while linear search works well for smaller arrays and is easier to implement.

Updated on: 2026-03-15T23:19:01+05:30

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements