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...
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?
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.
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.
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
@@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]);
@@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?
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
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
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
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.
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
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.
"I just forgot a for loop... happens to the best of us" ahahahha
Haha
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
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...
Finally a fluent English speaker. Thanks man for a great explanation!
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;
Yes I made a mistake in the else if statement. Thankfully it does not effect the outcome of the solution
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.
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
@@xavierelon but why you submit the codes and it works? the test cases never ran into the block of else if ??
Instead of Coffee time with the Tech Lead, it's tea time with Xavier Elon. LOL so clever.
Haha glad someone caught that
bruh with one sentence, you answer the question that I've been struggling for days. I really don't understand the question XD
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?
dude you are gr8!!
Why to start from last position,, why not from first position
men: types on keyboard;
legends: hits keyboard;
i can here, tap tap sound,
Haha it's not even a mechanical keyboard either
Thank you man. Absolutely loved the explanation.
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.
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.
@@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.
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
@@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]);
@@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?
Can we solve this problem without swapping?
Thaaanks dude
Look at the solution and write it :) Very nice :)
Bro you are carrying me in hackerrank, so many washed, poorly explained solutions out there! Thank you!!
Glad I could help
awesome explanation man, thanks!
Thank you Xavier!
You’re welcome
why the loop starts from the end?
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
Can you explain why we check if(q[i] != i+1) and every if conditions (i-1)>=0 , (i-2) >= 0?
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
Thank you for answering this
@@xavierelon Np, happy to help, explaining it helps me better understand it!
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
Can you do the solution of kitty problem
Which one?
@@xavierelon advanced problem in hackerrank named as kitty problem
@@anmolsharma9539 not a fan of that problem. Working on some more popular problems for videos
why is the foor loop from the end of the array instead from the begining ?
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.
nice intro man!
This question was very hard for me
Yes most people tend to find this one difficult
can you make more simple..
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
No none at all Thanks for noticing
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.