/**
* Continuos Sub-Sequence divisible by a number
* @author Prateek
*/
class SumSubSequence {
public static void main
(String[] args
) { //int[] arr={1, 1, 9 , 7, 12 , 5 , 8 , 2, 7, 2, 10, 2, 3};
//int divisor=13;
int[] arr={1 , 1 , 3 , 2 , 4, 1 ,4 , 5};
int divisor=5;
solve(arr,divisor);
}
/**
* Sub-Routine to count numnber of sub-sequnces
* @param d: the given divisor
*/
public static int solve(int[] arr, int d)
{
int Answer = 0;
int[] hash = new int[d];
int sum = 0;
int val;
int num;
for (int i = 0; i < arr.length; i++) {
num = arr[i];
if(num % d == 0) // counte numbes which are divisible by divisor
Answer ++ ;
sum += num;
val = sum % d;
if(val<0) //handle negative numbers
val = val * (-1);
hash[val] = hash[val] + 1;
}
int size=hash.length ;
for (int i = 0; i < size; i++) {
int count = hash[i];
if(count > 1)
Answer = Answer + count * (count -1)/2 ;
}
System.
out.
println(Answer
+hash
[0]); return Answer+hash[0];
}
}
LyoqCiAqIENvbnRpbnVvcyBTdWItU2VxdWVuY2UgZGl2aXNpYmxlIGJ5IGEgbnVtYmVyCiAqIEBhdXRob3IgUHJhdGVlawogKi8KIGNsYXNzIFN1bVN1YlNlcXVlbmNlIHsKCglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CgkJLy9pbnRbXSBhcnI9ezEsIDEsIDkgLCA3LCAxMiAsIDUgLCA4ICwgMiwgNywgMiwgMTAsIDIsIDN9OyAKCQkvL2ludCBkaXZpc29yPTEzOwoJCWludFtdIGFycj17MSAsIDEgLCAzICwgMiAsIDQsIDEgLDQgLCA1fTsKCQlpbnQgZGl2aXNvcj01OwoJCXNvbHZlKGFycixkaXZpc29yKTsKCX0KCgkvKioKCSAqIFN1Yi1Sb3V0aW5lIHRvIGNvdW50IG51bW5iZXIgb2Ygc3ViLXNlcXVuY2VzCgkgKiBAcGFyYW0gZDogdGhlIGdpdmVuIGRpdmlzb3IKCSAqLwoJcHVibGljIHN0YXRpYyBpbnQ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