Here in the code, we are not checking that whether the sum is -ve or +ve.
Still we are doing sum%=n for both +ve and -ve values of sum. Then why there is a need to do the sum = (sum+n)%n operation?
And what will -12%5 will give?
Here in the code, we are not checking that whether the sum is -ve or +ve.
Still we are doing sum%=n for both +ve and -ve values of sum. Then why there is a need to do the sum = (sum+n)%n operation?
And what will -12%5 will give?
Why don’t we test it out with an example?
Take −100 mod 8=4. This is because 8⋅−13=−104. The remainder is 4.
So now let’s take (37−54) mod 5. It’s equal to −17 mod 5=3. Substitute in and do the computation: Method 1 gives 3, which is what we want, and method 2 gives −2, so the correct approach is method 1.
Just confirm me, what is method 1 and 2?
method 1 is (a+n)%n to take modulo
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.