Problem in understanding code

Hi,
Regarding the code used by prateek bhaiya .
I can’t understand how is the sum being maintained because we take (sum%n) after adding a[i] to sum . So, how is the sum being maintained . Also , what is the need for sum=(sum+n)%n after initially doing sum%n

hello @imsharan0105 let us take the exaple of any array .
suppose n=5;
array is 1 1 1 1 1 .
here sum is 1 and 1 % 5 is then if we add n to it the sum will be 6 and then also if we take the mod with n i.e 5 then also the sum will remain 1 because when we divide 6 by 5 the remainder is 1.
in the next step sum=2.sum%(2%5)=2 only and after that 2+5=7 and 7%5=2 only in this way the sum is being maintained .
we have done this (sum+n)%n because in some cases our sum could go in the negative as well to handle that we have to do that .
like suppose our sum till now is -5 and we add 3 to it (-5+3)%5=-2 and in order to make it positive we have to do this .
-2+5=3 and 3%5=3.
because our sum till previous element was -5.
but in the frequency array we want to store that frequence of 5 not of -5 .that why we are doing like this .
if it was 5 and we add 3 to it then 8%5 would come out to be 3 that why we have added n to it to make it positive .
in this manner our same is maintained.
I hope I have cleared your doubt .please hit a thumbs up button if you feel that your query is resolved and please mark it as resolved .
Happy Learning !!

Hey ,
Thank you for the explanation .I understood it properly .

hello @imsharan0105 as you have said that you have understood this means that your doubt is now cleared and you can now mark this doubt as resolved and do hit the like button .
Happy Learning !!

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.