fork(17) download
  1. /**
  2.  * Continuos Sub-Sequence divisible by a number
  3.  * @author Prateek
  4.  */
  5. class SumSubSequence {
  6.  
  7. public static void main(String[] args) {
  8. //int[] arr={1, 1, 9 , 7, 12 , 5 , 8 , 2, 7, 2, 10, 2, 3};
  9. //int divisor=13;
  10. int[] arr={1 , 1 , 3 , 2 , 4, 1 ,4 , 5};
  11. int divisor=5;
  12. solve(arr,divisor);
  13. }
  14.  
  15. /**
  16. * Sub-Routine to count numnber of sub-sequnces
  17. * @param d: the given divisor
  18. */
  19. public static int solve(int[] arr, int d)
  20. {
  21. int Answer = 0;
  22. int[] hash = new int[d];
  23.  
  24. int sum = 0;
  25. int val;
  26. int num;
  27.  
  28. for (int i = 0; i < arr.length; i++) {
  29. num = arr[i];
  30.  
  31. if(num % d == 0) // counte numbes which are divisible by divisor
  32. Answer ++ ;
  33.  
  34. sum += num;
  35. val = sum % d;
  36.  
  37. if(val<0) //handle negative numbers
  38. val = val * (-1);
  39.  
  40. hash[val] = hash[val] + 1;
  41. }
  42.  
  43. int size=hash.length ;
  44. for (int i = 0; i < size; i++) {
  45. int count = hash[i];
  46.  
  47. if(count > 1)
  48. Answer = Answer + count * (count -1)/2 ;
  49. }
  50. System.out.println(Answer+hash[0]);
  51. return Answer+hash[0];
  52. }
  53. }
  54.  
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
11