We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Get the error now (at least I hope)
So I originally ended up with
return(m+s-1)%n;
And didn't understand what the error really was, it seemed right.
So to help out others here's a quick explanation, if I got this right the issue is when
(m + s - 1) % n evaluates to 0. The prisoners have a base of 1 not 0. Per the example you count 1 2 3 4 1 2 3 4, not 1 2 3 0 1 2 3 0, never is there a 0. Therefore % 4 returning 0 should actually still be 4, in the example, n.
So given:
return(m+s-1)%n||n;
If (m + s - 1) % n evaluates to 0 we will be FALSEY so we use the || operator and return n instead (4 in the example case)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Save the Prisoner!
You are viewing a single comment's thread. Return to all comments →
Get the error now (at least I hope) So I originally ended up with
And didn't understand what the error really was, it seemed right. So to help out others here's a quick explanation, if I got this right the issue is when (m + s - 1) % n evaluates to 0. The prisoners have a base of 1 not 0. Per the example you count 1 2 3 4 1 2 3 4, not 1 2 3 0 1 2 3 0, never is there a 0. Therefore % 4 returning 0 should actually still be 4, in the example, n.
So given:
If (m + s - 1) % n evaluates to 0 we will be FALSEY so we use the || operator and return n instead (4 in the example case)