Skip to content

Instantly share code, notes, and snippets.

@orhanobut
Last active December 23, 2015 12:49
Show Gist options
  • Save orhanobut/6637865 to your computer and use it in GitHub Desktop.
Save orhanobut/6637865 to your computer and use it in GitHub Desktop.
Finds the first sequence k of a given integer array. Complexity is O(n^2). It can be done with only o(n)
public static void findFirstSeq(int[] a, int k) {
int size = a.length;
for (int i = 0; i < size; i++) {
int total = 0;
for (int j = i; j < size; j++) {
total += a[j];
if (total > k) {
break;
} else if (total == k) {
System.out.println("FOUND IT " + a[i] + ", " + a[j]);
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment