Wrong answer for one test case

can’t find the problem with my code

We need to find such a pair of indices (i, j) that they satisfy the above condition. Here is the algorithm :

  1. Make an auxiliary array of size k as Mod[k] . This array holds the count of each remainder we are getting after dividing cumulative sum till any index in arr[].
  2. Now start calculating cumulative sum and simultaneously take it’s mod with K, whichever remainder we get increment count by 1 for remainder as index in Mod[] auxiliary array. Sub-array by each pair of positions with same value of ( cumSum % k) constitute a continuous range whose sum is divisible by K.
  3. Now traverse Mod[] auxiliary array, for any Mod[i] > 1 we can choose any to pair of indices for sub-array by (Mod[i]*(Mod[i] – 1))/2 number of ways . Do the same for all remainders < k and sum up the result that will be the number all possible sub-arrays divisible by K.

Please have a look at this short code to have an idea.


You can share your code via ide.codingblocks.com if you are using the same approach but still unable to debug your code.