HackerRank | New Year Chaos Algorithm Explained (Java + Whiteboard)

Поділитися
Вставка
  • Опубліковано 11 жов 2024

КОМЕНТАРІ • 54

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

    "I just forgot a for loop... happens to the best of us" ahahahha

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

    I agree, HackerRank question wordings are like a riddle on their own, 70% of the time I don't even fully understand what they are asking for

    • @misslaurenbecca
      @misslaurenbecca 6 місяців тому

      Right, I feel like I could have figured this out but I was scratching my head trying to figure out how we would know what the original order even was *facepalm* it's 1 2 3 4 5...

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

    Finally a fluent English speaker. Thanks man for a great explanation!

  • @watchloaf
    @watchloaf 4 роки тому +16

    for the swap on the second else if statement shouldn't be
    int temp = q[i - 2];
    q[i - 2] = q[i - 1];
    q[i - 1] = q[i];
    q[i] = temp;

    • @xavierelon
      @xavierelon  4 роки тому

      Yes I made a mistake in the else if statement. Thankfully it does not effect the outcome of the solution

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

      alternatively, you could do away with the temp variables in both if statements and set q[i] to i+1 since when know that's what it should be.

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

      even simpler, just use std::swap
      swap(q[i-2], q[i-1]);
      swap(q[i-1], q[i]);
      I don't know why no one in this chat is considering using std::swap lol

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

      @@xavierelon but why you submit the codes and it works? the test cases never ran into the block of else if ??

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

    Instead of Coffee time with the Tech Lead, it's tea time with Xavier Elon. LOL so clever.

    • @xavierelon
      @xavierelon  4 роки тому +2

      Haha glad someone caught that

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

    bruh with one sentence, you answer the question that I've been struggling for days. I really don't understand the question XD

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

    So you're basically looping from the back of the array and testing each element as if it was bribed.
    Would it be possible to loop from the front of the array and test each element as if it was the briber?

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

    dude you are gr8!!

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

    Why to start from last position,, why not from first position

  • @animatedzombie64
    @animatedzombie64 4 роки тому +1

    men: types on keyboard;
    legends: hits keyboard;
    i can here, tap tap sound,

    • @xavierelon
      @xavierelon  4 роки тому +1

      Haha it's not even a mechanical keyboard either

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

    Thank you man. Absolutely loved the explanation.

  • @roysantos5386
    @roysantos5386 4 роки тому +5

    I think there is a mistake in the swaps of the else if block. Did you lose the original value of q[i-2]? Interesting though.. your solution still passed all the test cases. Great job.

    • @xavierelon
      @xavierelon  4 роки тому +6

      Lol I'm looking now and it's been a while since I did this problem. Did you figure it out? I think it's swapping them incorrectly but since we don't have to return the sorted list, just the count, it is passing the test cases. Good catch on that one.

    • @roysantos5386
      @roysantos5386 4 роки тому +2

      @@xavierelon you're right, we don't have to return a sorted array or anything. There's probably a solution out there without doing any swaps.

    • @xavierelon
      @xavierelon  4 роки тому

      Roy Santos so I think you actually need to correctly sort i-2 and i-1 because you use those to check the next iteration but i does not need to be correctly swapped since it’s at the end and not checked again which is the element I swapped incorrectly. Very good job catching that though. Thank you

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

      @@roysantos5386 This is what I did, i just used std::swap, but it works the same way. If there was a situation where you would actually need to return the swapped list:
      swap(q[i-2], q[i-1]);
      swap(q[i-1], q[i]);

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

      @@xavierelon Late reply but do you know why in this problem its intended to swap brides in reverse order (reverse for loop)? I managed to get to this solution without looking at this one, but i was missing the fact that I had to iterate in reverse order, thus my test cases were wrong, but that one change fixed it, do you have an explanation for why you have to iterate in reverse order?

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

    Can we solve this problem without swapping?

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

    Thaaanks dude

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

    Look at the solution and write it :) Very nice :)

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

    Bro you are carrying me in hackerrank, so many washed, poorly explained solutions out there! Thank you!!

  • @sye119
    @sye119 4 роки тому +1

    awesome explanation man, thanks!

  • @_-6912
    @_-6912 4 роки тому +1

    Thank you Xavier!

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

    why the loop starts from the end?

    • @carloseduardo-im4ss
      @carloseduardo-im4ss 3 роки тому +3

      Because if it starts from the beginning, you won't be able to tell for sure how many swaps you did, take for example the array [2,3,4,5,1], every position swapped only once, still if we start looking from the beggining you would endup printing too chaotic, since the number 1 ended on the last position in the array.... I guess that's why, hope you can understand it

  • @marcinromaniak1549
    @marcinromaniak1549 4 роки тому +2

    Can you explain why we check if(q[i] != i+1) and every if conditions (i-1)>=0 , (i-2) >= 0?

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

      We check if(q[i] != i+1) because say for index 0, the value should be 1 if it wasnt swapped, so q[0] should equal 1, this repeates for all elements in the array as if this inequality is true, then swaps must have occured.
      The next two if conditions are checking if we should count it sa 1 swap, or as 2 swaps, I was hoenstly a little confused myself until he added the for loop showing we are iterating BACKWARDS... It makes much more sense this way, I tried doing it forwards and the logic is a little more sketchy

    • @xavierelon
      @xavierelon  4 роки тому +1

      Thank you for answering this

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

      @@xavierelon Np, happy to help, explaining it helps me better understand it!

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

      i + 1 will give the current index plus one. This is the bribe id expected when its in order {1, 2, 3, 4, 5}. So, when you compare lets say bribe 2, its index is 1, but its value its 2, so if you add 1 and compare that with the queue at the current index, if its already equal to each other, then you dont need to do any swapping, because its already in the correct order.
      Remember, this problem is not about sorting the list, its about checking if each bribe is able to swap positions with other bribes while only having to swap

  • @anmolsharma9539
    @anmolsharma9539 4 роки тому +2

    Can you do the solution of kitty problem

    • @xavierelon
      @xavierelon  4 роки тому +1

      Which one?

    • @anmolsharma9539
      @anmolsharma9539 4 роки тому

      @@xavierelon advanced problem in hackerrank named as kitty problem

    • @xavierelon
      @xavierelon  4 роки тому +2

      @@anmolsharma9539 not a fan of that problem. Working on some more popular problems for videos

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

    why is the foor loop from the end of the array instead from the begining ?

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

      I was wondering about that too. It wasn't obvious why it had to be in reverse order. I actually got this problem without looking at this video, but the "reverse iteration" was the only thing I missed from that problem. Everything else in my algorithm worked but because i missed that minor detail, i got the wrong test cases. It doesn't say anywhere in the problem that the bribes want to swap positions starting from the back of the queue instead of the front 🤷‍♂
      I guess in real life people in the back of the queue would want to swap more than the front? That part would make sense but the question is extremely misleading in that regard. I wouldn't worry about it if thats the only minor detail you missed.

  • @animatedzombie64
    @animatedzombie64 4 роки тому +1

    nice intro man!

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

    This question was very hard for me

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

      Yes most people tend to find this one difficult

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

    can you make more simple..

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

    Did you put any effort into this at all? I mean, you don't even explain your algorithm before you jump into coding. I have no idea what you're trying to do or WHY. This is not helpful

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

      No none at all Thanks for noticing

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

    This solution suppose that each person can be bribed only once. For example: 1 2 3 4 5 6 7 8 -> 3 bribes 2 -> 1 3 2 4 5 6 7 8 -> 5 bribes 2 -> 1 3 5 4 2 6 7 8 ---> Your algorithm would point that someone bribed more than two people but it is because number 2 was bribed twice... the problem is not done well done... it should say that each person can be bribed only once.