Last active
December 23, 2015 12:49
-
-
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)
This file contains 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
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