βœ…Floor in a Sorted Array(Contest)

Floor in a Sorted Array easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

Given a sorted array arr[] of size N without duplicates, and given a value x. Find the floor of x in given array. Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x.

Try to use binary search to solve this problem. Input First line of input contains number of testcases T. For each testcase, first line of input contains number of elements in the array and element whose floor is to be searched. Last line of input contains array elements.

Constraints: 1 ≀ T ≀ 100 1 ≀ N ≀ 10^5 1 ≀ arr[i] ≀ 10^18 0 ≀ X ≀ arr[n-1] Output Output the index of floor of x if exists, else print -1. Use 0-indexing Example Input: 3 7 0 1 2 8 10 11 12 19 7 5 1 2 8 10 11 12 19 7 10 1 2 8 10 11 12 19

Output: -1 1 3

Explanation: Testcase 1: No element less than or equal to 0 is found. So output is "-1". Testcase 2: Number less than or equal to 5 is 2, whose index is 1(0-based indexing). Testcase 3: Number less than or equal to 10 is 10 and its index is 3.

link:https://my.newtonschool.co/playground/code/4i9v4iqh2qzw/

```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner ob=new Scanner(System.in);
        int t=ob.nextInt();

        while(t-- >0){
            int n=ob.nextInt();
            long x=ob.nextInt();

            long[] arr=new long[n];
            for(int i=0; i<n;i++){
                arr[i]=ob.nextInt();

            }
            int index= floorSearch(arr, 0, n-1, x);
            System.out.println(index);
        }
    }
    static public int floorSearch(long[] arr, int low, int high, long x){
        if(low>high) return -1;
        if(x>=arr[high]) return high;
        int mid=(low+high)/2;
        if(arr[mid]==x) return mid;
        if(mid>0 && arr[mid-1]<=x && x<arr[mid]) return mid-1;
        if(x<arr[mid]) return floorSearch(arr, low, mid-1, x);
        return floorSearch(arr, mid+1, high, x);
    }
}

Last updated