Ceiling of every element in same array
Last Updated :
15 May, 2025
Given an array of integers arr[]. The task is to find the ceiling for each element in the array. The "ceiling" of an element is defined as the closest greater than or equal value present in the array (i.e., excluding the element itself). If no such element exists, return -1 for that position.
Examples:
Input: arr[] = [9, 5, 11, 10, 20, 12, 10]
Output: [10, 9, 12, 10, -1, 20, 10]
Explanation : The output array mainly contains smallest greater or equal element for every element. In case of single occurrence, we get greater and in case of multiple occurrences, we consider the same value.
Input: arr[] = [6, 11, 7, 8, 20, 12]
Output: [7, 12, 8, 11, -1, 20]
Input: arr[] = [3, 4, 2, 1, 6]
Output: [4, 6, 3, 2, -1]
[Naive Approach] Using Two Nested Loops - O(n^2) Time and O(1) Space
The idea is to compare each element with every other element in the array to find its next greater or equal element. The thought process is to manually search for a value that is ≥ current element (arr[i]) and is the smallest such value, i.e., its ceiling. We will be using two nested loops for this: the outer loop fixes the current element and the inner loop searches for its ceiling.
C++
// C++ program to find the ceiling for each element
// using Naive Approach
#include <iostream>
#include <vector>
using namespace std;
// Function to find the ceiling for each element
vector<int> findCeilings(vector<int> &arr) {
int n = arr.size();
vector<int> res(n);
for (int i = 0; i < n; i++) {
// Initialize ceiling as -1 for
// current element
int ceiling = -1;
for (int j = 0; j < n; j++) {
// Skip comparing element with itself
if (i == j) {
continue;
}
// Check if arr[j] is greater than or equal
// to arr[i] and either ceil is not set or
// arr[j] is smaller than current ceil
if (arr[j] >= arr[i]) {
if (ceiling == -1 || arr[j] < ceiling) {
ceiling = arr[j];
}
}
}
res[i] = ceiling;
}
return res;
}
// Driver code
int main() {
vector<int> arr = {10, 5, 11, 10, 20, 12};
vector<int> res = findCeilings(arr);
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
}
Java
// Java program to find the ceiling for each element
// using Naive Approach
class GfG {
// Function to find the ceiling for each element
static int[] findCeilings(int[] arr) {
int n = arr.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
// Initialize ceiling as -1 for
// current element
int ceiling = -1;
for (int j = 0; j < n; j++) {
// Skip comparing element with itself
if (i == j) {
continue;
}
// Check if arr[j] is greater than or equal
// to arr[i] and either ceil is not set or
// arr[j] is smaller than current ceil
if (arr[j] >= arr[i]) {
if (ceiling == -1 || arr[j] < ceiling) {
ceiling = arr[j];
}
}
}
res[i] = ceiling;
}
return res;
}
// Driver code
public static void main(String[] args) {
int[] arr = {10, 5, 11, 10, 20, 12};
int[] res = findCeilings(arr);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
}
}
Python
# Python program to find the ceiling for each element
# using Naive Approach
# Function to find the ceiling for each element
def findCeilings(arr):
n = len(arr)
res = [0] * n
for i in range(n):
# Initialize ceiling as -1 for
# current element
ceiling = -1
for j in range(n):
# Skip comparing element with itself
if i == j:
continue
# Check if arr[j] is greater than or equal
# to arr[i] and either ceil is not set or
# arr[j] is smaller than current ceil
if arr[j] >= arr[i]:
if ceiling == -1 or arr[j] < ceiling:
ceiling = arr[j]
res[i] = ceiling
return res
# Driver code
if __name__ == "__main__":
arr = [10, 5, 11, 10, 20, 12]
res = findCeilings(arr)
for i in range(len(res)):
print(res[i], end=" ")
C#
// C# program to find the ceiling for each element
// using Naive Approach
using System;
class GfG {
// Function to find the ceiling for each element
static int[] findCeilings(int[] arr) {
int n = arr.Length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
// Initialize ceiling as -1 for
// current element
int ceiling = -1;
for (int j = 0; j < n; j++) {
// Skip comparing element with itself
if (i == j) {
continue;
}
// Check if arr[j] is greater than or equal
// to arr[i] and either ceil is not set or
// arr[j] is smaller than current ceil
if (arr[j] >= arr[i]) {
if (ceiling == -1 || arr[j] < ceiling) {
ceiling = arr[j];
}
}
}
res[i] = ceiling;
}
return res;
}
// Driver code
static void Main(string[] args) {
int[] arr = {10, 5, 11, 10, 20, 12};
int[] res = findCeilings(arr);
for (int i = 0; i < res.Length; i++) {
Console.Write(res[i] + " ");
}
}
}
JavaScript
// JavaScript program to find the ceiling for each element
// using Naive Approach
// Function to find the ceiling for each element
function findCeilings(arr) {
let n = arr.length;
let res = new Array(n);
for (let i = 0; i < n; i++) {
// Initialize ceiling as -1 for
// current element
let ceiling = -1;
for (let j = 0; j < n; j++) {
// Skip comparing element with itself
if (i === j) {
continue;
}
// Check if arr[j] is greater than or equal
// to arr[i] and either ceil is not set or
// arr[j] is smaller than current ceil
if (arr[j] >= arr[i]) {
if (ceiling === -1 || arr[j] < ceiling) {
ceiling = arr[j];
}
}
}
res[i] = ceiling;
}
return res;
}
// Driver code
let arr = [10, 5, 11, 10, 20, 12];
let res = findCeilings(arr);
for (let i = 0; i < res.length; i++) {
process.stdout.write(res[i] + " ");
}
[Expected Approach] Using Sorting + Binary Search - O(n*log(n)) Time and O(n) Space
The idea is to use a sorted copy of the array to efficiently locate the minimum greater element (ceiling) using binary search. We first sort the array and then for each element, perform a binary search to get its index in the sorted version. If the element occurs multiple times, we return the same element as its ceiling, else return the next larger element.
Steps to implement the above idea:
- Create a sorted copy of the original array to help locate elements in ascending order.
- Loop through the original array and find the index of each element in the sorted array using binary search.
- Move one step forward from the found index to search for the minimum greater element (ceiling).
C++
// C++ program to find the ceiling for
// each element using sorted copy and binary search
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to perform binary search for current element
int findIndex(vector<int> &sorted, int target) {
int low = 0, high = sorted.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (sorted[mid] == target) {
return mid;
}
else if (sorted[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
// Function to find the ceiling for each element
vector<int> findCeilings(vector<int> &arr) {
int n = arr.size();
vector<int> res(n);
// Handle single element case
if (n == 1) {
res[0] = -1;
return res;
}
// Create sorted copy of the array
vector<int> sorted = arr;
sort(sorted.begin(), sorted.end());
// Traverse each element in original array
for (int i = 0; i < n; i++) {
// Please note that idx would always be valid
// as we are searching in the same array
int idx = findIndex(sorted, arr[i]);
idx++;
// Check if duplicate exists before current index
if (idx - 2 >= 0 && sorted[idx - 2] == arr[i]) {
res[i] = arr[i];
}
else if (idx < n) {
res[i] = sorted[idx];
}
else {
res[i] = -1;
}
}
return res;
}
// Driver code
int main() {
vector<int> arr = {10, 5, 11, 10, 20, 12};
vector<int> res = findCeilings(arr);
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
}
Java
// Java program to find the ceiling for
// each element using sorted copy and binary search
import java.util.*;
class GfG {
// Function to perform binary search for current element
static int findIndex(int[] sorted, int target) {
int low = 0, high = sorted.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (sorted[mid] == target) {
return mid;
}
else if (sorted[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
// Function to find the ceiling for each element
static int[] findCeilings(int[] arr) {
int n = arr.length;
int[] res = new int[n];
// Handle single element case
if (n == 1) {
res[0] = -1;
return res;
}
// Create sorted copy of the array
int[] sorted = Arrays.copyOf(arr, arr.length);
Arrays.sort(sorted);
// Traverse each element in original array
for (int i = 0; i < n; i++) {
// Please note that idx would always be valid
// as we are searching in the same array
int idx = findIndex(sorted, arr[i]);
idx++;
// Check if duplicate exists before current index
if (idx - 2 >= 0 && sorted[idx - 2] == arr[i]) {
res[i] = arr[i];
}
else if (idx < n) {
res[i] = sorted[idx];
}
else {
res[i] = -1;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {10, 5, 11, 10, 20, 12};
int[] res = findCeilings(arr);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
}
}
Python
# Python program to find the ceiling for
# each element using sorted copy and binary search
# Function to perform binary search for current element
def findIndex(sorted, target):
low = 0
high = len(sorted) - 1
while low <= high:
mid = (low + high) // 2
if sorted[mid] == target:
return mid
elif sorted[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
# Function to find the ceiling for each element
def findCeilings(arr):
n = len(arr)
res = [0] * n
# Handle single element case
if n == 1:
res[0] = -1
return res
# Create sorted copy of the array
sorted_arr = sorted(arr)
# Traverse each element in original array
for i in range(n):
# Please note that idx would always be valid
# as we are searching in the same array
idx = findIndex(sorted_arr, arr[i])
idx += 1
# Check if duplicate exists before current index
if idx - 2 >= 0 and sorted_arr[idx - 2] == arr[i]:
res[i] = arr[i]
elif idx < n:
res[i] = sorted_arr[idx]
else:
res[i] = -1
return res
if __name__ == "__main__":
arr = [10, 5, 11, 10, 20, 12]
res = findCeilings(arr)
for x in res:
print(x, end=" ")
C#
// C# program to find the ceiling for
// each element using sorted copy and binary search
using System;
class GfG {
// Function to perform binary search for current element
static int findIndex(int[] sorted, int target) {
int low = 0, high = sorted.Length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (sorted[mid] == target) {
return mid;
}
else if (sorted[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
// Function to find the ceiling for each element
static int[] findCeilings(int[] arr) {
int n = arr.Length;
int[] res = new int[n];
// Handle single element case
if (n == 1) {
res[0] = -1;
return res;
}
// Create sorted copy of the array
int[] sorted = (int[])arr.Clone();
Array.Sort(sorted);
// Traverse each element in original array
for (int i = 0; i < n; i++) {
// Please note that idx would always be valid
// as we are searching in the same array
int idx = findIndex(sorted, arr[i]);
idx++;
// Check if duplicate exists before current index
if (idx - 2 >= 0 && sorted[idx - 2] == arr[i]) {
res[i] = arr[i];
}
else if (idx < n) {
res[i] = sorted[idx];
}
else {
res[i] = -1;
}
}
return res;
}
static void Main() {
int[] arr = {10, 5, 11, 10, 20, 12};
int[] res = findCeilings(arr);
for (int i = 0; i < res.Length; i++) {
Console.Write(res[i] + " ");
}
}
}
JavaScript
// JavaScript program to find the ceiling for
// each element using sorted copy and binary search
// Function to perform binary search for current element
function findIndex(sorted, target) {
let low = 0, high = sorted.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (sorted[mid] === target) {
return mid;
}
else if (sorted[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
// Function to find the ceiling for each element
function findCeilings(arr) {
let n = arr.length;
let res = new Array(n);
// Handle single element case
if (n === 1) {
res[0] = -1;
return res;
}
// Create sorted copy of the array
let sorted = [...arr].sort((a, b) => a - b);
// Traverse each element in original array
for (let i = 0; i < n; i++) {
// Please note that idx would always be valid
// as we are searching in the same array
let idx = findIndex(sorted, arr[i]);
idx++;
// Check if duplicate exists before current index
if (idx - 2 >= 0 && sorted[idx - 2] === arr[i]) {
res[i] = arr[i];
}
else if (idx < n) {
res[i] = sorted[idx];
}
else {
res[i] = -1;
}
}
return res;
}
// Driver Code
let arr = [10, 5, 11, 10, 20, 12];
let res = findCeilings(arr);
for (let i = 0; i < res.length; i++) {
process.stdout.write(res[i] + " ");
}
Similar Reads
Floor of every element in same array Given an array of integers, find the closest smaller or same element for every element. If all elements are greater for an element, then print -1. We may assume that the array has at least two elements. Examples: Input : arr[] = {10, 5, 11, 10, 20, 12} Output : 10 -1 10 10 12 11 Note that there are
14 min read
Ceiling in right side for every element in an array Given an array of integers, find the closest greater element for every element. If there is no greater element then print -1 Examples: Input : arr[] = {10, 5, 11, 10, 20, 12} Output : 10 10 12 12 -1 -1 Input : arr[] = {50, 20, 200, 100, 30} Output : 100 30 -1 -1 -1 A simple solution is to run two ne
4 min read
Counting frequencies of array elements Given an array arr[] of non-negative integers which may contain duplicate elements. Return the frequency of each distinct element present in the array.Examples: Input : arr[] = [10, 20, 10, 5, 20]Output : [[10, 2], [20, 2], [ 5, 1]]Explanation: Here, 10 occurs 2 times, 20 occurs 2 times, and 5 occur
10 min read
Ceiling in a sorted array Given a sorted array and a value x, find index of the ceiling of x. The ceiling of x is the smallest element in an array greater than or equal to x. Note: In case of multiple occurrences of ceiling of x, return the index of the first occurrence.Examples : Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x
13 min read
Print all Distinct (Unique) Elements in given Array Given an integer array arr[], print all distinct elements from this array. The given array may contain duplicates and the output should contain every element only once.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: {12, 10, 9, 45, 2}Input: arr[] = {1, 2, 3, 4, 5}Output: {1, 2, 3, 4,
11 min read