First And Last Position Of An Element In A Sorted Array | FREE DSA Course in JAVA | Lecture 54

Sdílet
Vložit
  • čas přidán 22. 01. 2023
  • This question has been asked in the interview of Facebook, Google, and Microsoft and is a Leetcode problem number 34.
    The question reads - Given an array of integer numbers sorted in ascending order, find the starting and ending position of a given target value.
    if the target is not found in the array return [-1,-1].
    You must write an algorithm with O(log n) runtime complexity.
    Now a simple brute force approach will be to run a loop from the first index and return the index when the element is found first for a first position or starting position.
    And, for the ending or last position run a loop from the last index.
    But in this method, we will compromise with the time complexity given.
    So one thing which is very clear is that we have to use the binary search algorithm for this program.
    Let's see how a binary search approach with little changes can give the desired results as asked in this program.
    Subscribe to our channel for regular updates on the dsa course and click on the bell icon to never miss an update from our dsa course.
    Data Structures and Algorithms Free Course (Learn DSA Without Paise) Playlist - • Data Structures And Al...
    For more information, fill this form: forms.gle/8eiUmM92Fx563Aen9
    or call us at 8884881203
    Facebook: / thetapacademy
    Instagram: / tapacademy_online
    Linkedin: / 73820805
    Website: www.thetapacademy.com
    #coding #dsa #dsacourse #java #javainterview #array #binarysearch

Komentáře • 23

  • @azhargayawi
    @azhargayawi Před 28 dny

    i gurantee nobody on youtube can explain better than him❤

  • @arjungillela9565
    @arjungillela9565 Před rokem +4

    Sir, You made it this simple, directly entered to the brain, Thank you

    • @TAPACADEMY
      @TAPACADEMY  Před rokem +2

      You are very welcome. Do subscribe to the channel.

  • @bhavanimohan6809
    @bhavanimohan6809 Před 10 měsíci +1

    You are such a genius made me understand very clearly when ever I have doubt on DSA I just immediately landed on your channel.

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

    very nice and simple explanation instead, easy to understand

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

    Best explanation sir thank u so much❤️

  • @musicallymad9
    @musicallymad9 Před 6 měsíci

    SUCH A WONDERFUL EXPLAINATION BY YOU SIR. THANKS A LOT SIR. THE WAY YOU EXPLAIN EVERY PROBLEM IS JUST AWESOME😍

  • @krishnavar7219
    @krishnavar7219 Před rokem

    the way you explained this challenge...it is genius

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

    Great lecture

  • @sohamsonwane1534
    @sohamsonwane1534 Před 7 měsíci

    best explanation sir

  • @UmaGowda1232
    @UmaGowda1232 Před rokem

    Superb explaination sir 💥

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

    what an explaination wowwwwww

  • @bun_bun17
    @bun_bun17 Před rokem

    you guys make the best effort, you are great

  • @pranalinaik2447
    @pranalinaik2447 Před 9 měsíci

    Thank you so much😇

  • @lerinkuriakose
    @lerinkuriakose Před rokem

    Super🤩

  • @BilalKhanBaraich
    @BilalKhanBaraich Před 3 měsíci

    Given a list of sorted integers and a ‘key’, find and return the index of the first integer that is not less than the ‘key’. If no such integer exists, return -1. The complexity shall be O(logn). (Marks 10)
    Example 1: {1, 2, 4, 5, 5, 6}, key = 2, returned index 2;
    Example 2: {1, 2, 4, 5, 5, 6}, key = 3, returned index 3;
    Example 3: {1, 2, 4, 5, 5, 6}, key = 5, returned index 4;
    Example 4: {1, 2, 4, 5, 5, 6}, key = 7, returned index -1;
    (Note: In the example, the indexing starts with 1 and not 0.)
    Only PSEUDO-CODE is acceptable.
    Sir kindly Solve this question

  • @srinivasareddymandem9204
    @srinivasareddymandem9204 Před 9 měsíci

    anybody clear me what if encounter if the first occurence at first index how should we approach we cant compare mid-1 because 0 is the last

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

    class Solution {
    public int[] searchRange(int[] nums, int target) {
    int[] res={-1,-1};
    int beg=0,end=nums.length-1;
    int mid=0;
    while(begtarget)
    end=mid-1;
    else
    beg=mid+1;
    }
    beg=0;
    end=nums.length-1;
    mid=0;
    while(begtarget)
    end=mid-1;
    else
    beg=mid+1;
    }
    return res;
    }
    }

  • @avinashnoubade7272
    @avinashnoubade7272 Před 8 měsíci

    String problem please

  • @krishnavar7219
    @krishnavar7219 Před rokem

    sir,paste code in chat or give patreon link

  • @aryanverma1233
    @aryanverma1233 Před rokem

    THIS CODE IS NOT WORKING