Given an input array we can find a single sub-array which sums to K (given) in linear time, by keeping track of sum found so far and the start position. If the current sum b
Quadratic Time: O(n2) in worst case.
private static void findSubArray(int[] is, int N) {
System.out.println("Continuous sub array of " + Arrays.toString(is) + " whose sum is " + N + " is ");
List arry = new ArrayList<>(is.length);
for (int i = 0; i < is.length; i++) {
int tempI = is[i];
arry.add(tempI);
for (int j = i + 1; j < is.length; j++) {
if (tempI + is[j] == N) {
arry.add(is[j]);
System.out.println(arry);
} else if (tempI + is[j] < N) {
arry.add(is[j]);
tempI = tempI + is[j];
} else {
arry.clear();
break;
}
}
}
}
public static void main(String[] args) {
findSubArray(new int[] { 42, 15, 12, 8, 6, 32 }, 26);
findSubArray(new int[] { 12, 5, 31, 13, 21, 8 }, 49);
findSubArray(new int[] { 15, 51, 7, 81, 5, 11, 25 }, 41);
}