Created
November 22, 2019 07:53
-
-
Save arafatkamaal/296c18bf10405c7522a8c830a1490a4b to your computer and use it in GitHub Desktop.
Given a array and a number k, find all contigious array sequences that are divisible by k
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Given a array and a number k, find all contigious array sequences that are divisible by k | |
* | |
* | |
* | |
*/ | |
public class KSubsequences { | |
public static int ksub(int array[], int k) { | |
int startWindow = 0; | |
int sum = 0; | |
int result = 0; | |
int l = array.length; | |
while(l != 0) { | |
for(int i = startWindow; i < array.length; i++) { | |
sum += array[i]; | |
if (sum % k == 0) { | |
result++; | |
} | |
} | |
sum = 0; | |
startWindow++; | |
l--; | |
} | |
return result; | |
} | |
public static void main(String args[]) { | |
System.out.println("Beginning of the program"); | |
System.out.println(ksub(new int[]{5, 10, 11, 9, 5}, 5)); | |
System.out.println(ksub(new int[]{1, 2, 3, 4, 1}, 3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment