4 Sort a K Sorted Array | Sort Nearly Sorted Array

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ม.ค. 2025

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

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

    0:00 *Introduction*
    1:00 *Problem Statement*
    3:00 *Sorting Idea*
    4:00 *Heap approach & Explanation*
    10:00 *Code Implementation*
    *Nicely Explained : )*

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

    Bruh , No one even made me understand the problem this good let alone make me undersatand solution, you are gifted.

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

    Crisp and Precise, Man U just nailed it though ! 🤛🏻

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

    Your explanations are awsome. I got stuck in this question "Smallest range in K lists" can you please make a video on this.

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

    Bhaiya jis tarah ap samjhate ho , 1 number

  • @tejaslokhande3725
    @tejaslokhande3725 5 หลายเดือนก่อน +9

    This was asked to me in PhonePe interview (Cali, US ) position

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

    Really helpfull.Finally understand the use of k sorted array to sort using heap,Thank you once again.

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

    // Nearly sorted GFG
    vector nearlySorted(int arr[], int n, int k){
    priority_queueminh;
    vectorans;
    for(int i=0;ik)
    {
    ans.push_back(minh.top());
    minh.pop();
    }
    }
    while(minh.size()>0)
    {
    ans.push_back(minh.top());
    minh.pop();
    }
    return ans;

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

    No body could have explained ot better 😌🤘💯🔥

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

    It’s May ,waiting for backtracking, greedy, graphs, trees patiently. 😊😊

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

      yes..patiently

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

      @@nagarjuna119 yes i'm also waiting for backtracking and graph.. there is no any good sources available on these topics .

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

      @@markos8955 webinar on backtracking by coding ninjas and graph by coding blocks is available in youtube

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

      @@raviashwin1157 I think due to lockdown he is not able to make any new videos

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

      babbar love babbar ka video dekho jake beta.

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

    The original code :
    ---------------------------------------
    #include
    #include
    #include
    using namespace std;
    void sort_k_sorted_array(int *arr, int n, int k)
    {
    priority_queue minh;
    int j=0;
    for(int i=0; ik)
    {
    arr[j]=minh.top();
    minh.pop();
    j++;
    }
    }
    while(minh.size()>0)
    {
    j++;
    arr[j]=minh.top();
    minh.pop();
    }
    }
    void print_array(int *arr, int n)
    {
    for(int i=0; i

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

      is this always return full sorted array or if we take arr[] = {6,5,3,4,2,8,9} then the output will be {3,2,4,5,6,8,9} is it correct ans?? im confuse lit bit

    • @AD-hb6ip
      @AD-hb6ip 2 ปีที่แล้ว +2

      @@mitashiv3918 the element should be from [i-k,i+k] if u take k=3 element 2's index in matrix should be 3 or less

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

      can you please explain why you used minh.top() to insert element in the array. whereas we can you use minh.top() directly as it returns that element also and even reduces one step.

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

    Sir your videos are awesome, hats off to you .... Plzzz backtracking upload kardo

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

    Code for java
    ```
    class Solution
    {
    //Function to return the sorted array.
    ArrayList nearlySorted(int arr[], int num, int k)
    {
    // your code here
    ArrayList list = new ArrayList();
    PriorityQueue q = new PriorityQueue();
    for(int i = 0 ;ik){
    list.add(q.poll());
    }
    }
    while(!q.isEmpty()){
    list.add(q.poll());
    }
    return list;
    }
    }
    ```
    Thanks for the explaination sir

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

    Ek video pen ghumane ki bhi bana do bhaiya, bohot cool lgta hai 😍😍😂😂

    • @AJ-dd3ln
      @AJ-dd3ln 3 ปีที่แล้ว

      bruh the pen is not rotating just watch it by slowing the video..

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

    Thank You So Much for this wonderful video....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    Awesome explanation bhiya 😄😄 Just keep up the good work!!

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

    Thank you so much bro for this valuable content.so happy to learn

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

    It was very helpful...Thanks bhaiya

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

    Great explanation!

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

    Anyone or Aditya Verma
    If you are saying that min element is always at the top and the remaining elements in heap are not necessarliy sorted then why did you inserted 9 (8:36) at a sorted position rather than at the lowest. you have taken all the examples where heap elements are sorted below top.

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

      Aditya is correct . basically in minHeap , after popping min element re hepify runs which brings only min element to top. Check re hepify once

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

      @@JameS00989 hello bhai kon se college se ho..

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

    Great stuff Aditya!!

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

    Great explaination by u😊😊

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

    Excellent explanation Sir.

  • @bharatmajeti1061
    @bharatmajeti1061 5 หลายเดือนก่อน +1

    But if we have 4 in place of 8. do the algorithm still work? cause then the order after this algorithm would be 2,3,4,5,6 but 6 can't move more than 3 places from it's original position.

  • @AmarjeetKumar-en1gk
    @AmarjeetKumar-en1gk ปีที่แล้ว

    Mai ye padh kr gya tha, Heap ka implementation puchh liya.

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

    great video bhai!!

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

    Is using STL/ STANDARD LIBRARY and PRIORITY QUEUE acceptable in coding rounds ???? The idea of understanding the implementation part of some of the tough algorithms scares the shit out of me !! :( expecting replies from the IT and software professionals .....

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

      Yes STL is allowed

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

    JAVA CODE :
    public static ArrayList nearlySorted(int arr[], int k) {
    ArrayList l = new ArrayList();
    PriorityQueue q = new PriorityQueue();
    for (int i = 0; i < arr.length; i++) {
    q.add(arr[i]);
    if (q.size() > k) {
    l.add(q.remove());
    }
    }
    while (!q.isEmpty()) {
    l.add(q.remove());
    }
    return l;
    }

  • @sasidharnaidu4507
    @sasidharnaidu4507 11 หลายเดือนก่อน

    We could update the given array itself as the k elements were added into the heap at any point of time.

  • @MadhavGupta-fi2tu
    @MadhavGupta-fi2tu ปีที่แล้ว

    Sir plz you should also put link of question in description. Nice exp

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

    Please upload more you are amazing

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

    u are a legend 😍😍😍😍😍😍

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

    Hi, why we use a heap size k+1, i havent get the intuition behind this. Can we use a heap of other size?. Why we are making the heap of size k+1 for they stating upto k+1 elements is sorted. Please clarify!🤒

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

      Let's take example of 0th index :
      The element which has its position at 0th index may be present atmost k distance away from 0th index.
      So we store the 0th element and the next k elements to find the smallest among them, so that we can put it at the 0th index.
      Hope it helped you.

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

    Why are we using min heap here when we can interpret the question as finding smallest element in k terms. Aren't we supposed to use max heap then

    • @akshaykumar-vv2ps
      @akshaykumar-vv2ps 2 ปีที่แล้ว +2

      well actually there we were finding k th smallest element whereas here we are finding smallest element.here we require the top element to be smallest hence min heap,whereas to find kth smallest element we need the smallest element at the bottom,then 2nd smallest and so on.. till kth smallest

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

    Thank you very much.

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

    what should be the size of the heap?if it is k+1 then why?

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 ปีที่แล้ว

    Nice explaination !!

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

    Video Stands Out even in 2024

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

    arr[] = {10, 9, 8, 7, 4, 70, 60, 50}
    k = 4, i think this case would fail for this algorithm.
    Please correct me if I am wrong.

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

      it works

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

      @@rohitjadhav512 Hi Rohit, when we dry run it ,the first element that goes outside the heap would be 7 and then 4 would be inserted and pop up in next iteration.
      So how is this test case passing.
      Please feel free if I am wrong and explain

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

      @@sakshamsrivastava6280 no bro...7 won't pop first...4 will get pop first because IF(heap.size() > k) satisfies when heap.size() become 5 and at that time heap top would be 4 n that 4 will get pop....

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

      Yes it failing for k=3

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

    The code in Java:
    class Solution
    {
    //Function to return the sorted array.
    ArrayList nearlySorted(int arr[], int n, int k)
    {
    // your code here
    PriorityQueue minHeap = new PriorityQueue();

    ArrayList ans = new ArrayList();

    for(int i=0;ik){
    ans.add(minHeap.peek());
    minHeap.poll();
    }
    }
    for(int i=0;i

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

      int j=0 Arr[j++] inspace instead of list

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

    What is functionality of priority queue in JS. Like we have used directly STL for C++. What I can use in JS.

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

    Same problem in GFG is named as "Nearly sorted"

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

    It's June 2023, still waiting for graph playlist 🙃

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

    If k is 2 then the first value will be 3 instead of 2. Kindly cover this case!!!

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

      I think if K is changed, so will the Input array. The input array in this question is designed for k=3.

  • @atulwadhwa192
    @atulwadhwa192 3 หลายเดือนก่อน

    at 3:05 I paused for a while and boom I wrote the whole solution by myself. Thanks brother but thodi cheating hai yeh. I'll improve on this

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

    0:00 intro
    1:00 ps

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

    bhai how did you identify that we should use min heap?

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

      Brother, I have covered that already in the starting videos of the series, please refer them

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

      Here, we need to print array in ascending order so smallest element in k range will come first, that is top of min heap. If we need to print array in descending order, I think we will use max-heap.

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

      @@jagrit07 Agreed! I think one must identify the type of heap based on what is required.. Since we require the ascending order of elements, take the min-heap. It's unlike Kth largest/smallest where type of heap used is opposite to the requirement because we're eliminating the smallest/largest element i.e. it's a different thought process than this question.

    • @ujjawal.pandey
      @ujjawal.pandey 3 ปีที่แล้ว

      @@damercy I think Sai Charan's comment should be pinned and yours (& Jagrit Bhupal too) answer should be hearted!

    • @113_manshi4
      @113_manshi4 ปีที่แล้ว

      @@jagrit07 Wow this was very helpful. Thank you!

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

    hey , bro how do you swirl your pen , during tutorial =????????

  • @NikhilKumar-mn8py
    @NikhilKumar-mn8py 4 ปีที่แล้ว +1

    sir aap kahan tthe aaj tak. Thanks a lot.

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

    Perfect🤩

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

    how i know that minheap will be used in this question,kindly reply......

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

    Thank you 💕

  • @AmanChauhan-hr1wh
    @AmanChauhan-hr1wh ปีที่แล้ว

    what if its is asked to modify in the same array

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

    what if in the place of index 5 (which is 8) is 1?
    what would the min heap do?

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

      i cant not go that far only have k far from it's right position

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

    Thank you 😊

  • @ASHISHYADAV-pj7ek
    @ASHISHYADAV-pj7ek 4 ปีที่แล้ว

    Awesome bro

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

    Here is my Python solution guys who are coding in python may be it will help :
    from heapq import heappop, heappush,heapify
    arr =[6, 5, 3, 2, 8, 10, 9]
    k = 3
    min_heap = []
    sorted_lis = []
    for i in range(len(arr)):
    heappush(min_heap,arr[i])
    if len(min_heap)>k:
    res =heappop(min_heap)
    sorted_lis.append(res)
    while min_heap!=[]:
    sorted_lis.append(heappop(min_heap))
    print(sorted_lis)

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

      Brother how can we implement max heap using heapq in python?

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

    Excellent Explanation!!!!
    totally out of context question
    Bro are you from UP??

    • @ShreyaSingh-vr9qi
      @ShreyaSingh-vr9qi 4 ปีที่แล้ว +1

      Bareily sa hain

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

      MP, most probably. UP people don't say "Hum" as "Uppan".

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

      @@AdityaRaj222 EXACTLY bro

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

    Heap like magic😂😂

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

    for arr[]:{5,6,4,7,2,3}; and k = 2. How we can say that at index 0 the element to be present has to be k distance away?

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

      In the requirement of the question it is mentioned that the provided array will be k-sorted (not fully un-sorted or sorted).

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

      @@SuperNirajpandey okay, I got it, Thanks!

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

    Is there any leetcode problem similar to this ?

  • @heyOrca2711
    @heyOrca2711 12 วันที่ผ่านมา

    thank you bhai

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

    Ok rather than taking in vector make are pointer and change whole arr

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

    one doubt why dont we use max heap ie any particular reason for choosing min heap over max heap??? plz answer

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

      see some previous comments, it's explained.

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

    Aditya always green flag 👏

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

    is it merge k sorted arrays?

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

    Why heap?

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

    Bro i-k to i+k ,,,,,,so stack size should be 2k na????????????????????????

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

      No bro !....i-k to i+k is the region in which we will find the sorted element. Size of the stack will be k as we go on popping the elements when it reaches k

    • @AmitKumar-hq4wb
      @AmitKumar-hq4wb 4 ปีที่แล้ว

      @@shreshthsingh7744 bro can u help me with the code

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

      Teetar ke do aage teetar, teetar ke do peeche teetar, bolo kitne teetar?
      --- Teen, not paanch
      Similarly, heap size should be k, not 2k.

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

    sum up 9:40

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

    Ap compatative programming ki bhi video bana do

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

    bhai eak tutorial pen ghumane pe bhi bana do plis

  • @venkateshhs3776
    @venkateshhs3776 9 หลายเดือนก่อน

    Thank you :)

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

    can you still do this if k is unknown?

  • @Deepak-gj4ni
    @Deepak-gj4ni 3 ปีที่แล้ว

    Bhai Why you used priority queue I know they are heap but we are not creating heap it's the priority queue which is queue in itself agr array s min heap bna hota to maza ajata aur

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

    sab question inke flipkart ke interview me aaye huye hai 😂😂

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

    sir mauj krdi aapne to
    bas ho sake to bit pe videos or bana do
    or sir pen ghumana b sikha do

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

    bhai merge sort ka code likhneme dikkat jaati hai ...plss uspe ek video banao

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

      mycodeschool channel dekh lo.
      usme... Selection, Bubble, Insertion, Merge, Quicksort tak ache se hai...
      uske bad Jenny's Lecture mein.. Heap Sort ache se diya hua hai
      itne tak Comparision based sorting algo's hai.. jo books mein hoti hai.. max woh O (n logn) ka performance dega.
      Phir non-comparision based sorting algorithms aa jaega...
      Counting, Radix, Bucket etc etc.

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

    #python implementation
    import heapq
    li=[6,5,3,2,8,10,9]
    heap=[]
    k=3
    v=[]
    for i in range(len(li)):
    heapq.heappush(heap,li[i])
    if len(heap)>k:
    v.append(heap[0])
    heapq.heappop(heap)
    while len(heap)>0:
    v.append(heap[0])
    heapq.heappop(heap)
    print(v)

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

    my reheapifyis not working

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

    GFG Answer:
    #include
    using namespace std;
    int main() {
    vector v;
    priority_queue minh;
    int t;
    cin>>t;
    while(t--){
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i>arr[i];
    v.clear();
    for(int i=0;ik){
    v.push_back(minh.top());
    minh.pop();
    }
    }
    while(minh.size()>0)
    {
    v.push_back(minh.top());
    minh.pop();
    }
    for(int i=0;i

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

    Can we do this questions in O(1) space complexity??

    • @SHASHIKUMAR-pp4hg
      @SHASHIKUMAR-pp4hg 4 ปีที่แล้ว +5

      yup,with insertion sort with timr complexity O(n*k)

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

    Hey you always have the best videos but do you have this problem in English translation? lol
    i tried watching this one but idk you're language, no disrespect.

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

    what is the complexity of inbuilt sort for a vector??

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

      inbuilt sort means ? std::sort() ?
      std::sort() is quick sort, infact most library sorting algorithms are quick sort, which is O(n logn)

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

      @@PabitraPadhy it actually depends on number of elements in vector either quick sort Or merge sort

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

      O(nlog(n))

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

      @@yashodeepdhas8408 How it's depends on vector elements can u explain...?

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

      @@yari4686 std::sort uses intro sort which analyze the size of vector and depending on that apply sorting technique either insertion sort or merge sort on number of elements

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

    youtube should have a 3x button😅

    • @srajanratti8093
      @srajanratti8093 8 หลายเดือนก่อน +1

      You could do that by editing the javascript of the page.

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

    GFG VIDEO CODE :
    class Solution
    {
    public:
    //Function to return the sorted array.
    vector nearlySorted(int arr[], int num, int K){
    priority_queue minh;
    vector v;
    for(int i=0;iK)
    {
    v.push_back(minh.top());
    minh.pop();
    }
    }
    while(minh.size()>0)
    {
    v.push_back(minh.top());
    minh.pop();
    }
    return v;
    }
    };

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

    merci

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

    bro code ka video hai?

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

    great

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

    I think this implementation will also work
    priority_queue minh;
    int j=0;
    for(int i=0;ik){
    a[i-k]=minh.top();
    minh.pop();
    j++;
    }
    }
    while(minh.size()!=0){
    a[j]=minh.top();
    minh.pop();
    j++;

    }

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

    Bro if you could do the videos in English, it would be really great.

  • @M.m554
    @M.m554 ปีที่แล้ว

    Sir plz start graphs😔😭

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

    Bro i watched all heap videos and i think you have skipped heapify in its entirety, pls make vid abt it

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

      You can watch heap playlist by TECH DOSE. Aditya bhaiya had mentioned it in the first video that he would focus on heap stl

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

    Sir, please tell how to implement heap in java.

    • @DINESHKUMAR-gw1kz
      @DINESHKUMAR-gw1kz 4 ปีที่แล้ว +8

      For MinHeap use PriorityQueueminH = new PriorityQueue(); For MaxHeap use PriorityQueuemaxH = new PriorityQueue(Collections.reverseOrder());

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

      @@DINESHKUMAR-gw1kz why you pass Collections.reverseOrder() inside the constructor of priorityQueue ?

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

    3:49

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

    can you exaplin why it is n log k please?

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

      Because we are comparing (sorting) only k elements at a time

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

    Bro sde 2 bn gye

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

    I believe this should be Java soultion let me know if anything can be corrected, Thanks
    static int[] sort(int[] arr, int k) {
    PriorityQueue minH = new PriorityQueue((a,b) -> a - b);
    int[] arr1 = new int[arr.length];
    int j=0;
    for(int i = 0;ik) {
    arr1[j]=minH.poll();
    j++;
    }
    }
    while(minH.size()>0) {
    arr1[j]=minH.poll();
    j++;
    }
    return arr1;
    }

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

    woh sab hatao pehle pen ghumane ki alag se video tutorial banao , mai tabse try kr rha hu

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

    Java GFG Ans:-
    class Solution
    {
    ArrayList nearlySorted(int arr[], int num, int k)
    {
    PriorityQueue minHeap = new PriorityQueue();
    ArrayList al = new ArrayList();
    for(int a: arr){
    minHeap.add(a);
    if(minHeap.size() > k){
    al.add(minHeap.poll());
    }
    }
    while(minHeap.size() != 0){
    al.add(minHeap.poll());
    }
    return al;
    }
    }

  • @aksh.01
    @aksh.01 2 ปีที่แล้ว

    Java Code:
    class Solution{
    ArrayList nearlySorted(int arr[], int n, int k){
    ArrayList list = new ArrayList();
    PriorityQueue minH = new PriorityQueue();
    for(int i=0; i