Skip to content

Instantly share code, notes, and snippets.

@KeshariPiyush24
Created September 21, 2024 08:29
Show Gist options
  • Save KeshariPiyush24/fce3cf3534909663177a267181718df2 to your computer and use it in GitHub Desktop.
Save KeshariPiyush24/fce3cf3534909663177a267181718df2 to your computer and use it in GitHub Desktop.
Implement Lower Bound

Question: Implement Lower Bound

Intution:

Time Complexity: $$O(log(n))$$

Space Complexity: $$O(1)$$

Solution:

public class Solution {
    public static int lowerBound(int[] arr, int n, int x) {
        int low = 0;
        int high = n - 1;
        int ans = n;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] < x) low = mid + 1;
            else {
                ans = mid;
                high = mid - 1;
            }
        }
        return ans;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment