Find Kth Largest/Smallest Element in an Array | PriorityQueue in Java & C++ | DSA-One Course #33

Поділитися
Вставка
  • Опубліковано 26 вер 2024
  • Hey guys, In this video, we're going to learn about PriorityQueues in Java and in C++. We'll also solve one problem called : Find kth largest element in an array.
    PriorityQueue implementation: www.geeksforge...
    Code for Kth largest element: www.geeksforge...
    🥳 Join our Telegram Community:
    Telegram channel: telegram.me/re...
    Telegram group: telegram.me/ds...
    🚀 Follow me on:
    Instagram: / anuj.kumar.sharma
    Linkedin: / sharma-kumar-anuj
    Twitter: / realanujbhaiya
    💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
    📚 Complete DSA Playlist: • DSA-One Course - The C...
    Complete Android Development Playlist: • Android Development Tu...
    Hashtags:
    #anujbhaiya #dsaone
    Ignore these tags:
    priority queue in java
    priorityqueue in java
    priorityqueue
    java priorityqueue
    what is priority queue in java
    java priorityqueue in tamil
    priorityqueue in java in tamil
    priorityqueue class in java
    priority queue in data structure
    internal working of priorityqueue in java
    java priority queue in tamil
    priority queue in java in tamil
    priorityqueue java
    priority queue in collection in java
    java priority queue example in tamil
    priority queue in data structure java
    kth largest element in an array
    kth smallest element in an array
    kth largest element in an array leetcode
    find kth largest element in an array
    kth smallest element
    kth largest element
    find kth largest elements in array
    kth smallest element in an array in c
    kth smallest element in an array in o(n)
    kth smallest element in an array gfg
    kth smallest element in an array using heap
    kth smallest element in an array geeksforgeeks

КОМЕНТАРІ • 172

  • @sudhakarmataker5718
    @sudhakarmataker5718 2 роки тому +39

    In JAVA code
    its,
    pq.poll() ;
    insted of
    pq.pop();

  • @namansharma5128
    @namansharma5128 2 роки тому +36

    10:04 In C++, to use min heap in priority queue, we use different syntax. If we define a priority queue as -->
    priority_queue< datatype, vector, greater > pq --> this will create a min heap priority queue.
    In case the datatype is int, it would look like -->
    priority_queue< int, vector, greater >pq;
    👇

    • @AddiNarayan
      @AddiNarayan 2 роки тому +1

      That's so nice of you , I was finding this only..

    • @kumaramresh7905
      @kumaramresh7905 2 роки тому

      we can multiply those numbers by -1 to use max heap as min heap

  • @swanandx
    @swanandx 3 роки тому +23

    Priority queue is a abstract datatype. And heap is a data structure that makes the implementation of this data type very nice.

  • @anshrusia1347
    @anshrusia1347 2 місяці тому +1

    That conditional pop and push in the second loop was the game changer in the terms of time complexity

  • @hammad8965
    @hammad8965 3 роки тому +4

    On fire 😅
    First
    Keep it up, daily videos upload karo bhaiyya

  • @gshan994
    @gshan994 3 роки тому +1

    Bhaiya...to the point aap padha rhe... bahut hi aachese samaj aa rha :)

  • @rohankarmakar1761
    @rohankarmakar1761 11 місяців тому +3

    01:40 Priority Queue is a data structure where elements come and go on a priority basis.
    03:20 Priority queue is a data structure used to find the Kth largest/smallest element
    05:00 Implementing Max Heap and Min Heap in Priority Queue
    06:40 PriorityQueue in Java & C++ implements min heap and max heap by default
    08:20 The PriorityQueue data structure in Java can be used to find the Kth largest element in an array.
    10:00 Find the third largest element using a min-heap in Priority Queue
    11:40 Implementing a min heap priority queue to find the kth largest element in an array
    13:15 To find the kth largest element in an array, implement a min-heap and replace the top element with the current element.

  • @ArjunSingh-is1bg
    @ArjunSingh-is1bg 3 роки тому +1

    bhaiya daily videos laao bhut help hoti aap ki videos se.
    Hats off to you Bro

  • @agyaani8060
    @agyaani8060 3 роки тому +3

    Thanks a lot bhaiya❤..U r our lifeline✨❤❤

  • @genzlearners3183
    @genzlearners3183 3 роки тому +9

    I was stucked on the same question , thanks bhaiya 👍☺️

    • @Pritamdaspk
      @Pritamdaspk 3 роки тому

      #include
      #include
      using namespace std;
      int main()
      {
      int n=5;
      int arr[n]={3,7,8,6,5};
      int k=3;
      sort(arr,arr+n);
      cout

    • @ranajitbasak4377
      @ranajitbasak4377 3 роки тому +2

      @@Pritamdaspk It won't work if the array has duplicates

  • @arslanmuhammad4190
    @arslanmuhammad4190 Рік тому

    Love You Anuj Bhai I from Pakistan. What a beautiful way of teaching.

  • @preetitiwari1307
    @preetitiwari1307 3 роки тому +6

    It really helped me to understand the concept, I was not able to solve this question from very long time..

  • @rubinakhatoon2496
    @rubinakhatoon2496 2 роки тому +2

    Awesome explanation
    Thankyou so much
    It helps me to proceed further
    Carry on like this and help us ❤️

  • @pratikmhatre4815
    @pratikmhatre4815 Рік тому +2

    The time complexity for this solution is O (N Log K) and space will O(K).
    We have to visit every element of given array hence the O(N) and for inserting into a PriorityQueue it takes O(N) but here the max element in PQ at any moment is K the final time becomes O(N Log K).
    We are using a PriorityQueue whose size cannot exceed K. Thus the space complexity becomes O(K);

    • @kapilsingh2816
      @kapilsingh2816 Рік тому

      for inserting into a PriorityQueue it takes O(logN)

    • @tarunbehera934
      @tarunbehera934 Рік тому +1

      @@kapilsingh2816 extract krne mein O (log n ) and ye 'k' times lag rha hai toh O (k log(n)) hojayega ig..

  • @amoldeshpande3877
    @amoldeshpande3877 2 роки тому +3

    very good explanation, pls provide time and space complexity after every video.

  • @ankitapushp7407
    @ankitapushp7407 3 роки тому +7

    Thank you so much Bhaiya..😇

    • @pran7119
      @pran7119 Рік тому

      ​@Harsh Rajput 😂😂😂😂

  • @anshumaan1024
    @anshumaan1024 Рік тому +1

    bhai kya smjaya mzza a gya 🤗

  • @ankittjindal
    @ankittjindal 3 роки тому +2

    Bs ase hii video ki frequency chye❤️

  • @AmeerHamza-mt5yi
    @AmeerHamza-mt5yi 3 роки тому

    Hey, Anuj Please make a video on any one of them it's your fan request/viewer 1. How to become a Full-stack Software Developer,
    2. How to become RPA Developer
    3. RoadMap to career path In Devops Engineering detail. salary, scope, demand,

  • @lokendrasingh9780
    @lokendrasingh9780 2 роки тому +1

    thank you bhaiya ❤️ for this amazing lacture 🔥🙏

  • @sayyedaaman5719
    @sayyedaaman5719 2 роки тому

    best video for priorityQueue thank you sir

  • @saptarshichattopadhyay8234
    @saptarshichattopadhyay8234 13 днів тому

    Thank you so much Sir..😘😘😘😘😘😘😘😘😘😘🥰

  • @TheMohit987
    @TheMohit987 2 роки тому +1

    Very well explained.

  • @kumar10100
    @kumar10100 3 роки тому +1

    Anuj Bhaiya Ji Namaskar from sangam city.
    Sir mere ko programming Nahi aati.
    😊😊

  • @i_mchick5311
    @i_mchick5311 3 роки тому +2

    bhaiya "Merge k Sorted Arrays" question kra do pl

  • @ZK-nc6wb
    @ZK-nc6wb 2 роки тому +1

    coding me pure youtube par ek apk samjhane ka tareeka hi mast hai

  • @divyareddy7622
    @divyareddy7622 2 роки тому +1

    keep going!!!!!! love the video

  • @aryanmane4946
    @aryanmane4946 2 роки тому

    We Also make maxHeap . Make Loop of K-1 and pop element . after loop return top() element

  • @tekno864
    @tekno864 2 роки тому

    Bhaiya is Algorithm ki time complexity O(n) hogi kya?
    Or agar is question ko Arrays.sort() method se karenge to time complexity O(nlogn) hogi?

  • @parikshitsinghrathore6130
    @parikshitsinghrathore6130 2 роки тому +1

    what is the time and space complexity of this solution?

  • @ABCDEFGH-fz5ju
    @ABCDEFGH-fz5ju 11 місяців тому

    Explanation 500x better than college professors

  • @himanshujakhmola5960
    @himanshujakhmola5960 2 роки тому

    anuj but largest ke liye gfg me or another platforms pr max heap apply hai and smallest element ke liye minheap , just opposite your method.

  • @mishrahimanshu2000
    @mishrahimanshu2000 3 роки тому

    Bhaiya please 1 video bana dijiye development ke liye Android Development seekhe ya Phir Spring Boot seekh k backend/Web Development kre...
    Please!!!

  • @kaushalprajapat252
    @kaushalprajapat252 8 місяців тому

    If I'm not wrong @5:15 in java minheap's structure 15-shoud be in left of 5, It inserted before 10...
    If wrong someone correct me..
    Thx in advance

  • @KD-xi9wu
    @KD-xi9wu 3 роки тому +1

    Your videos are amazing please try to make videos in English.

  • @siddharthpunmiya2744
    @siddharthpunmiya2744 3 роки тому

    Bhaiya mere method time complexity jyada hosaktha hai but yeh approach bhi sahi hoga na java mai
    Hum sab element priority queue mai daal dege and comparator pass kardege reverse order ka and we will pop that many many till as the value of k and jaise he reach karege waise woh print kardege

  • @satyamsinghchauhan4123
    @satyamsinghchauhan4123 Рік тому

    bro aap phle code me pop () use krr rhe hain aur kth largest me poll () dono work krta h kya max/min return krne k liye

  • @random2402
    @random2402 2 роки тому

    Thank you so much bhaiya for this awesome video.

  • @swarnsingh-gc8cd
    @swarnsingh-gc8cd 2 роки тому +1

    PriorityQueue doesn't have pop() method. So it should be pq.poll() in code.

  • @haramicoder8483
    @haramicoder8483 2 роки тому

    good tutorial helped me a lot ....Thanks Sir Ji !!

  • @SE3_65_Bhupendra_Yadav
    @SE3_65_Bhupendra_Yadav 3 місяці тому

    Nice video bhaiya

  • @kavya7757
    @kavya7757 2 роки тому

    bht achha samjhayein

  • @user-hq8pt4lv7w
    @user-hq8pt4lv7w 3 роки тому +2

    Sir, how to start preparation for software companies ....currently in 1st year bsc computer science
    Is there any hope for getting a good job in software companies just after passing bsc comp sc as a fresher?

  • @pranavraj5698
    @pranavraj5698 3 роки тому +1

    Thanks

  • @saketgautam4417
    @saketgautam4417 3 роки тому +2

    I request everyone who's following the playlist, please like the videos as this motivate the creator a lot. Bhaiya is doing such an amazing job by providing us premium content for free. Support him and also share the playlist with needy people.

  • @Developer-404
    @Developer-404 2 роки тому +1

    can we heapify that array and extract k element from that array?

  • @Shadow28888
    @Shadow28888 3 роки тому +1

    Please graph and dp ka problems bhi upload karna because UA-cam me accha contents nhi mile🙏

  • @easysolutions135
    @easysolutions135 3 роки тому

    Anuj Bhaiya love you broo.

  • @SRV.77
    @SRV.77 Рік тому

    Awesome bhaiya

  • @Pradnya_Deore
    @Pradnya_Deore 3 роки тому

    Sir I want to become an Software developement Engineer so what skills required for it..I mean what strategy I have used,like subjects and all...

  • @mohitsinghchauhan6856
    @mohitsinghchauhan6856 3 роки тому +8

    Bhaiya please make a video on Freelancing also..... Like Web development.... We Youngsters want to generate some initial income after High school .... So, please make a detailed video on that....❤️❤️

  • @muditkhanna8164
    @muditkhanna8164 Рік тому

    please also explain this with quickselect algo

  • @dudamneerajdattu1459
    @dudamneerajdattu1459 Рік тому

    time cmplexity of pq.push() ?

  • @kashyap6369
    @kashyap6369 3 роки тому

    Great video bhaiya

  • @MS-eg9zk
    @MS-eg9zk 3 роки тому +2

    Anuj bhai like kriye

  • @nilshr2
    @nilshr2 3 роки тому +1

    Bhiya dheere dheere karke linked List bhi upload karna

  • @The-fc1fi
    @The-fc1fi 3 роки тому +1

    In c language protity queue have or not

  • @anjaliradcliffe2891
    @anjaliradcliffe2891 Рік тому

    dhanyawad

  • @udaybhaskar3697
    @udaybhaskar3697 2 роки тому

    Underrated video

  • @niteshshankar9211
    @niteshshankar9211 3 роки тому

    Can you make a live video of shuffler app please. I am stuck. In android development tutorial playlist.

  • @K_EC_AnuragKumarMishra
    @K_EC_AnuragKumarMishra 2 роки тому

    great explanation

  • @dreamyme543
    @dreamyme543 2 роки тому

    Wow superb

  • @warriorgaming9509
    @warriorgaming9509 2 роки тому

    Nice Expalined

  • @VVS15
    @VVS15 2 роки тому +2

    Agar elements ko sort kar liya hai to heap kyo banaya........Kth largest chahiye to (K-1)th element lelena

  • @satakshipal5008
    @satakshipal5008 3 роки тому

    nice explanation bhaiya

  • @roshangeorge97
    @roshangeorge97 Рік тому

    but priority queue is min heap by default right?

  • @siddhantyadav4531
    @siddhantyadav4531 3 роки тому

    Thanks a lot 👍👍👍 Sir

  • @abromioitis
    @abromioitis 3 роки тому

    Bhaiya please upload video on regular basis ......... please

  • @sourin.majumdar
    @sourin.majumdar 2 роки тому

    Bhaiya time and space complexity kya hogi iski?

  • @harry-cf4ii
    @harry-cf4ii 3 роки тому +2

    Bhaiya according to ur estimation in how many videos will DSA and algorithm course finish??and will u start a C++ series from scratch to advanced level for placement specific after which we will be able to crack all the questions in C++ language, after the DSA series🥺🥺🥺❤️❤️because i only get the feel of coding from ur channel. Hope u will do it too and help me achieve my dream package from a Tier -3 college😔🥺❤️🤗🔥

  • @ankitdubey6813
    @ankitdubey6813 3 роки тому +3

    💯👍

  • @meetsoni1938
    @meetsoni1938 2 роки тому

    Maza aa gaya 🔥🔥

  • @sujeet3035
    @sujeet3035 2 роки тому +1

    Does it help in case of duplicate elements?

  • @somveerkhaunkar6191
    @somveerkhaunkar6191 2 роки тому

    thanks bhaiya

  • @nikhilgupta8941
    @nikhilgupta8941 2 роки тому

    Why priority queue method is greater then sorting algo nlogn

  • @ashutoshsingh-uh4io
    @ashutoshsingh-uh4io Рік тому

    bhaiya IDE me kiya kariye na doubts reh jaate hai bhut :(

  • @niteshshankar9211
    @niteshshankar9211 3 роки тому

    Can you male a live video of shuffler app please. I am stuck.

  • @laxmikantmailare5062
    @laxmikantmailare5062 Рік тому

    What is TC here? is it O(n)+O(klogk)?

  • @sakibhossain6136
    @sakibhossain6136 2 роки тому

    I did as you said but It doesn't work. As I know It should work but I got TLE using priorityQueue

  • @shehtabmasud9612
    @shehtabmasud9612 2 роки тому

    11:03 if you are coding in Java should be pq.poll()

  • @rupamdey4467
    @rupamdey4467 Рік тому

    what is the time complexity of this approach?

  • @akashyadav3211
    @akashyadav3211 2 роки тому

    isn't it O(nlogn)🤔....and the question says it should be done in O(n) TC.

  • @Rohitsingh2410
    @Rohitsingh2410 3 роки тому +3

    Slight confusion i have, if we need to solve this using a priority queue to get k=3rd largest number, then
    Why can't we just insert all elements in a max heap , and iterate over it till 3rd element is there and return it? in case of duplicates also , we can compare it with side elements, or insert it in a set. can someone explain?

    • @sohamroy9272
      @sohamroy9272 2 роки тому +3

      But in your case, the space complexity will be O(n). Here the space complexity is O(k) which is most optimized.

    • @Rohitsingh2410
      @Rohitsingh2410 2 роки тому +1

      @@sohamroy9272 thanks

    • @prtdesh25
      @prtdesh25 2 роки тому

      @@sohamroy9272 anyway we are iterating through whole array, so its O(n) only

    • @sohamroy9272
      @sohamroy9272 2 роки тому +1

      @@prtdesh25 I am talking about the SPACE complexity which is O(k) and time complexity is O(n) because we are iterating the whole array.

  • @RahulDutta-v3c
    @RahulDutta-v3c 4 місяці тому

    bhai pehle cpp wale code mein hi error aagya... kya likha hai

  • @alokranjan3185
    @alokranjan3185 2 роки тому

    to0
    easy sir thanks

  • @ashishbiswakarma5357
    @ashishbiswakarma5357 2 роки тому

    pq.poll(); to remove top element in PQ

  • @Engineer_With_A_Life
    @Engineer_With_A_Life 2 роки тому

    can't we directly return 3rd last element of the sorted array??

    • @harshitpant07
      @harshitpant07 Рік тому

      during sorting it will take nlogn while here it will be n+klogn

  • @arpitjindal3085
    @arpitjindal3085 3 роки тому +1

    🔥🔥

  • @rryann088
    @rryann088 Рік тому

    clearly explained

  • @kartikaysharma4015
    @kartikaysharma4015 5 місяців тому

    Question at 7:37

  • @maharun
    @maharun 2 роки тому

    7:35 Question - Print K-th Largest element in an array

  • @hannanhub1717
    @hannanhub1717 Рік тому

    why smallest element from last three element should be our Kth largest element and not from first three elements?

  • @aarishfaiz7880
    @aarishfaiz7880 2 роки тому

    pop k jagha poll se bhi kaam chal jaa rha hai

  • @anshulsingh1265
    @anshulsingh1265 3 роки тому +2

    Imagine getting a Heart From Anuj bhaiya

  • @mondal9509
    @mondal9509 3 роки тому +6

    Bhaiya aapne ab tak kitne internship kiye hai?

  • @rohan8758
    @rohan8758 5 місяців тому

    Bhaiyan Intuition toh bana hi nhi, 300 questions mein iska code kaise yaad rakho ?😢😢

  • @Paresh_pixelography
    @Paresh_pixelography 3 роки тому

    placement season chalu hoga gya hai sir, please jaldi sab videos daaldo

  • @nayanasagar6062
    @nayanasagar6062 3 роки тому +3

    1st like ❤️

  • @sssumeet
    @sssumeet 2 роки тому

    🙌🙌🙌

  • @zoxstar1921
    @zoxstar1921 2 роки тому

    what if there are duplicates in an array