Skip to content

Instantly share code, notes, and snippets.

@digvijaybhakuni
Last active May 19, 2016 11:30
Show Gist options
  • Save digvijaybhakuni/945e589a4f78ac959c635a0d8f9f5833 to your computer and use it in GitHub Desktop.
Save digvijaybhakuni/945e589a4f78ac959c635a0d8f9f5833 to your computer and use it in GitHub Desktop.
TestSubSetProblem2
import java.util.Arrays;
public class TestSubSetProblem {
public TestSubSetProblem() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
int[] arr = { -1, 8, 4, -2, 5, -9, 56, 9, -5, 9, 8, -8, -9 };
int len = arr.length;
int maxSum = 0;
int maxTemp = 0;
int s = 0, e = 0, tempS = 0, tempE = 0;
boolean isSeq = false;
for (int i = 0; i < len; i++) {
if (arr[i] > 0) {
if (!isSeq) {
tempS = i;
}
maxTemp += arr[i];
isSeq = true;
} else if (isSeq) {
tempE = i - 1;
// maxTemp = getSum(arr, tempS, tempE);
if (maxTemp > maxSum) {
maxSum = maxTemp;
s = tempS;
e = tempE;
}
maxTemp = 0;
isSeq = false;
}
}
print(Arrays.copyOfRange(arr, s, e + 1));
}
static void print(int[] a) {
for (int i : a) {
System.out.print(i + ", ");
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment