This one is showing run error for one testcase i have used cummulative sum approach effiecient one
One test case is showing run error becz you have intialised the array in a wrong way before taking input of n from user. But still if you make it accurate then also there will be time limit exceed in one test case.
An efficient solution is :
Let there be a subarray (i, j) whose sum is divisible by n
sum(i, j) = sum(0, j) - sum(0, i-1)
Sum for any subarray can be written as q*n + rem where q is a quotient and rem is remainder
Thus,
sum(i, j) = (q1 * n + rem1) - (q2 * n + rem2)
sum(i, j) = (q1 - q2)n + rem1-rem2
We see, for sum(i, j) i.e. for sum of any subarray to be
divisible by n, the RHS should also be divisible by n.
(q1 - q2)n is obviously divisible by n, for (rem1-rem2) to
follow the same, rem1 = rem2 where
rem1 = Sum of subarray (0, j) % n
rem2 = Sum of subarray (0, i-1) % n
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.