15 Minimum Difference Element in a Sorted Array

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ธ.ค. 2024
  • Given a sorted array, find the element in the array which has minimum difference with the given number. .
    ------------------------------------------------------------------------------------------
    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.

ความคิดเห็น • 286

  • @harendrameghwal1374
    @harendrameghwal1374 3 ปีที่แล้ว +257

    almost all of the viewers are able to think that we have to find ceil and floor to solve this problem because he made a solid base of approaching a problem if anyone follow the playlist sincerely such a finest tutor he is

    • @harikrushnasuhagiya3925
      @harikrushnasuhagiya3925 2 ปีที่แล้ว +2

      if you know ki ceil value find krni ho to last me arr[high] return krvado or floor value find krni ho to arr[low] return karvado simple....

    • @crosswalker45
      @crosswalker45 2 ปีที่แล้ว +13

      @@harikrushnasuhagiya3925 its actually opposite... if you want to find ceil then return arr[start] or arr[low], and if you want to find the floor then return arr[end] or arr[high].

    • @syedmohammadarsalan2583
      @syedmohammadarsalan2583 ปีที่แล้ว +3

      Yes me too thinking the same bro ❤

    • @Mylife013
      @Mylife013 ปีที่แล้ว

      True

    • @imPriyansh77
      @imPriyansh77 ปีที่แล้ว

      True

  • @coderboybadshah3966
    @coderboybadshah3966 4 ปีที่แล้ว +403

    another way to think about it is, find ceil and floor of the key and select between them

    • @priyanshu.tiwari
      @priyanshu.tiwari 4 ปีที่แล้ว +48

      Even I had the same thought :)

    • @abhikbhattacharya1130
      @abhikbhattacharya1130 4 ปีที่แล้ว +20

      Same here :)

    • @ytbfactsrevealed3419
      @ytbfactsrevealed3419 4 ปีที่แล้ว +78

      This also means that we can apply this approach in finding floor and ceil of an element

    • @AmandeepSingh-ot6st
      @AmandeepSingh-ot6st 4 ปีที่แล้ว +18

      Ekdum. Mai bhi yehi socha...tha....but the one taught by Aditya is 🔥is time efficient 💪

    • @himanshujain7014
      @himanshujain7014 4 ปีที่แล้ว +4

      Exactly

  • @sarthaktiwari8515
    @sarthaktiwari8515 4 ปีที่แล้ว +92

    dont say thanks. i need to say thanks to you. you helped me fall in love with coding. thank you

    • @TheAdityaVerma
      @TheAdityaVerma  4 ปีที่แล้ว +25

      Happy to hear that! bro ✌️

  • @gamingwithbablu3940
    @gamingwithbablu3940 5 หลายเดือนก่อน +3

    First me floor and ciel find krke min diff return krne ki Socha tha ... fir vdo dekha toh sir ji ne simple bianry search la concept use krke solve krr diya...
    Waaah

  • @sagestudy
    @sagestudy ปีที่แล้ว +3

    Yeh Master nahi Jaadugar hai 🪄🪄✨✨ !!! Crystal Clear Explanation.

  • @hemantranjan2297
    @hemantranjan2297 4 ปีที่แล้ว +38

    I got too much confidence in the concept of Binary Search...thank you so much, sir, for uploading this playlist.

  • @charan775
    @charan775 4 ปีที่แล้ว +180

    To break it down into the problems we already solved, need to find floor & ceil of the given element and return one of them which has min diff with the given key

    • @TheAdityaVerma
      @TheAdityaVerma  4 ปีที่แล้ว +167

      Yeah could be a solution, thats really the creative thinking I want everybody to develop.

    • @keen_dev
      @keen_dev 4 ปีที่แล้ว +18

      @@TheAdityaVerma but we can use same algo to find ceil and floor of key.Right??

    • @sanjayjoshi8807
      @sanjayjoshi8807 4 ปีที่แล้ว +5

      @@keen_dev at the end low is pointing to ceil and high is pointing to floor . but just make sure the key doesn't exists then only valid

    • @rishabsharma5307
      @rishabsharma5307 3 ปีที่แล้ว +6

      @@sanjayjoshi8807 Even if key exists it's still valid cuz in that case absolute difference will become zero

    • @ritikpatil4077
      @ritikpatil4077 3 ปีที่แล้ว

      @@sanjayjoshi8807 Use % n

  • @anuragagnihotri5238
    @anuragagnihotri5238 3 ปีที่แล้ว +6

    "Ye Koi Co-incindence nahi hai, ye har baar aisa he hoga.." , :D bahut sahi, is se pehle ki janta ko lage ki ye toh a gaya luckily, bhai ne clear kar diya abe har baar aisa he hoga :)

    • @TheAdityaVerma
      @TheAdityaVerma  3 ปีที่แล้ว +1

      😂😂😂😂😂😂😂😂😂

  • @theSeniorSDE
    @theSeniorSDE 2 ปีที่แล้ว +2

    This Binary Search playlist is dope and that is all anybody will need to crack interviews. Amazing...

  • @pranay2972
    @pranay2972 3 ปีที่แล้ว +24

    Just make sure that the key to be searched isnt lesser than a[0] and greater than a[n-1]. In such cases the high/low can go out of index(-1 or n) so before checking the abs diff see if they are valid indices.

  • @syedarfath9298
    @syedarfath9298 3 ปีที่แล้ว +6

    Oh man, you're legendary programmer, last 3 videos just saw the problem statement. I thought of the approach and coded it. You're OP in programming.

  • @Lalit_Shirsath
    @Lalit_Shirsath 3 ปีที่แล้ว +4

    This is my approach ...........
    Returns ceil of key (different than aditya sir's logic ) and calculates the min difference
    int mindiff(int a[],int low,int high,int key){
    while(low

  • @animeshmishra1235
    @animeshmishra1235 4 ปีที่แล้ว +16

    Please upload more DP videos on topics you showed in the introduction to DP video .Your way of teaching is awesome. College placements are near ,need your help desperately 😂😂

    • @madhabkafle8898
      @madhabkafle8898 2 ปีที่แล้ว +1

      where are you working now sir?

  • @Saurabh-fe2bg
    @Saurabh-fe2bg 2 ปีที่แล้ว

    Sir, I want to touch your feet, you are a teacher which every student deserves. God Bless!!

  • @ajitkumarchaurasiya3076
    @ajitkumarchaurasiya3076 4 ปีที่แล้ว +2

    Another way is to find floor and ceil..and select between them...thanks a lot ..such a great tutorial on youtube

  • @manavshah7450
    @manavshah7450 3 ปีที่แล้ว +5

    I was waiting for him to talk about floor and ceil which he taught before😁
    But this way is also nice!!

  • @sudhanshusharma9123
    @sudhanshusharma9123 4 ปีที่แล้ว +2

    Thank you for a great explanation. Ultimately, we are finding floor & ceil of key. We all had a thought of this approach while watching floor or ceil video (at least I had).One of the comments in ceil video even mentioned the same approach.

  • @98anirban
    @98anirban 4 ปีที่แล้ว +2

    Just after listening to the problem statement, the solutions strikes floor and ceil of the key would do the work as well. Thanks, Aditya! If we start thinking in such a manner, gonna ace every interview!!! :D

    • @98anirban
      @98anirban 4 ปีที่แล้ว

      *But implementing the way showed in the video would save time.

  • @md.saifulislam6528
    @md.saifulislam6528 ปีที่แล้ว

    After watching this video, satisfaction automatically brings a smile to my face (Thanks a lot bro)

  • @roushanraj8530
    @roushanraj8530 3 ปีที่แล้ว

    After you explained problem statement then i paused the video and came up with that, i can find ceil and floor then take min difference between them and return it .......
    This all i was able to think because of you all previous beautiful explanations 💯🤩😍😘😘😘

  • @purandarparitosh
    @purandarparitosh 4 ปีที่แล้ว +2

    Bhai!! Kya observation hai! Mind blowing! Kya dimag lagaya hai!

  • @mahakgupta5165
    @mahakgupta5165 4 ปีที่แล้ว +11

    Please upload more tutorials on graphs ,hashing ,backtracking....
    your videos are really helping a lot.
    Thank you so much.

    • @TheAdityaVerma
      @TheAdityaVerma  4 ปีที่แล้ว +17

      Thanks, will do!

    • @souvik5560
      @souvik5560 4 ปีที่แล้ว +1

      @@TheAdityaVerma Thank you for your initiative.
      Please upload lectures on Backtracking

  • @sauravdas7591
    @sauravdas7591 3 ปีที่แล้ว +1

    best video of Binary search playlist

  • @AdpYTExplorer
    @AdpYTExplorer ปีที่แล้ว

    What a beautiful problem and you’re a great teacher

  • @shashijaiswal5014
    @shashijaiswal5014 2 ปีที่แล้ว +2

    I think we should keep a check if low=0 so that we don't access invalid memory.
    Try to dry run for an array like 1 3 8 10 or 15 18 20 21 for key equals 12. You will understand.

  • @rahul_singh_rajput3292
    @rahul_singh_rajput3292 2 ปีที่แล้ว

    literally itna easy maine 1 ghanta socha aur using min & ans variable se kiya aur last wala arr[mid] < key and key

  • @jagratgupta8392
    @jagratgupta8392 3 ปีที่แล้ว

    Very good approach ...best ques so farr.....just using the concept of while loop breaking..ceil and floor

  • @rajatbudania6181
    @rajatbudania6181 4 ปีที่แล้ว +32

    This method can be also applied to find ceil and floor :)

  • @riyaaggarwal572
    @riyaaggarwal572 4 ปีที่แล้ว +1

    You explained very well .Sir ,Please upload lectures on binary trees as many companies also ask about them .

  • @dilsedhoni9229
    @dilsedhoni9229 2 ปีที่แล้ว

    BASE CASE:
    if ( key < arr[0] ) return arr[0];
    if ( key > arr[n-1] ) return arr[n-1];

    • @yashrdoshi
      @yashrdoshi 7 หลายเดือนก่อน

      if ( key < arr[0] ) return arr[0] - key;
      if ( key > arr[n-1] ) return key - arr[n-1];

  • @adityasrivastava7563
    @adityasrivastava7563 2 ปีที่แล้ว +1

    Also after seeing this video we can conclude that:
    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.

  • @namanlakhwani4752
    @namanlakhwani4752 4 ปีที่แล้ว +5

    The corner cases are not handled in the video.....let suppose arr={ 3,4,5 } and key= 6 then after the end of the while loop high=2 and low=3 but while comparing ( arr[ low] - key ) will give segmentation fault as arr[3] is not present in the array above (array out of bound ).
    Similarly, if the key=2 then at the end high= -1 and low= 0 and arr[high] will give the same error.
    The solution could be:
    int a = INT_MAX, b=INT_MAX;
    if ( low < N ) { a= abs( arr[low] - key ); }
    if ( high >= 0 ) { b= abs( arr[high] - key ); }
    return ( min ( a , b ) ) ;
    Correct me....if I'm wrong anywhere.

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว +1

    Nice explanation your videos are really good...please keep on making such videos.

  • @suryanshshrivastava197
    @suryanshshrivastava197 4 ปีที่แล้ว +2

    My approach was to find the floor and ceil of the given number and then check for min diff
    This seems less complicated. Thanks

  • @sanjaygarg3540
    @sanjaygarg3540 2 ปีที่แล้ว

    Amazing Explanation !! Thanks for making for these videos !

  • @SaurabhSingh-sy1pe
    @SaurabhSingh-sy1pe 2 หลายเดือนก่อน

    You could encounter an "index out of range" error in your original code in cases where the binary search leads left or right outside the valid indices of the array. Specifically, this can happen when the target is smaller than the smallest element or larger than the largest element in the array.
    use this
    if(left>=arr.length){
    return arr[right];
    }
    if(right

  • @imPriyansh77
    @imPriyansh77 ปีที่แล้ว

    Thanks Sir... Wonderful explanation

  • @sunnykakrani7830
    @sunnykakrani7830 3 ปีที่แล้ว +2

    After completing while loop there may be chance that low can point to -1 when key is lesser than all the elements in the array and high can point to length of array of key is greater than than all the elements . We need to check these two conditions.

    • @vedantchawla3765
      @vedantchawla3765 2 ปีที่แล้ว +1

      Just the reverse actually. low can point to the length of the array and high can point to -1.

  • @devangsingh2950
    @devangsingh2950 3 ปีที่แล้ว +1

    You can also use the peak element kind of concept here as the absolute difference will make a valley and you just have to find the least element in the valley.

  • @souvik5560
    @souvik5560 4 ปีที่แล้ว +2

    @Aditya Verma
    We can also use this technique to find the ceil and floor of an element.
    Please let me know if I am wrong.

  • @bestsaurabh
    @bestsaurabh 4 ปีที่แล้ว +2

    Boundary check is missing, if low crosses upper bound or high crosses lower bound, we need to put check for that. Right?

  • @coderboybadshah3966
    @coderboybadshah3966 4 ปีที่แล้ว +7

    will you to going upload playlist on patterns in Greedy Algorithms? Please try to upload videos soon as Placements session is very close.

  • @ranajitmukherjee9789
    @ranajitmukherjee9789 4 ปีที่แล้ว +1

    At first i thought of using ceil and floor concept but the one u showed was much simpler

  • @vikashkumarchaurasia1299
    @vikashkumarchaurasia1299 4 ปีที่แล้ว

    Nice explanation bro. Thanks!!!

  • @kumarrajput0001
    @kumarrajput0001 4 ปีที่แล้ว +1

    This approach could be applied to find floor/ceil/floor & ceil in one go. We don't really need to store the intermediate results as described in one of your earlier videos. if the arr[mid] == el, return mid. Otherwise, simply compare the start and end after the while loop and return accordingly.

    • @rishabsinha8749
      @rishabsinha8749 4 ปีที่แล้ว

      But u have to write two functions for floor and ceil seperately and time complexity will also be greater for the above approach and this video's approach is best because it is exactly same as binary search

    • @DroidHolicOfficial
      @DroidHolicOfficial 2 ปีที่แล้ว +3

      @@rishabsinha8749 Not necessary. You can find ceil and floor in one function itself. And time complexity will be the same as normal Binary Search
      // Floor and Ceil of an element
      const floorAndCeilOfElement = (arr, key) => {
      let start = 0;
      let end = arr.length - 1;
      let ceil = -1;
      let floor = -1;
      while(start key){
      ceil = arr[mid];
      end = mid - 1;
      }
      if(arr[mid] < key){
      floor = arr[mid]
      start = mid + 1;
      }
      }
      return [floor, ceil];
      }

  • @sahllsaharn4664
    @sahllsaharn4664 2 ปีที่แล้ว

    learnt a new concept bro thanks

  • @JohnWick-kh7ow
    @JohnWick-kh7ow 3 ปีที่แล้ว +3

    My code-
    int minDiffElement(vector& arr,int key){
    int n=arr.size();
    int left=0,right=n-1;
    int minDiff=INT_MAX;
    int ans;
    while(left

  • @NehaGupta-lf1sr
    @NehaGupta-lf1sr 4 ปีที่แล้ว

    Such a nice video... thanks for it... pls make videos on system design questions

  • @chiragsolanki1234
    @chiragsolanki1234 10 หลายเดือนก่อน

    Love your explaintion

  • @rahularyan5554
    @rahularyan5554 2 ปีที่แล้ว +1

    Sir yhn ,par nearly sorted array ka concept bhi laga skte hain na ?? Kuki neighbours hin and hogen if key present nhi rhga toh ????

  • @Stupid-uc9mj
    @Stupid-uc9mj ปีที่แล้ว

    Thank you very much😘

  • @gamingwithbablu3940
    @gamingwithbablu3940 5 หลายเดือนก่อน

    First I'm thinking to find floor and ceil of the key if jey is nit present then return mim diff😅😅but sir ji aone toh aur zyada jam lines me ans bata diya❤❤❤❤

  • @nitantjoshi9903
    @nitantjoshi9903 3 ปีที่แล้ว +1

    We need to take care of one more condition what if our key is greater than the largest element or what if our key is less than the smallest element. So in the case when the key is greater than the largest element our start would point at nth index(which is incorrect) and in the second case when our element is less than the smallest element our end would point at -1(which is incorrect)
    EX:-
    1) arr[1,4,5,8,9] X=10
    2) arr[2,4,5,8,9] X=1

    • @lannymagnet4936
      @lannymagnet4936 2 ปีที่แล้ว

      We can in this case do an if check twice for if high not less than 0 and low not greater than n+1, then calculate result. To make it work for length 1 and 2 array, just do no return. Do Binary Search for length 3 or greater.

  • @nikhiljindal4580
    @nikhiljindal4580 4 ปีที่แล้ว +2

    sir does this mean that we can return low or high for the floor and ceil problem respectively as well without storing the result in another variable?

  • @nishantingle1438
    @nishantingle1438 2 ปีที่แล้ว +2

    Basically, those 2 neighbours are floor & ceiling.

  • @rohitjadhav512
    @rohitjadhav512 3 ปีที่แล้ว

    after one by one, we are doing a postmortem on binary search🤣.... as we are getting a fundamental but new and unknown thing about this.... amazing

  • @lovishbansal9655
    @lovishbansal9655 ปีที่แล้ว +1

    please add the links of the platforms on which we can find this question or similar one.

  • @vakhariyajay2224
    @vakhariyajay2224 ปีที่แล้ว

    Thank you very much.

  • @arnabmukherjee5840
    @arnabmukherjee5840 3 ปีที่แล้ว

    WOW !!! what an observation of binary search man !!!

  • @gokulnaathbaskar9808
    @gokulnaathbaskar9808 2 ปีที่แล้ว

    That was amazing!

  • @prachi3746
    @prachi3746 3 ปีที่แล้ว

    What is wrong with iterating through array and finding the minimum diff value of key and array element
    for(int i=0;iMath.abs(a[i]-k)){
    val=Math.abs(a[i]-k); ans=a[i];
    }}
    return ans;

  • @DroidHolicOfficial
    @DroidHolicOfficial 2 ปีที่แล้ว

    // Floor and Ceil of an element
    const floorAndCeilOfElement = (arr, key) => {
    let start = 0;
    let end = arr.length - 1;
    let ceil = -1;
    let floor = -1;
    while(start key){
    ceil = arr[mid];
    end = mid - 1;
    }
    if(arr[mid] < key){
    floor = arr[mid]
    start = mid + 1;
    }
    }
    return [floor, ceil];
    }
    //Main Logic
    const minimumDifferenceElement = (arr, key) => {
    let [floor,ceil] = floorAndCeilOfElement(arr,key);
    let diff1 = Math.abs(floor - key);
    let diff2 = Math.abs(ceil - key);
    return diff1 < diff2 ? floor : ceil;
    }

  • @techzone0131
    @techzone0131 3 ปีที่แล้ว

    Pen and papper combination is also best 👍👍👍👍

  • @blackeagle4443
    @blackeagle4443 2 ปีที่แล้ว +1

    If I am not wrong then we can use this approach to find floor and ceil as well??

    • @kirtikhohal3313
      @kirtikhohal3313 2 ปีที่แล้ว

      yes ofc

    • @cse048harshkumawat6
      @cse048harshkumawat6 2 ปีที่แล้ว

      @@kirtikhohal3313 never
      E.g. 1,2,3,4 key =5
      At last end is at 3 and start is at 4 4 that means according to you floor is 3 and ceil is 4 that's not correct for this floor is 4 and ceil not defined 😅

  • @sharmanihal99
    @sharmanihal99 2 ปีที่แล้ว

    start=0
    end=len(arr)-1
    mid=-1
    diff=sys.maxint
    while start

  • @aneeshrajanthakur5317
    @aneeshrajanthakur5317 3 ปีที่แล้ว +1

    Great video. Just had a doubt. We can use this same technique while finding the ceil and floor for an element right? I mean one of his videos explains it but that solution is too long. This seems to be a smarter way to do it

  • @abhishekhorton79
    @abhishekhorton79 3 ปีที่แล้ว

    another way to think about this ques is you just have to find the target element but while doing it keep a variable and change the absolute diff again and again that will give you an answer ;)

  • @yashrathore7507
    @yashrathore7507 3 ปีที่แล้ว

    We can find floor and ceil as arr[end] and arr[start] respectively.

  • @guddukumar-v8z5l
    @guddukumar-v8z5l 7 หลายเดือนก่อน +1

    simply find the floor index and then find the minimum value from floor index and floor+1 index.

  • @sudhadevi6692
    @sudhadevi6692 ปีที่แล้ว +1

    Almost Bowdown❤

  • @SiddharthSaklaniK_PE_
    @SiddharthSaklaniK_PE_ 2 ปีที่แล้ว

    we can also find lower and upper bound of key and subtract key from it who ever gave minimum return that element

  • @jayshree7574
    @jayshree7574 4 ปีที่แล้ว +1

    really amazing playlist!!!

  • @sourabhpatil4002
    @sourabhpatil4002 4 ปีที่แล้ว +5

    The issue with this code is when you have to search for a element which is greater than your Max element of array.. Then list index goes out of range..
    Please correct me if I am wrong

    • @k_co_KristidharPandit
      @k_co_KristidharPandit 3 ปีที่แล้ว +1

      @Sourabh Patil
      I thought the same ...then what to do u think?

    • @MansiThakkar139
      @MansiThakkar139 3 ปีที่แล้ว

      Only one index will be valid at that time and you only need to compare it with that index to get the min difference.

    • @venkateshkulkarni2227
      @venkateshkulkarni2227 3 ปีที่แล้ว +7

      you could check before performing binary search:
      if(targetarr[n-1]) return target-arr[n-1]

    • @shruthidasa7139
      @shruthidasa7139 2 ปีที่แล้ว

      @@venkateshkulkarni2227 Thanks, there is only a slight mistake .As we have to return the element,
      if(targetarr[n-1]) return arr[n-1];

  • @ankitborbora3158
    @ankitborbora3158 ปีที่แล้ว

    Can I find floor and ceiling by this method as well?

  • @kusmaurya3675
    @kusmaurya3675 3 ปีที่แล้ว

    If key is greater than all the elements present in the array then one edge case should be there to check for the low index value (which points to max size of array) i.e., it should be less than max size of array in that case we return ABS( a[high] - key).
    Same for the lowest value also high point at index "-1" so ABS( a[low] - key).
    Plz Correct me if I am wrong?

  • @RahulDwari
    @RahulDwari 6 หลายเดือนก่อน

    Just Brilliant

  • @ayanagrawal
    @ayanagrawal 3 ปีที่แล้ว +3

    To all those suggesting floor and ceil method
    O(logn) < O(2*logn)

  • @prasunanand4330
    @prasunanand4330 3 ปีที่แล้ว

    Then using this we can find ceil and floor too right using start and end index/ value respectively?

  • @roliagrawal3124
    @roliagrawal3124 2 ปีที่แล้ว

    Have a doubt why we do need to get the absolute difference between high and low both? As it will be always number - arr[low] for what case we are taking number - arr[high] also?

  • @govindlohar6816
    @govindlohar6816 ปีที่แล้ว

    well explaination

  • @BruceLee-bt2mq
    @BruceLee-bt2mq 2 ปีที่แล้ว +1

    i just watched the video till 2 :32 ,and my solution approach is min(diff(k,floor of k),diff(k,ceil of k))

    • @kirtikhohal3313
      @kirtikhohal3313 2 ปีที่แล้ว +3

      Haha same, and I was so sure that he's also gonna tell this, but he made the solution more easy and less lengthy.

  • @Noone-kl6sc
    @Noone-kl6sc 3 ปีที่แล้ว +1

    thanks bro

  • @cmcatholic1798
    @cmcatholic1798 2 ปีที่แล้ว

    13:00 ye climax toh rohit shetty ki movies se bhi bada tha🤣🤣🤣

  • @DeviDevi-yr2sv
    @DeviDevi-yr2sv 3 ปีที่แล้ว

    Sir plZ backtrack or graph daal dijiye..internships start ho chili h..ab tak thik se prepare nhi hua....aap daaldenge toh next year tak achee se prepare hojega

  • @willturner3440
    @willturner3440 4 ปีที่แล้ว

    Find ceil and floor then print minimum between them, is this approach is correct?

  • @brijeshsharma216
    @brijeshsharma216 4 ปีที่แล้ว +2

    there's one thing we need to check .. lets say array is 10,12,14,16,19 and key is 9.. In this case arr[end] at the end of while loop will give segmentation fault as end will be -1.. so before doing key-arr[start] , key-arr[end] after while loop we should check start, end are >=0 &&

  • @csdoit
    @csdoit 3 ปีที่แล้ว

    Bhaiya can we apply the same algorithm for ceil and floor??

  • @jeetintyagi
    @jeetintyagi 4 ปีที่แล้ว

    bhai bhot pyaara samajhaate ho

  • @ytbfactsrevealed3419
    @ytbfactsrevealed3419 4 ปีที่แล้ว +2

    Can't we use the same approach to find the floor and ceil of an element ??

    • @NikhilKumar-of5gb
      @NikhilKumar-of5gb 4 ปีที่แล้ว

      It's possible only when key is not present

    • @rahulgopinath8301
      @rahulgopinath8301 4 ปีที่แล้ว

      @@NikhilKumar-of5gb But isn't it still possible? Since if the key is present then the floor/ceil of the key is simply the key itself.

    • @rishabsinha8749
      @rishabsinha8749 4 ปีที่แล้ว

      But u have to write two functions for floor and ceil seperately and time complexity will also be greater for the above approach and this video's approach is best because it is exactly same as binary search

  • @aadyantagrawal8804
    @aadyantagrawal8804 ปีที่แล้ว

    if the key is greater than the last element of the array we can simply return the last element of the array right ? as that would be the element with minimum difference ?

  • @vinaybansal2430
    @vinaybansal2430 4 ปีที่แล้ว

    can i just use simple binary search and compare the difference with middle element?
    becos it will automatically include left and right elements..

  • @kanhav03
    @kanhav03 3 ปีที่แล้ว

    Hey what if the key is greater than the last element or lower than the first element, then we will not get both bounded numbers to choose from.

  • @ShubhamGupta-xm4og
    @ShubhamGupta-xm4og 4 ปีที่แล้ว

    I am beginner just wanted to know can we solve it by ceil and floor, I have done this by ceil and floor and code has rum also. I want there no exception in this na?

  • @ulhasch1969
    @ulhasch1969 4 ปีที่แล้ว +2

    seems it would fail if the key is outside of range, for ex - arr = [1,3,8,10,15] and key =20. it will fail. instead output should be 5.

    • @avneetsng
      @avneetsng 4 ปีที่แล้ว +1

      no it will not , at the end high will be 4 and low will be 5 . and and will be key-arr[high] => 20 -15 =5.

    • @bishnoi6409
      @bishnoi6409 3 ปีที่แล้ว +1

      in this case ,you have to check if key value less than a[0] then return a[0]-key....and if key>a[n-1] then return key-a[n-1].

  • @lmaoded7505
    @lmaoded7505 2 ปีที่แล้ว

    Can we do it this way:
    1. Maintain a variable ```result = INT_MAX``` which is supposed to store the minimum absolute difference
    2. At each step, find the **absolute** difference of ```arr[mid] - target```
    3. Compare it with the variable ```result``` => ```result = min(result, abs(arr[mid] - target))
    4. Let the algorithm converfe using the standard logic of Binary Search (when we eliminate the search space)
    5. The final answer should be stored in ```result``` variable

  • @mohitsharma1882
    @mohitsharma1882 4 ปีที่แล้ว +3

    bhaiya your videos are very helpful , but in dynamic programming playlist plzz upload on minimum edit distance also because you skipped that. After studying from your videos I can correlate all the things that's why I am requesting for this.

    • @TheAdityaVerma
      @TheAdityaVerma  4 ปีที่แล้ว +4

      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 !! ✌️❤️

    • @asifnawaz3454
      @asifnawaz3454 4 ปีที่แล้ว +1

      @@TheAdityaVerma brother its may ............. where r u

    • @souvik5560
      @souvik5560 4 ปีที่แล้ว

      @@TheAdityaVerma Your videos are really helpful 👍
      Kindly upload backtracking lectures.

  • @riteshgupta6438
    @riteshgupta6438 4 ปีที่แล้ว

    This will give an error when the index of low or high goes out of bounds.
    Taking the same array , arr = [1,3,8,10,15] and key = 20.
    The low index will be at 5 which will give an index error after completing the binary search.

    • @pranavmishra3212
      @pranavmishra3212 4 ปีที่แล้ว +2

      We can avoid this by just checking whether arr[n-1] >= key or not.

  • @Prashantkumar-pn6qq
    @Prashantkumar-pn6qq 4 ปีที่แล้ว

    Hi Aditya, what if low/high goes out of array bounds? Could be the case if we have x = 16 in your example....low will be equal to n.

    • @namanlakhwani4752
      @namanlakhwani4752 4 ปีที่แล้ว +2

      Yes....the corner cases are not handled in the video.....let suppose arr={ 3,4,5 } and key= 6 then after the end of the while loop high=2 and low=3 but while comparing ( arr[ low] - key ) will give segmentation fault as arr[3] is not present in the array above.
      Similarly, if the key=2 then at the end high= -1 and low= 0 and arr[high] will give the same error.
      The solution could be:
      int a = INT_MAX, b=INT_MAX;
      if ( low < N ) { a= abs( arr[low] - key ); }
      if ( high >= 0 ) { b= abs( arr[high] - key ); }
      return ( min ( a , b ) ) ;

  • @ambujmishra183
    @ambujmishra183 ปีที่แล้ว

    legend++😍😍

  • @SACHINSINGH-re5ft
    @SACHINSINGH-re5ft 2 ปีที่แล้ว

    bhai plz try to put some question links related to the questions so that it will be easy for us to hover that question