Divisible subarrays problm
TLE is due to the N^2 part.
Let me give you some hint to solve this problem in O(N).
- You have created an array c. If we say that c[5] is 4 for eg. If we pick any 2 entries out of these for entries of the array, and take the subarray between these chosen subindexes, then it will be divisible by N. Now of ways of doing this is 4 C 2, or 4*3/2 = 6.
- Repeat this for 0-(N-1)
- Remember c[0] = 1 initially.
Initialise array c using a loop as c[n]={0} does not initialise array to 0.
int c[100001] = {0} will also work and initialise array with 0.
i have initialised it via ll int c[i]={0} and still its showing wrong answer on submission
Array contains negative numbers and you are using unsigned long long int which is creating problems.
Here is the corrected code:
https://ide.codingblocks.com/s/44586
https://ide.codingblocks.com/s/44599
sorry just found out -ve cases arent being handled correctly , i will make more corrections
corrected code
https://ide.codingblocks.com/s/44644
thanks i solved the qn
https://ide.codingblocks.com/s/44856
It is giving run error in one test case.
How can i correct that ?
Hey, you are getting run error because you are performing modulo operation of v[i] only and your sum variable is still adding the array elements which leads it to cross the range of long long int. So, if you want to use sum variable to add the array elements perform the modulo operations on it also to avoid the run error.