JavaScript Program for Ceiling in a sorted array


An array is a linear data structure that contains the objects and in sorted array elements are present in increasing order. We have given a sorted array and an integer x. We have to print the ceiling of the integer x from the given array. The ceiling of a sorted array with value x is the smallest element larger than or equal to x, while the floor is the largest member less than or equal to x. If the ceiling of the x does not exist in the array we have to print ‘ceiling of x does not exist in the array.

Let us assume we have given a sorted array and an integer x as

Input

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

Output

The ceiling of 19 does not exist in the array

Explanation

19 or greater than 19 there are, 0 values present in the array that are why the ceiling of 19 does not exist so the output is the ceiling of 9 is, does not exist in the array.

Input

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

Output

3

Explanation: As we know the ceiling of x is the smallest larger than or equal to x so here x is 2 and in the array 2 is not present and just a greater element to the 2 that is present in the array is 3. So the output is 3.

Approaches

Here we are going to discuss two approaches. Let’s see them below section −

Approach 1: Find the Ceiling in the Sorted Array by Linear Search

The idea of this approach is that we are going to find the element which is equal to or greater than the given number x in the given sorted array by simply traversing the array from starting to the required or end of the array.

Example

// function to implement the linear search 
function findCeiling(arr, x){	
   var len = arr.length // getting length of the array 
	
   // traversing over the given array 
   for(var i =0; i<len; i++){
   
      // if the current number is equal to current number 
      // or greater than the given number, return it
      if(x <= arr[i]){
         return i;
      }
   }
   
   // as we have travesed over the array 
   // no element is smaller as compare to the given element 	
   return -1;
}

// defining the array 
var arr = [1, 2, 4, 5, 8, 19, 25]
var num = 15
console.log("The given array is: ");
console.log(arr);

// calling the function 
var idx = findCeiling(arr,num);
if(idx == -1){
   console.log("Ceiling of given number " + num + " does not exists");
}
else{
   console.log("Ceiling of given number " + num + " in the array is " + arr[idx]);
}
num = 78
idx = findCeiling(arr,num);
if(idx == -1){
   console.log("Ceiling of the given number " + num + " does not exists");
}
else{
   console.log("Ceiling of the given number " + num + " in the array is " + arr[idx]);
}

Time Complexity: O(N), where N is the size of the array.

Space Complexity: O(1), because we are not using any extra space.

Approach 2: Find the Ceiling in the Sorted Array by Binary Search

The idea of this approach is that we are going to traverse the array but not the whole array. As we know we have given a sorted array so here we apply binary search in which we divide the array into two parts from the middle and check if the

Example

// function to implement the linear search 
function findCeiling(arr, l, h, x){

   // defining the end cases 
   if(x <= arr[l]){
      return l;
   }
   else if(x > arr[h]){
      return -1;
   }    
   var mid = (l + h)/2;   
   
   // if current element and x are same, return current index  
   if(arr[mid] == x){
      return mid;
   }
   else if(arr[mid] < x){        
      if(mid + 1 <= h && x <= arr[mid + 1]){
         return mid +1;
      }
      else{
         return findCeiling(arr, mid+1, h, x);
      }
   }
   else{
      if(mid - 1 >= l && x > arr[mid - 1]){
         return mid;
      }
      else{
         return findCeiling(arr,l, mid-1, x);
      }
   }
}

// defining the array 
var arr = [1, 2, 4, 5, 8, 19, 25]
var num = 15
console.log("The given array is: ");
console.log(arr);

// calling the function 
var idx = findCeiling(arr,0, arr.length-1, num);
if(idx == -1){
   console.log("Ceiling of given number " + num + " does not exists");
}
else{
   console.log("Ceiling of given number " + num + " in the array is " + arr[idx]);
}
num = 78
idx = findCeiling(arr, 0, arr.length-1, num);
if(idx == -1){
   console.log("Ceiling of the given number " + num + " does not exists");
}
else{
   console.log("Ceiling of the given number " + num + " in the array is " + arr[idx]);
}

Time Complexity: O(log(N)), where N is the size of the array.

Space Complexity: O(log(N)), because we are using extra space.

Conclusion

In this tutorial, we have implemented a JavaScript program to find the ceiling of the given number for the given sorted array. The ceiling of the given number is to find the number that is present in the given array and is equal to the given number, if not then just greater than the given number. If none of the elements present in the given array is greater as compared to the given number then it does not exist. We have implemented linear and binary search algorithms.

Updated on: 21-Apr-2023

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements