Merge k Sorted Arrays 🛑

Sdílet
Vložit
  • čas přidán 17. 05. 2022
  • This is the video under the series of DATA STRUCTURE & ALGORITHM in a BINARY HEAP Playlist. Now we are going to code Heap in Data Structure or Implement. in this video we Solve a Programming Question from the geeksforgeeks Heap Tag Question on the Merge k Sorted Arrays a Medium Tag Question From heap Data structure. Let's learn Heaps series by Hello world by Prince.
    Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
    complete DSA preparation: github.com/Prince-1501/Comple...
    ----------------------------------------------------------------------------------------
    Merge k Sorted Arrays: practice.geeksforgeeks.org/pr...
    ----------------------------------------------------------------------------------------
    *Follow me *
    LinkedIn► / iamprince
    Facebook► / helloworldofficials
    Instagram► / helloworldbyprince
    Twitter► / prince_king_
    Telegram► telegram.me/helloworldbyprince
    ----------------------------------------------------------------------------------------
    ►Our Playlists on:-
    ► Tree: • Tree Data Structure & ...
    ► Stack & Queue: • Stack & Queue Data Str...
    ► Hashing: • Hashing Data Structure...
    ► Graph: • Graph Data Structure &...
    ► Matrix: • Matrix (Multidimension...
    ► Heap: • Heap Data Structure & ...
    ► STL: • Standard Template Libr...
    ► Leetcode: • LeetCode Solutions And...
    ►Competitive Programming: • Full course in Competi...
    ►C++ Full Course : • C++ full Course in HINDI
    ►Algorithms: • L-01 || Prefix Sum Arr...
    ►Data Structure: • Data Structures with C...
    ------------------------------------------------------------------------
    🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
    ✨ Tags ✨
    merge k sorted arrays
    merge k sorted arrays leetcode
    merge k sorted arrays using a priority queue
    merge k sorted arrays gfg practice
    merge k sorted arrays gfg
    merge k sorted arrays python
    merge k sorted arrays using priority queue java
    merge k sorted arrays coding ninjas
    merge k sorted arrays using heap
    merge k sorted arrays interviewbit
    merge k sorted arrays divide and conquer
    merge k sorted arrays interviewbit solution
    merge k sorted arrays techie delight
    merge k sorted arrays priorityqueue java
    Heap sort playlist Hello world
    Heap sort practice problems
    heap practice problems gfg
    leetcode heap questions
    leetcode heap
    heap hello world
    Types of the heap in Data structure & algorithms
    playlist heap Hindi
    question asked in Google
    off-campus placement
    Practice heap data structure
    heap in a data structure in Hindi
    heap Full playlist for Beginners
    algorithms
    heap
    data structure
    gate computer science preparation
    programming languages
    Comment "#Princebhai" if you read this 😉😉
    #Heap #merge_k_sorted_arrays #programming

Komentáře • 148

  • @adrijsharma8404
    @adrijsharma8404 Před rokem +1

    good video! worth watching. Conceptual clarity.

  • @MohdShahrukh-ne8nc
    @MohdShahrukh-ne8nc Před rokem

    awesome explanation, able to code in java after your explanation ,no need to see your code, Thanks man

  • @nishagawade3545
    @nishagawade3545 Před 2 lety +9

    Your channel is really amazing, and your explanation too. Thanks for helping me to improve logic

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc Před rokem

    love the you explaind with ease and with detail

  • @circketblendwithmusic

    bhaiya bhot acha video hai bhot hardwork kar rahe ho app Thank you so much bhaiya.

  • @ranimandage1484
    @ranimandage1484 Před 12 dny

    One of the best vedio

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

    bhaiya puraa samaj aagya after watching this video 2-3 times but still samaj aagya thank you❤❤

  • @praveennarvaneni2970
    @praveennarvaneni2970 Před rokem

    That's a clean explanation

  • @hcsrihari
    @hcsrihari Před rokem +2

    this video deserves more likes and views . great explanation .

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

    Appreciate the efforts man. Best explaination available for this question on youtube in my opinion 💪

  • @viveksoni1923
    @viveksoni1923 Před rokem

    Bhaiya keep it up very nice explanation

  • @mdtaslimreza1305
    @mdtaslimreza1305 Před rokem

    its a great explanation brother.sach m maza a gaya

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

    made this qs so clear and easy Thanks!!

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem +2

      You're welcome! Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @Surya-zu3oj
    @Surya-zu3oj Před rokem

    this video deserves more likes and views . very well explained

  • @amanpandey4550
    @amanpandey4550 Před rokem +1

    Understood very well.

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

    Bhaiya ur style of teaching is so amazing , u make things really simple

  • @jatilyadav4000
    @jatilyadav4000 Před rokem

    Best Explaination prince Sir.....

  • @jatilyadav4000
    @jatilyadav4000 Před rokem

    Thank You Sir............

  • @SatyamKumar-bw4vi
    @SatyamKumar-bw4vi Před 2 lety +3

    Hare Krishna..! Great Video

  • @lakeshkumar1252
    @lakeshkumar1252 Před 11 měsíci

    thank you sir for very nice explanation

  • @MaayankMavericks888
    @MaayankMavericks888 Před rokem +1

    Great Explanation.

  • @shashwatdhingra3580
    @shashwatdhingra3580 Před rokem +5

    Bhai ye maine khud kiya hai pairs ka use karke.
    Sirf aapki vajah se. Thanks. 🙏
    typedef pair pi;
    class Solution
    {
    public:
    //Function to merge k sorted arrays.
    vector mergeKArrays(vector arr, int K)
    {
    vector ans;
    priority_queue< pi, vector, greater >pq;
    //Step 1
    // Pushing all first element of all SubArrays in Min Heap.
    int size = arr.size();
    for(int i=0; i

  • @cs_111_amansingh6
    @cs_111_amansingh6 Před rokem

    Teacher nhi ye koi jadu jo itna axa samjhaye aay hay

  • @ganakkathuria4893
    @ganakkathuria4893 Před rokem +2

    Outstanding explanation , it's far more better then paid courses , Now , I myself see your videos instead of the courses I bought....I have a small doubt instead of using struct mycomp i'm using similar
    static bool mycomp function but it's not working can you explain why

  • @Satyamk001
    @Satyamk001 Před rokem +1

    great explanation sir.

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

    The sweat is the proof of your hard work

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

    Very well explained sir.

  • @tanaykamath1415
    @tanaykamath1415 Před 2 lety

    Great video, this deserves more likes!

  • @deepakgoyal1643
    @deepakgoyal1643 Před rokem +1

    what will be the time complexity of this soltuion please tell sir

  • @kushagraahire1871
    @kushagraahire1871 Před rokem

    Nice concept

  • @kritsimarahuja8978
    @kritsimarahuja8978 Před rokem

    thank you so much sir ♥

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      Keep learning buddy and keep growing
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @samarthaggarwal8297
    @samarthaggarwal8297 Před rokem

    Thanks!!

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

    This video really helps. One request please, plesae, please make a video on stl comparator function, how it works.

  • @kingoftheworld8226
    @kingoftheworld8226 Před rokem

    bhiya shi me smjh aaya gfg k course se nhi ayaa tha jabki

  • @ankit_yadav11
    @ankit_yadav11 Před 11 měsíci

    bhaiya hume aaj first aisa solution hai linkedlist bst tree stacks queue sara padh liya aisa comperator use karke jo isme bna rhe hai ye samjh hi nhi aaya bilkul demotivate ho gya tha then i get ur videos

  • @gamingworld2952
    @gamingworld2952 Před 2 lety

    really great, working tooo hard

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      Thank you! Cheers! Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

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

    Hi it was really a great approach that you have used. I just wanted to know if this is some common approach which is used to solve this question or this is the approach that you have come up with?

  • @shivanshkaldaik4165
    @shivanshkaldaik4165 Před rokem +1

    WHAT WILL BE THE TIME COMPLEXITY FOR THIS SIR COULD YOU PLZ EXPLAIN??

  • @saurabhtiwari9653
    @saurabhtiwari9653 Před rokem

    Prince Bhai... ❤️

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

    Thank youu for these, if there has to merge linked lists instead of arrays, can we use the same approach?

  • @anonymous090
    @anonymous090 Před 2 lety

    Thank you Bhaiya

  • @mr.bjajsinghaniya2872

    best video

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

    We can also use greater right instead of bool comparator to make it min heap?

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

      Lekin greater < int > me data type to int hai na
      Humlog custom data type ke liye wo kaam nahi karega we have to make it out Own

    • @shivamehta6537
      @shivamehta6537 Před 2 lety

      @@HelloWorldbyprince ok

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

    can you tell time complexity and space complexity bcs for interview perspective it is very important to know tc and sc so from now onwards include tc and sc in your video

  • @raghavsingla765
    @raghavsingla765 Před rokem

    data d means we are calling constructor ?? and if we are calling constructor using d then how can we put three values in one heap

  • @tanveer.shaikh
    @tanveer.shaikh Před 2 lety +1

    no one was explaining how we are inserting the next element but suddenly it kicks and understood , example of finding first k fastest horses

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

    can we do pair instead of data

  • @sejalrai30
    @sejalrai30 Před rokem

    thanks

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      Welcome Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @vivekswami2182
    @vivekswami2182 Před rokem

    🔥🔥🔥🔥

  • @deveshkumar2862
    @deveshkumar2862 Před rokem +1

    What is the TIme complexity and space complexity

  • @satvrii
    @satvrii Před rokem

    ❤❤❤❤❤❤

  • @krishnarajput2371
    @krishnarajput2371 Před 2 lety

    Good explanation

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx Před 6 měsíci

    Understood

  • @piyushjain5407
    @piyushjain5407 Před rokem +1

    What is the need if comparator? and which values does d1 and d2 denote

  • @beingSenpai
    @beingSenpai Před rokem

    so the TC is KlogK ?

  • @AmitKumar-eq5cw
    @AmitKumar-eq5cw Před rokem

    Nice

  • @063harshsahu2
    @063harshsahu2 Před rokem

    #princebhai

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

    done by myself...thank you.masum.
    multisets;
    for(int i=0;i

  • @RameshPatel-yv3jc
    @RameshPatel-yv3jc Před 2 lety

    I have only one doubt, how do you make comparison structure. please bhaiya make a separate video on this.

  • @176_shubhadeepsen2
    @176_shubhadeepsen2 Před 2 lety

    god lvl explanation

  • @ss-md4qn
    @ss-md4qn Před 2 lety

    Superb

  • @hiteshyadav9399
    @hiteshyadav9399 Před rokem +1

    Helpful video❤ but what is time and space complexity?

  • @adhed_engineer
    @adhed_engineer Před rokem

    maja aa gaya

  • @harshjain7945
    @harshjain7945 Před 2 lety

    Understanding++

  • @ankit_yadav11
    @ankit_yadav11 Před 11 měsíci

    yar main point hai comperator kaise use kar rhe h wo samjh hi nhi aaya wahi phash ja rha hun mai baki sara to use karte hi aa rh hun but comperator new h use samjhao aap achge se

  • @ultron1060
    @ultron1060 Před rokem

    Correct me if I'm its space complexity is O(N) - We are filling our custom data structure and putting it in pq , Maximum elements in data and in pq will always be N

  • @abhishekverma7604
    @abhishekverma7604 Před rokem +2

    class Solution
    {
    public:
    //Function to merge k sorted arrays
    void heapify(vector&arr,int n,int i)
    {
    int largest = i;
    int left = 2*i + 1;
    int right = 2 *i + 2;
    if(left < n and arr[left] > arr[largest])
    {
    largest = left;
    }
    if(right < n and arr[right] > arr[largest])
    {
    largest = right;
    }
    if(largest != i)
    {
    swap(arr[i],arr[largest]);
    heapify(arr,n,largest);
    }
    }




    void build_heap(vector&arr,int n)
    {
    // for internal nodes
    // build heap from an array
    for(int i = (n/2-1);i>=0;i--)
    {
    heapify(arr,n,i);
    }
    }


    void heap_sort(vector&arr,int n)
    {
    build_heap(arr,n);
    for(int i = n-1;i>=0;i--)
    {
    swap(arr[0],arr[i]);
    heapify(arr,i,0);
    }
    }

    vector mergeKArrays(vector arr, int K)
    {
    //code here
    vectorans;
    for(int i = 0;i

  • @sakshisoni4194
    @sakshisoni4194 Před rokem

    What is time complexity of efficient approach

  • @veerendrashukla
    @veerendrashukla Před rokem +1

    Hello Prince
    why do we take next item from the same array? what is intuition behind that?

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      ek baar aap please dry run karke dekho na code ko jo maine bataya hai i promise aapko samjh me aa jayega agar fir v nahi aayega to i here to help u

    • @DheerajDivaker
      @DheerajDivaker Před rokem

      straight forward intuition: - array sorted hone ki Wajah se, jis array se minimum aayega, ussi array se next time bhi minimum aane ke jyada chances honge.

  • @Kiran_Singh203
    @Kiran_Singh203 Před rokem

    Jai shree krishna
    Bhaiya mycompt struct internally work kaise kr raha h please bta do

  • @Raj10185
    @Raj10185 Před rokem

    Logic is crytal clear and explnation is crystal clear .
    i write my own code for this much simpler then your code bhaiya no use of comperrator no need to define a seperate class
    typedef typedef pair pii;
    vector mergeKArrays(vector arr, int k)
    {

    vector ans;
    priority_queue pq;

    for(int i =0 ; i0)
    {
    ans.push_back(pq.top().first);
    int row = pq.top().second.first;
    int col = pq.top().second.second;

    if(col+1

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

      This code doesn't make any sense bro here the TC is O(N * k * log (N * k)). which can be achieve even after using 2 for Loops and storing all elements in Min heap!!

  • @HimanshuGupta-ni3pk
    @HimanshuGupta-ni3pk Před rokem

    This question should be in hard level

  • @khushdesai7600
    @khushdesai7600 Před rokem

    bhai is mai operator overload kiya wo samj nahi aaya

  • @anshumaan1024
    @anshumaan1024 Před rokem +2

    ✅ TC -> O(NKlogK)
    ✅ SC -> O(NK), for filling our user defined data type,
    where , K denotes number of input arrays
    N denotes number of maximum elements in an array
    🙂🙂

    • @noob_coder8926
      @noob_coder8926 Před 11 měsíci

      please enlighten me if i am wrong
      in worst case the time and space complexity would be O(k^2*logk) and O(k^2)

  • @shivanigupta9747
    @shivanigupta9747 Před rokem +1

    I don't understand the concept of compare

  • @nikhilbrahmapurikar3803

    time complexity?

  • @dineshbhainayee5470
    @dineshbhainayee5470 Před 2 lety

    Sir ap = curr.apos & vp = curr.vpos kese find hogaa

  • @KrishnaSharma-bv5ys
    @KrishnaSharma-bv5ys Před rokem

    Sab sahi tha bas comparator smj nhi aya ...

  • @codinglife7516
    @codinglife7516 Před rokem +2

    instead of using concept of data we can use pair

  • @ujjawaldewangan8345
    @ujjawaldewangan8345 Před rokem

    mycomp smjh nhi aaya

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

    What is the time complexity of this min heap approach ?

  • @rishabhkumar9406
    @rishabhkumar9406 Před rokem

    HOT STUFF 🥵🥵

  • @shubhampawar456
    @shubhampawar456 Před 2 lety

    My solution::::
    class Pair{
    int key;
    int i,j;
    Pair(int key,int i,int j){
    this.key=key;
    this.i=i;
    this.j=j;
    }
    }
    class Solution
    {
    //Function to merge k sorted arrays.
    public static ArrayList mergeKArrays(int[][] arr,int K)
    {
    // Write your code here.
    ArrayList l=new ArrayList();
    PriorityQueue q=new PriorityQueue(new Comparator(){
    public int compare(Pair p1,Pair p2){
    return p1.key-p2.key;
    }
    });
    for(int i=0;i

  • @pritishpattnaik4674
    @pritishpattnaik4674 Před 2 lety

    last example dry run done : 4 5 6 9 10