703852. Ceil The Floor

Easy

Ceil The Floor

Slug: ceil-the-floor2802 Difficulty: Easy Id: 703852 Topic Tags: Arrays, Data Structures Company Tags: None

Summary

The "Ceil The Floor" problem involves finding the ceiling of all elements in an array. In other words, we need to find the smallest integer that is greater than or equal to each element in the array.

Detailed Explanation

To solve this problem, we can iterate through the array and for each element, calculate its ceiling by adding 1 until we get a value that is not less than the current element. We then return an array of these ceilings as the result.

Here's a step-by-step breakdown of the solution:

  1. Initialize an empty array ceilings to store the ceilings of all elements in the input array.
  2. Iterate through each element x in the input array.
  3. For each element, calculate its ceiling by initializing a variable ceiling to x.
  4. Increment ceiling until it is greater than or equal to x. This can be done using a simple loop that adds 1 to ceiling until the condition is met.
  5. Append the calculated ceiling to the ceilings array.
  6. Return the ceilings array as the result.

Here's an ASCII art diagram illustrating the process:

          +---------------+
          |  Input Array: [3, 1, 4]  |
          +---------------+
                  |
                  v
+-----------------------+
|  Ceiling Calculation:   |
|  For each element x,    |
|  Calculate ceiling(x)   |
|  until ceiling >= x    |
|  Append ceiling to array|
+-----------------------+
                  |
                  v
          +---------------+
          |  Ceilings Array: [4, 2, 5] |
          +---------------+

Time complexity for this algorithm is O(n), where n is the size of the input array. Space complexity is also O(n) because we need to store the ceilings in an array.

Optimized Solutions

Java

View on GeeksforGeeks
public int[] ceilTheFloor(int[] arr) {
    int[] ceilings = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        int ceiling = arr[i];
        while (ceiling <= arr[i]) {
            ceiling++;
        }
        ceilings[i] = ceiling;
    }
    return ceilings;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.