Check if Floor and Ceil Diff in an Array is Same for a Number

Last Updated : 25 Jul, 2026

Given a sorted array arr[] and an integer x, determine whether x is at the same absolute distance from its floor and ceil i.e. the difference between x and its floor is the same as the difference between its ceil and x.

Note: The floor of x is the largest element in the array that is less than or equal to x, and the ceil of x is the smallest element in the array that is greater than or equal to x.

Examples:

Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 5
Output: true
Explanation: The floor of 5 is 2 and the ceil is 8. The distances are |5 - 2| = 3 and |8 - 5| = 3, which are equal. Hence, the answer is true.

Input: arr[] = [1, 2, 5, 7, 8, 11, 12, 15], x = 9
Output: false
Explanation: The floor of 9 is 8 and the ceil is 11. The distances are |9 - 8| = 1 and |11 - 9| = 2, which are not equal. Hence, the answer is false.

Try It Yourself
redirect icon

[Naive Approach] Linear Scan - O(n) Time and O(1) Space

The idea is to traverse the sorted array once to find the floor and the ceil of x. Since the array is sorted, every element less than or equal to x can be a candidate for the floor, while the first element greater than or equal to x becomes the ceil. After obtaining both values, compare their absolute differences from x.

Working of Approach:

  • Initialize variables to store the floor and the ceil.
  • Traverse the array from left to right.
  • Update the floor whenever the current element is less than or equal to x.
  • Store the first element greater than or equal to x as the ceil.
  • If either the floor or the ceil does not exist, return false.
  • Compare the absolute difference between x and the floor with the absolute difference between the ceil and x.
  • Return true if the differences are equal; otherwise, return false.
C++
#include <bits/stdc++.h>
using namespace std;

bool isBalanced(vector<int>& arr, int x) {
    int floor = -1, ceil = -1;

    // Find the floor and ceil of x
    for (int num : arr) {
        if (num <= x) {
            floor = num;
        }

        if (num >= x && ceil == -1) {
            ceil = num;
        }
    }

    // Floor or ceil doesn't exist
    if (floor == -1 || ceil == -1) {
        return false;
    }

    // Check if x is at the same absolute difference
    return abs(x - floor) == abs(ceil - x);
}

int main() {
    vector<int> arr = {1, 2, 8, 10, 10, 12, 19};
    int x = 5;

    cout << (isBalanced(arr, x) ? "true" : "false");

    return 0;
}
Java
public class GFG {

    static boolean isBalanced(int[] arr, int x) {
        int floor = -1, ceil = -1;

        // Find the floor and ceil of x
        for (int num : arr) {
            if (num <= x) {
                floor = num;
            }

            if (num >= x && ceil == -1) {
                ceil = num;
            }
        }

        // Floor or ceil doesn't exist
        if (floor == -1 || ceil == -1) {
            return false;
        }

        // Check if x is at the same absolute difference
        return Math.abs(x - floor) == Math.abs(ceil - x);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 8, 10, 10, 12, 19};
        int x = 5;

        System.out.println(isBalanced(arr, x));
    }
}
Python
def isBalanced(arr, x):
    floor = -1
    ceil = -1

    # Find the floor and ceil of x
    for num in arr:
        if num <= x:
            floor = num

        if num >= x and ceil == -1:
            ceil = num

    # Floor or ceil doesn't exist
    if floor == -1 or ceil == -1:
        return False

    # Check if x is at the same absolute difference
    return abs(x - floor) == abs(ceil - x)


if __name__ == "__main__":
    arr = [1, 2, 8, 10, 10, 12, 19]
    x = 5

    print(isBalanced(arr, x))
C#
using System;

class GFG {

    static bool isBalanced(int[] arr, int x) {
        int floor = -1, ceil = -1;

        // Find the floor and ceil of x
        foreach (int num in arr) {
            if (num <= x) {
                floor = num;
            }

            if (num >= x && ceil == -1) {
                ceil = num;
            }
        }

        // Floor or ceil doesn't exist
        if (floor == -1 || ceil == -1) {
            return false;
        }

        // Check if x is at the same absolute difference
        return Math.Abs(x - floor) == Math.Abs(ceil - x);
    }

    static void Main() {
        int[] arr = {1, 2, 8, 10, 10, 12, 19};
        int x = 5;

        Console.WriteLine(isBalanced(arr, x));
    }
}
JavaScript
function isBalanced(arr, x) {
    let floor = -1;
    let ceil = -1;

    // Find the floor and ceil of x
    for (const num of arr) {
        if (num <= x) {
            floor = num;
        }

        if (num >= x && ceil === -1) {
            ceil = num;
        }
    }

    // Floor or ceil doesn't exist
    if (floor === -1 || ceil === -1) {
        return false;
    }

    // Check if x is at the same absolute difference
    return Math.abs(x - floor) === Math.abs(ceil - x);
}

// Driver Code
const arr = [1, 2, 8, 10, 10, 12, 19];
const x = 5;

console.log(isBalanced(arr, x));

Output
true

[Better Approach] Binary Search using Lower Bound and Upper Bound - O(log n) Time and O(1) Space

