10 Find Floor of an element in a Sorted Array

Sdílet
Vložit
  • čas přidán 16. 03. 2020
  • FIND FLOOR OF AN ELEMENT IN A SORTED ARRAY:
    Given a sorted array and a value x, the floor of x is the largest element in array smaller than or equal to x. Write efficient functions to find floor of x.
    Example:
    Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 5
    Output : 2
    2 is the largest element in arr[] smaller than 5.
    PROBLEM STATEMENT LINK:https:www.geeksforgeeks.org/floor-i...
    PLAYLIST LINK: • Binary Search | Interv... .
    ------------------------------------------------------------------------------------------
    Here are some of the gears that I use almost everyday:
    🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
    👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
    💻 : My gaming laptop: amzn.to/3yjcn23
    📱 : My Ipad: amzn.to/39yEMGS
    ✏️ : My Apple Pencil: amzn.to/3kMnKYf
    🎧 : My Headphones: amzn.to/3kMOzM7
    💺 : My Chair: amzn.to/385weqR
    🛋 : My Table: amzn.to/3kMohtd
    ⏰ : My Clock: amzn.to/3slFUV3
    🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
    PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

Komentáře • 137

  • @kabboghosh1853
    @kabboghosh1853 Před 4 lety +59

    best series on binary search

  • @rishukumarsingh3926
    @rishukumarsingh3926 Před rokem +10

    Legend , using exactly same template with minor changes for almost every problem. Respect++

  • @anjanaygupta2554
    @anjanaygupta2554 Před 3 lety +38

    just wanted to say, you're doing a great service to the society! :)

  • @adityasrivastava7563
    @adityasrivastava7563 Před 2 lety +21

    Think about approach given below it may help:
    floor : end pointer after the whole execution of loop,if mid is not exist.
    ceil : start pointer after the whole execution of loop,if mid is not exist.

    • @MOHITRANA-to7rf
      @MOHITRANA-to7rf Před rokem +7

      same i was thinking if target == arr[mid] return mid else left

    • @-SumitKumar
      @-SumitKumar Před 8 měsíci

      Yaa we should return start if elementnis not available 😅

  • @ashutoshyadav9549
    @ashutoshyadav9549 Před 3 lety +3

    Thanks a lot boss for such amazing content

  • @neerajtadhiyal3152
    @neerajtadhiyal3152 Před 2 lety +4

    the first question I was able to do on my own on the bases of previoous questions Thanks biro...

  • @Priyam6F4
    @Priyam6F4 Před rokem +1

    Approach
    The problem is little bit modified when compared to finding the floor/ceil of an element using binary search. Usually to find the floor,(Here I am assuming you know the basic binary search concept of finding the floor and ceil) we consider the rightmost search space.
    if(nums[mid]

  • @kumarsaurabhraj2538
    @kumarsaurabhraj2538 Před 3 lety +5

    There's another way to do this, an element would be the floor of K if that element is less than K and the element next to is greater than 4 or is the end of the array.
    Here is the code would look like
    int solve(long long int arr[], int i, int j, int k, int N) {
    if (i>j)
    return -1;
    int mid = (i + j)/2;
    if (arr[mid]==k)
    return mid;
    if (arr[mid] < k && (arr[mid+1] > k || mid+1==N))
    return mid;
    if (kj)
    return -1;
    int mid = (i + j)/2;
    if (arr[mid] k || mid+1==N))
    return mid;
    if (k

  • @rishabhgupta9846
    @rishabhgupta9846 Před rokem

    good teaching style,able to solve by myself

  • @karanbhati7552
    @karanbhati7552 Před 2 lety +11

    Here's my simple Soln:
    int findFloor(vector a, long long n, long long x){
    int l = 0;
    int h = n-1;

    while(l

  • @hritwiksom3032
    @hritwiksom3032 Před 3 lety +6

    Will lower bound work here if I need index instead of element?

  • @prachurjyabasistha4682
    @prachurjyabasistha4682 Před 4 lety +22

    Another good idea maybe is to find the index of first greater element than x(using binary search) and then index-1 will be the answer.

    • @imshivendra
      @imshivendra Před 2 lety

      int findFloor(vector v, long long n, long long x){

      // Your code here
      int start=0;
      int end=n-1;
      int floor=-1;

      while(start

    • @chetanraghavv
      @chetanraghavv Před 2 lety +4

      ​@@imshivendra
      1. We have to return index not the element so change v[mid] to mid under the if condition
      2. floor is a C++ keyword so change it to Floor or something else

    • @usmanaliansari5223
      @usmanaliansari5223 Před rokem

      u can use uprBOND and lwrbnd FUNCTION

    • @myyoutubeisthis
      @myyoutubeisthis Před rokem

      @@chetanraghavv int start=0,mid,res=-1,end=n-1;
      while(startv[mid])
      {
      res=mid;
      start=mid+1;
      }
      else
      end=mid-1;
      }
      return res;

  • @anjaliprajapati6921
    @anjaliprajapati6921 Před 2 lety +1

    such a helpful series...thanks a lot😇

    • @imshivendra
      @imshivendra Před 2 lety

      int findFloor(vector v, long long n, long long x){

      // Your code here
      int start=0;
      int end=n-1;
      int floor=-1;

      while(start

    • @Ak-um1yg
      @Ak-um1yg Před rokem +1

      @@imshivendra we need to return INDEX and you are returning the ELEMENT

    • @kunalboxer4432
      @kunalboxer4432 Před rokem

      @@imshivendra bro else se to terminate kro kamsekum

  • @sejalrathore7080
    @sejalrathore7080 Před 3 lety +5

    Bhaiya please upload more videos.The way you teach is incredible 👌.I loved watching your video ❤️✨. Please bhaiya upload more video🙏🙏.

    • @imshivendra
      @imshivendra Před 2 lety

      int findFloor(vector v, long long n, long long x){

      // Your code here
      int start=0;
      int end=n-1;
      int floor=-1;

      while(start

  • @vaibhavdeshmukh7900
    @vaibhavdeshmukh7900 Před 3 lety

    Sir paper prr jo video aap banate ho... Vo bahut sahi hai... Please paper prr banao....
    Aur aapke video sach me bahut jayda helpful hai.....
    Thankyou for making such an awesome videos for us!

  • @barunsingh2016
    @barunsingh2016 Před 3 lety +19

    I always confused between ceil and floor concept thank you for connecting it with the room concept...

    • @imshivendra
      @imshivendra Před 2 lety

      int findFloor(vector v, long long n, long long x){

      // Your code here
      int start=0;
      int end=n-1;
      int floor=-1;

      while(start

  • @dhikshithrm
    @dhikshithrm Před 2 lety +1

    we can just return hi or end when lo == hi or start == end in normal binary search, and hi+1 for ceil.

  • @anubhavgupta2224
    @anubhavgupta2224 Před 4 lety +6

    Nice vedio Sir. Sir please upload backtracking questions vedio as well

    • @TheAdityaVerma
      @TheAdityaVerma  Před 4 lety +9

      Will upload more video in the month of may !! Till then keep on sharing and help this channel grow + that keeps me motivated to make more videos !! ✌️❤️

    • @MdKais-fx5jl
      @MdKais-fx5jl Před 3 lety

      @@TheAdityaVerma please make a playlist of graph also....sir❤❤❤❤❤

  • @arpit27953
    @arpit27953 Před 2 lety +3

    I think its better to use upper and lower bound for floor and ceil respectively.

  • @user-zl9kn7od9c
    @user-zl9kn7od9c Před rokem +1

    My Observation ,
    Because the array is sorted so
    if(mid>5) then the mid-1 should be less than 5 then return mid -1 else continue loop e=mid-1;
    if(a[mid]>5){
    if(a[mid-1]

  • @monikachand8819
    @monikachand8819 Před 2 lety

    But can someone tell me the reason why it's not working whenever I'm trying to take the user input for cin>>arr[i]..?

  • @rohanmane7423
    @rohanmane7423 Před rokem +2

    floor and ceil of an soted array solution.
    public class Main
    {
    public static void main(String[] args) {
    int arr[] = {6,7,8,9,10};
    int key = 5;
    int c=ceil(arr,key);
    int f =floor(arr,key);
    System.out.println("ceil is "+c);
    System.out.println("floor is "+f);
    }
    private static int ceil(int arr[], int key)
    {
    int lo = 0;
    int hi = arr.length-1;
    int ans = -1;
    while(lokey)
    {
    ans = arr[mid];
    hi = mid - 1;
    }
    if(arr[mid] < key)
    {
    lo = mid + 1;
    }
    }
    return ans;
    }
    private static int floor(int arr[], int key)
    {
    int lo = 0;
    int hi = arr.length-1;
    int ans =-1;
    while(lo

  • @ROHIT-gv7xk
    @ROHIT-gv7xk Před 3 lety

    great

  • @deveshgaykar6755
    @deveshgaykar6755 Před 4 lety

    baki kai sorting method kaha pe use honge to??

  • @physicsrox184
    @physicsrox184 Před 2 lety +3

    I think We can simply run Binary Search and after the while loop we'll get end pointing the floor and start pointing the ceil, Assuming target isn't present and the loop runs until start doesn't become larger than end.

  • @ayusharyan683
    @ayusharyan683 Před 2 lety

    int ans(-1);
    if (key < arr[0]) //If our key is less than the smallest element in the array
    ans = -1;
    else if (key > arr[n - 1]) //If our key is greater than the largest element in the array
    ans = arr[n - 1];
    else
    { //Simple binary search just take care of the condition
    int low(0), high(n - 1);
    while (low

  • @pranjaljain7659
    @pranjaljain7659 Před 2 lety

    BESTTTT !!!!!!!!

  • @saahilsharma6537
    @saahilsharma6537 Před 2 lety +2

    JAVA SOLUTION:-
    public static int FloorOfNumber(int[] arr,int key) {
    int n = arr.length;
    int start = 0, end = n - 1;
    int max=0;
    while(startkey)
    {
    end=mid-1;
    }
    if(arr[mid]

  • @arjundevmishra7207
    @arjundevmishra7207 Před rokem

    similar to finding first index of element 👍👍👍👍

  • @kumarsaurabhraj2538
    @kumarsaurabhraj2538 Před 3 lety

    Here is a recursive CPP implementation of the approach shown in this video
    int solve(long long int arr[], int i, int j, int k, int candidate) {
    if (i>j)
    return candidate;
    int mid = (i + j)/2;
    int c = candidate;
    if (arr[mid]arr[candidate])
    c = mid;
    }
    if (k

    • @imshivendra
      @imshivendra Před 2 lety +1

      int findFloor(vector v, long long n, long long x){

      // Your code here
      int start=0;
      int end=n-1;
      int floor=-1;

      while(start

    • @pranav288
      @pranav288 Před 2 lety

      @@imshivendra same problem bro, 2nd test case failing (input too large)

  • @koushilbadam9186
    @koushilbadam9186 Před 2 lety

    simple solution in python
    #floor of the element in sorted array
    class Solution:
    def bs(self,nums,target):
    l=0
    r=len(nums)-1
    res=0
    for i in range(len(nums)):
    mid=l+(r-l)//2
    if nums[mid]target:
    r=mid-1
    print(res)


    t1=Solution()
    t1.bs([11,12,15,18,2,5,6,8],16)

  • @VishalKumar-xr4nm
    @VishalKumar-xr4nm Před rokem

    In Simple way we have to return upper_bound( x ) -1;
    class Solution{
    static int findFloor(long arr[], int n, long x)
    {
    return upper_bound(arr,x) - 1;
    }
    static int upper_bound(long[] arr,long x){
    int low = 0, high = arr.length;
    while(low

  • @niveditakrishna2447
    @niveditakrishna2447 Před 3 lety +1

    Hi Aditya..I tried the solution on leet code
    public class Solution {
    public int SearchInsert(int[] nums, int target) {

    int start=0,end=nums.Length-1,mid;
    int result=-1;
    while(start

    • @imshivendra
      @imshivendra Před 2 lety

      give the link of leetcode problem . I don't fiond this question on leetcode

  • @kirtikhohal3313
    @kirtikhohal3313 Před 2 lety +3

    For those who are considering this logic is a little bit complex, actually it's not and its more logical and impressive to the interviewer(because you can actually explain how you're finding the floor value), rather than just performing binary search and returning mid or high index, coz fortunately it's giving the answer.
    I've tried briefing down this concept in five steps:-
    1) Declare a res variable, initialize it with -1, in case no smaller value is found in the whole array, then this -1 will be returned.
    2) Start with finding the middle element. Compare the middle element with target value.
    3) If middle is equal to the target, you don't have to do anything, just store the res=mid, and break, come out of the loop.
    4) If middle element is less than the target element, then this middle element can be the possible answer for the floor , so just store this middle into res. But as the definition of floor function says that apart from being smaller than the target element , it should also be the greatest among candidate answers. Since, we've found an element which is smaller than the target, we cannot stop here, we need to check whether there's an element which is greater than this middle, but still smaller than the target. So, whenever we want to move to the greater side, we always move towards right.
    so do, first=mid+1, if any greater element is found than this middle, then we'll store that in res, otherwise, we've saved our previous answer in the res variable.
    5) If middle element is greater than the target, then you know for sure that this middle cannot be a candidate for floor value, and you'll not store this into res. You'll move towards smaller values, that is towards left, that is last=mid-1. And keep on searching greatest smaller value for the target in the left part only.
    Here's the snippet:-
    int findFloor(vector v, long long n, long long x){
    int res=-1;
    if(n==1 && v[0]x)
    return -1;
    else
    {
    int first=0,last=n-1,mid;
    while(first

    • @spoidermon4323
      @spoidermon4323 Před 2 lety +1

      if(v[mid]==x)return mid;
      we can directly return mid nah

    • @kirtikhohal5025
      @kirtikhohal5025 Před 2 lety +1

      @@spoidermon4323 No, we cannot just return it, and then come out of the loop. I've explained in point no. 4 and 5, that why we've to traverse the either part of the array, even we've found one of the candidate element.

    • @snegar1677
      @snegar1677 Před 2 lety +3

      Thank you for the English explanation

  • @imshivendra
    @imshivendra Před 2 lety

    Aditya Bhaiya Your intuition is not working here. aapka logic gfg pe multiple test cases me fail ho raha hai
    int findFloor(vector v, long long n, long long x){

    // Your code here
    int start=0;
    int end=n-1;
    int floor=-1;

    while(start

  • @akhileshshahare
    @akhileshshahare Před 2 lety

    Can we not do normal BS iteration and then return the value at "end" index (i.e when x is not the floor) ?

  • @palakgupta4104
    @palakgupta4104 Před 4 lety +5

    what if there are duplicate elements, then which index should be return.
    ex- 2,4,5,6,10,10,10,10,10,10,10
    for finding floor of 10 ...should it be 5 or 4?

  • @souravmehraniya2832
    @souravmehraniya2832 Před 2 lety

    are bhai khaha gayab ha tu , or videos dal de yr , bohot help hori ha ekdum bhai

  • @messi_codes
    @messi_codes Před 2 lety +1

  • @anuragshah516
    @anuragshah516 Před 4 lety +4

    Returning mid if element is present otherwise returning mid Or mid - 1 instead of -1 after the while loop in the binary search approach will do the same work.

    • @AJ-gg1jc
      @AJ-gg1jc Před 3 lety

      Yes bro you are right , i solved it at gfg.... and one more thing that should be checked very first, if(a[n-1])>=key) so return n-1 or a[n-1].

    • @anuragshah516
      @anuragshah516 Před 3 lety +1

      @@AJ-gg1jc I think this case will be automatically handled no need for checking.

    • @AJ-gg1jc
      @AJ-gg1jc Před 3 lety

      @@anuragshah516 yeah but i got something wrong then i checked my code and updated that.....
      I was juat testing on custome input..... it is possible that , code was right

    • @gurnoorsingh2954
      @gurnoorsingh2954 Před 3 lety

      Bhai kya apni approach explain kar sakte ho ?
      Mujhe samajh nhi aaya

    • @AJ-gg1jc
      @AJ-gg1jc Před 3 lety +1

      ​@@gurnoorsingh2954 Bro just do one thing with simple binary search code..... print its mid after each iteration, you will find that the last mid you printed out, is your answer here if the element is present otherwise "mid-1" is your answer.... Now you may ask that why are we checking mid-1 , because a[mid] can be larger than the given key element.... so as we know a[mid-1] will always be smaller or floor. ...... see my code here...>>>>
      Accepted on gfg,....... function which return floor(given key element)...
      #include
      using namespace std;
      int flor(int a[],int n,int key){
      if(a[n-1]

  • @gauravmandal007
    @gauravmandal007 Před 3 lety

    best

  • @akshaybhuradia9199
    @akshaybhuradia9199 Před 5 měsíci

    function floor_in_sorted_array(arr, ele){
    let start = 0;
    let end = arr.length -1;
    let res =-1;
    while(start= ele){
    end = mid -1
    }
    else if(arr[mid]

  • @mdkashifraza2603
    @mdkashifraza2603 Před 2 lety

    //floor of an elelment in sorted array
    int Floor_inSorted(int a[] , int key , int n)
    {
    int l = 0;
    int h = n-1;
    int mid;
    int result;
    while(l

  • @Rajjain_
    @Rajjain_ Před 3 lety

    Practice link?

  • @getakashverma19
    @getakashverma19 Před 10 měsíci

    JAVA Solution
    // Function to find floor of x
    // arr: input array
    // n is the size of array
    static int findFloor(long arr[], int n, long x)
    {
    int res = -1;
    int l = 0;
    int r = n-1;
    while(l

  • @-SumitKumar
    @-SumitKumar Před 8 měsíci

    Yaa we should return start if elementnis not available 😅😅

  • @panavkumar9009
    @panavkumar9009 Před 2 lety +2

    This became a little complex. Rather than this we could have simply used binary search to search for the element. If the element would have been present then the binary search would have simply returned the index of that element. Otherwise at the end of the loop in the binary search instead of returning -1 at the end we will compare the element at the start index and the element at the end index and return whichever is the smaller of them.

    • @kirtikhohal3313
      @kirtikhohal3313 Před 2 lety +1

      yeah, you're right, we can make this more simple, just keep on iterating through the while loop, when the control will come out of the loop, just check that ar[mid]

  • @VIRAL-VIDZzzOfficial
    @VIRAL-VIDZzzOfficial Před rokem

    how to write code please

  • @imshivendra
    @imshivendra Před 2 lety

    int findFloor(vector v, long long n, long long x){
    // Your code here
    int start=0;
    int end=n-1;
    int floor=-1;
    while(start

    • @dhikshithrm
      @dhikshithrm Před 2 lety +2

      in gfg the question is asking to find index of floor you are returning the element, return mid instead of v[mid]

  • @ojasvichaudhary8724
    @ojasvichaudhary8724 Před 3 lety

    end always give you floor value index if element is not present

  • @anujjain9273
    @anujjain9273 Před 2 lety +6

    int findFloor(long long int arr[], int N, long long int K) {
    long long int low=0;
    long long int high = N-1;
    int res=-1;
    while(lowK)
    {
    high = mid-1;
    }
    else if(arr[mid]

  • @gigachad6844
    @gigachad6844 Před 2 lety

    Use simple Binary Search, and outside while instead of returning -1, return low;

    • @boredpotato6366
      @boredpotato6366 Před 2 lety +1

      Nice solution, did your husband give it to you?

    • @gigachad6844
      @gigachad6844 Před 2 lety +1

      @@boredpotato6366 gonna cry?

    • @imshivendra
      @imshivendra Před 2 lety +1

      your code is not working for multiple test cases

    • @gigachad6844
      @gigachad6844 Před 2 lety +1

      @@imshivendra I've literally submitted my code on leetcode

    • @imshivendra
      @imshivendra Před 2 lety +2

      @@gigachad6844 Actuall I didn't find this problem n leetcode if you have problem link can you plese give it to me

  • @manjarimistry5941
    @manjarimistry5941 Před rokem

    Intuitive solution :
    class Solution{
    // Function to find floor of x
    // arr: input array
    // n is the size of array
    static int findFloor(long arr[], int n, long x)
    {
    int start=0,end=n-1;
    while(start

    • @akankshasingh704
      @akankshasingh704 Před rokem

      Hey could you please explain why are you returning the end?

  • @mayankmittal7963
    @mayankmittal7963 Před rokem

    Loop not stopping......

  • @deekshantgoyal1375
    @deekshantgoyal1375 Před rokem

    int l=0;
    int r=nums.length-1;
    while(l=key)
    r=m-1;
    else
    l=m+1;
    }
    return l;

  • @onkarpreetsingh5299
    @onkarpreetsingh5299 Před 4 lety +3

    #include
    using namespace std;
    int main()
    {
    int n,ele;
    cin>>n;int a[n];
    for(int i=0;i>a[i];
    coutele;
    int start =0,end=n-1,res;
    while(start

  • @prbai3844
    @prbai3844 Před 2 lety +1

    #include
    using namespace std;
    int binarySearch(int arr[], int n, int key)
    {
    int res;
    int l=0;
    int r=n-1;
    while(l key)
    {

    r=mid-1;
    }
    else if(key>arr[mid])
    {
    l=mid+1;
    res= arr[mid];
    }
    }
    // We reach here when element is not present in array
    return res;
    }
    // Driver Code
    int main()
    {
    int arr[] = {-3, 2, 10, 39, 45};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key=40;
    cout

  • @myyoutubeisthis
    @myyoutubeisthis Před rokem

    int start=0,mid,res=-1,end=n-1;
    while(startv[mid])
    {
    res=mid;
    start=mid+1;
    }
    else
    end=mid-1;
    }
    return res;

  • @adityajoshi3922
    @adityajoshi3922 Před 3 lety

    this was my solution will it work??
    int floor(int a[],int n,int t){
    int l=0,h=n-1;
    while(lt){
    h=mid-1;
    }else{
    l=mid+1;
    }
    }
    return -1;
    }

  • @adityavaste3732
    @adityavaste3732 Před 2 lety +2

    int binarySearch1(int arr[],int len,int target){
    int start = 0;
    int end = len-1;
    while(start target) end = mid -1;
    else start = mid + 1;
    }
    return end;
    }

  • @umanggupta5803
    @umanggupta5803 Před 3 lety

    // Online C++ compiler to run C++ program online
    #include
    #include
    using namespace std;
    int floor(vector &arr, int k)
    {
    int start=0, end=arr.size()-1,mid;
    while(start

  • @manikanth.8507
    @manikanth.8507 Před 3 lety

    //c++ code:
    int findFloor(vector v, long long n, long long x){
    // Your code here
    long long start=0;
    long long end=n-1;
    long long mid;
    long long index=-1;;
    while(start

  • @nishantgupta6902
    @nishantgupta6902 Před 11 měsíci +1

    int findFloor(vector v, long long n, long long x){
    // Your code here
    int s=0;
    int e=n-1;
    int res = -1;
    while(s

  • @vaibhavyt2715
    @vaibhavyt2715 Před 4 měsíci

    Python Solution:-
    class Solution:
    #User function Template for python3
    #Complete this function
    def findFloor(self,A,N,X):
    #Your code here
    result = -1
    start = 0
    end = N-1
    while start

  • @varungoel6981
    @varungoel6981 Před 3 lety +3

    Simple and easy understandable code
    #include
    using namespace std;
    int main(){
    int arr[]={1,2,3,4,8,10,12,19};
    int n=sizeof(arr)/sizeof(arr[0]);
    int target=11;
    int s,e,m;
    s=0;
    e=n-1;
    while(s

  • @Ankur-zd4db
    @Ankur-zd4db Před 2 lety

    Please bhaiya videos banate raho please 🙏

  • @sailendrachettri8521
    @sailendrachettri8521 Před 2 lety +1

    CODE
    #include
    using namespace std;
    /*
    Floor in the sense - if we are searching the floor of 5 and that value is present in array then return that value. Otherwise return key-1 value;
    */
    void print_arr(int arr[], int size);
    int find_floor(int arr[], int size, int key);
    int main(){
    int size, key; cin>>key>>size;
    int arr[size];
    for(int i = 0; i < size; i++){
    cin>>arr[i];
    }
    print_arr(arr, size);
    int res = find_floor(arr, size, key);
    if(res

  • @TensionNOTPratik
    @TensionNOTPratik Před 2 lety +3

    // C++ Solution With Binary Search →
    int Floor(int arr[], int size, int key){
    int start = 0;
    int end = size-1;
    int mid= start + (end - start / 2);
    int ans=0;
    while(start arr[mid]){
    ans = arr[mid];
    start = mid + 1;
    }else if( key < arr[mid]){
    end = mid - 1;
    }
    mid = start + (end - start / 2);
    }
    return ans;
    }

    • @imshivendra
      @imshivendra Před 2 lety

      int findFloor(vector v, long long n, long long x){

      // Your code here
      int start=0;
      int end=n-1;
      int floor=-1;

      while(start

    • @imshivendra
      @imshivendra Před 2 lety

      Bhai maine bhi yahi cde likha hai lekin ye code kaam nahi kar raha hai GFG par. you can see my code

    • @TensionNOTPratik
      @TensionNOTPratik Před 2 lety

      @@imshivendra
      try this one
      int findFloor(vector v, long long n, long long x){
      using ll=long long;
      ll start = 0;
      ll end = n-1;
      ll mid = start + (end - start/2);
      ll ans = -1;

      while(start v[mid]){
      ans = mid;
      start = mid + 1;
      }else if(x < v[mid]){
      end = mid - 1;
      }
      mid = start + (end - start/2);
      }
      return ans;
      }

    • @imshivendra
      @imshivendra Před 2 lety

      Thanks brother I got solution

  • @malickadeenhassan2248
    @malickadeenhassan2248 Před 3 lety

    try writing code in a compiler :)

  • @messi_codes
    @messi_codes Před 2 lety

    💬💬💬💬💬💬

  • @nitinpraksh3494
    @nitinpraksh3494 Před 3 lety

    full code:
    public static int findFloorOfElement(int[] arr, int k) {
    int start = 0;
    int end = arr.length - 1;
    while (start

  • @shubhamkshirsagar4511

    static int findFloor(long arr[], int n, long x)
    {
    int s=0;
    int e=n-1;
    int min=-1;
    while(s

  • @kittuchowdary9599
    @kittuchowdary9599 Před 2 lety

    Please explain in English

  • @ayushkumar-wt2wm
    @ayushkumar-wt2wm Před 2 lety

    int floor(int a[],int target,int n)
    {
    int low=0,high=n-1,res=-1;
    while(lown>>key;
    int a[n];
    for(int i=0;i>a[i];
    }
    int ans=floor(a,key,n);
    cout

  • @BikkiMahato
    @BikkiMahato Před 3 lety +1

    public class FindFloorElementSortedArray {
    public static void main(String[] args) {
    int arr[] = { 1, 2, 3, 4, 8, 9, 10, 11, 12 };
    int k = 5;
    System.out.println(solve(arr, k));
    }
    static int solve(int arr[], int k) {
    int left = 0;
    int right = arr.length - 1;
    int ans = -1;
    while (left k) {
    right = mid - 1;
    } else {
    ans = arr[mid];
    left = mid + 1;
    }
    }
    return ans;
    }
    }