The idea is to use binary search to efficiently find the floor and the ceil of x in the sorted array. The lower bound gives the first element greater than or equal to x, which is the ceil, while the element just before the upper bound gives the largest element less than or equal to x, which is the floor. Once both values are obtained, compare their absolute differences from x.

Working of Approach:

  • Find the ceil of x using lower bound.
  • Find the floor of x using upper bound - 1.
  • If either the floor or the ceil does not exist, return false.
  • Compare the absolute difference between x and the floor with the absolute difference between the ceil and x.
  • Return true if the differences are equal; otherwise, return false.
C++
#include <bits/stdc++.h>
using namespace std;

bool isBalanced(vector<int>& arr, int x) {

    // Find the ceil of x
    auto ceilIt = lower_bound(arr.begin(), arr.end(), x);

    // Find the floor of x
    auto floorIt = upper_bound(arr.begin(), arr.end(), x);

    // Ceil or floor doesn't exist
    if (ceilIt == arr.end() || floorIt == arr.begin()) {
        return false;
    }

    --floorIt;

    // Check if x is at the same absolute difference
    return abs(x - *floorIt) == abs(*ceilIt - x);
}

int main() {
    vector<int> arr = {1, 2, 8, 10, 10, 12, 19};
    int x = 5;

    cout << (isBalanced(arr, x) ? "true" : "false");

    return 0;
}
Java
public class GFG {

    static boolean isBalanced(int[] arr, int x) {

        // Find the ceil of x
        int ceilIdx = lowerBound(arr, x);

        // Find the floor of x
        int floorIdx = upperBound(arr, x) - 1;

        // Ceil or floor doesn't exist
        if (ceilIdx == arr.length || floorIdx < 0) {
            return false;
        }

        // Check if x is at the same absolute difference
        return Math.abs(x - arr[floorIdx]) == Math.abs(arr[ceilIdx] - x);
    }

    static int lowerBound(int[] arr, int x) {
        int left = 0, right = arr.length;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] < x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    static int upperBound(int[] arr, int x) {
        int left = 0, right = arr.length;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] <= x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 8, 10, 10, 12, 19};
        int x = 5;

        System.out.println(isBalanced(arr, x));
    }
}
Python
def lowerBound(arr, x):
    left, right = 0, len(arr)

    while left < right:
        mid = left + (right - left) // 2

        if arr[mid] < x:
            left = mid + 1
        else:
            right = mid

    return left


def upperBound(arr, x):
    left, right = 0, len(arr)

    while left < right:
        mid = left + (right - left) // 2

        if arr[mid] <= x:
            left = mid + 1
        else:
            right = mid

    return left


def isBalanced(arr, x):

    # Find the ceil of x
    ceilIdx = lowerBound(arr, x)

    # Find the floor of x
    floorIdx = upperBound(arr, x) - 1

    # Ceil or floor doesn't exist
    if ceilIdx == len(arr) or floorIdx < 0:
        return False

    # Check if x is at the same absolute difference
    return abs(x - arr[floorIdx]) == abs(arr[ceilIdx] - x)


if __name__ == "__main__":
    arr = [1, 2, 8, 10, 10, 12, 19]
    x = 5

    print(isBalanced(arr, x))
C#
using System;

class GFG {

    static bool IsBalanced(int[] arr, int x) {

        // Find the ceil of x
        int ceilIdx = LowerBound(arr, x);

        // Find the floor of x
        int floorIdx = UpperBound(arr, x) - 1;

        // Ceil or floor doesn't exist
        if (ceilIdx == arr.Length || floorIdx < 0) {
            return false;
        }

        // Check if x is at the same absolute difference
        return Math.Abs(x - arr[floorIdx]) == Math.Abs(arr[ceilIdx] - x);
    }

    static int LowerBound(int[] arr, int x) {
        int left = 0, right = arr.Length;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] < x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    static int UpperBound(int[] arr, int x) {
        int left = 0, right = arr.Length;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] <= x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    static void Main() {
        int[] arr = {1, 2, 8, 10, 10, 12, 19};
        int x = 5;

        Console.WriteLine(IsBalanced(arr, x));
    }
}
JavaScript
function lowerBound(arr, x) {
    let left = 0, right = arr.length;

    while (left < right) {
        const mid = left + Math.floor((right - left) / 2);

        if (arr[mid] < x) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }

    return left;
}

function upperBound(arr, x) {
    let left = 0, right = arr.length;

    while (left < right) {
        const mid = left + Math.floor((right - left) / 2);

        if (arr[mid] <= x) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }

    return left;
}

function isBalanced(arr, x) {

    // Find the ceil of x
    const ceilIdx = lowerBound(arr, x);

    // Find the floor of x
    const floorIdx = upperBound(arr, x) - 1;

    // Ceil or floor doesn't exist
    if (ceilIdx === arr.length || floorIdx < 0) {
        return false;
    }

    // Check if x is at the same absolute difference
    return Math.abs(x - arr[floorIdx]) === Math.abs(arr[ceilIdx] - x);
}

// Driver Code
const arr = [1, 2, 8, 10, 10, 12, 19];
const x = 5;
console.log(isBalanced(arr, x));

Output
true

[Expected Approach] Binary Search using Lower Bound - O(log n) Time and O(1) Space

The idea is to use only the lower bound to determine both the floor and the ceil of x. The lower bound returns the first element greater than or equal to x, which is the ceil. If this element is equal to x, then both the floor and the ceil are x. Otherwise, the previous element is the floor. Once the floor and the ceil are obtained, compare their absolute differences from x.

Working of Approach:

  • Find the first element greater than or equal to x using lower bound.
  • If no such element exists, return false.
  • If the element found is equal to x, return true since both the floor and the ceil are x.
  • If the first element is greater than x and no previous element exists, return false.
  • Otherwise, use the previous element as the floor and the current element as the ceil.
  • Compare the absolute difference between x and the floor with the absolute difference between the ceil and x.
  • Return true if the differences are equal; otherwise, return false.
C++
#include <bits/stdc++.h>
using namespace std;

bool isBalanced(vector<int>& arr, int x) {

    // Find the ceil of x
    auto it = lower_bound(arr.begin(), arr.end(), x);

    // Ceil doesn't exist
    if (it == arr.end()) {
        return false;
    }

    // If x is present, floor = ceil = x
    if (*it == x) {
        return true;
    }

    // Floor doesn't exist
    if (it == arr.begin()) {
        return false;
    }

    // Check if x is at the same absolute difference
    return abs(x - *(it - 1)) == abs(*it - x);
}

int main() {
    vector<int> arr = {1, 2, 8, 10, 10, 12, 19};
    int x = 5;

    cout << (isBalanced(arr, x) ? "true" : "false");

    return 0;
}
Java
public class GFG {

    static boolean isBalanced(int[] arr, int x) {

        // Find the ceil of x
        int idx = lowerBound(arr, x);

        // Ceil doesn't exist
        if (idx == arr.length) {
            return false;
        }

        // If x is present, floor = ceil = x
        if (arr[idx] == x) {
            return true;
        }

        // Floor doesn't exist
        if (idx == 0) {
            return false;
        }

        // Check if x is at the same absolute difference
        return Math.abs(x - arr[idx - 1]) == Math.abs(arr[idx] - x);
    }

    static int lowerBound(int[] arr, int x) {
        int left = 0, right = arr.length;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] < x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 8, 10, 10, 12, 19};
        int x = 5;
        
        System.out.println(isBalanced(arr, x));
    }
}
Python
def lowerBound(arr, x):
    left, right = 0, len(arr)

    while left < right:
        mid = left + (right - left) // 2

        if arr[mid] < x:
            left = mid + 1
        else:
            right = mid

    return left


def isBalanced(arr, x):

    # Find the ceil of x
    idx = lowerBound(arr, x)

    # Ceil doesn't exist
    if idx == len(arr):
        return False

    # If x is present, floor = ceil = x
    if arr[idx] == x:
        return True

    # Floor doesn't exist
    if idx == 0:
        return False

    # Check if x is at the same absolute difference
    return abs(x - arr[idx - 1]) == abs(arr[idx] - x)


if __name__ == "__main__":
    arr = [1, 2, 8, 10, 10, 12, 19]
    x = 5
    
    print(isBalanced(arr, x))
C#
using System;

class GFG {

    static bool isBalanced(int[] arr, int x) {

        // Find the ceil of x
        int idx = LowerBound(arr, x);

        // Ceil doesn't exist
        if (idx == arr.Length) {
            return false;
        }

        // If x is present, floor = ceil = x
        if (arr[idx] == x) {
            return true;
        }

        // Floor doesn't exist
        if (idx == 0) {
            return false;
        }

        // Check if x is at the same absolute difference
        return Math.Abs(x - arr[idx - 1]) == Math.Abs(arr[idx] - x);
    }

    static int LowerBound(int[] arr, int x) {
        int left = 0, right = arr.Length;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] < x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    static void Main() {
        int[] arr = {1, 2, 8, 10, 10, 12, 19};
        int x = 5;

        Console.WriteLine(isBalanced(arr, x));
    }
}
JavaScript
function lowerBound(arr, x) {
    let left = 0, right = arr.length;

    while (left < right) {
        const mid = left + Math.floor((right - left) / 2);

        if (arr[mid] < x) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }

    return left;
}

function isBalanced(arr, x) {

    // Find the ceil of x
    const idx = lowerBound(arr, x);

    // Ceil doesn't exist
    if (idx === arr.length) {
        return false;
    }

    // If x is present, floor = ceil = x
    if (arr[idx] === x) {
        return true;
    }

    // Floor doesn't exist
    if (idx === 0) {
        return false;
    }

    // Check if x is at the same absolute difference
    return Math.abs(x - arr[idx - 1]) === Math.abs(arr[idx] - x);
}

// Driver Code
const arr = [1, 2, 8, 10, 10, 12, 19];
const x = 5;

console.log(isBalanced(arr, x));

Output
true
Comment