Skip to content

Instantly share code, notes, and snippets.

@Vaib215
Created September 25, 2022 23:04
Show Gist options
  • Save Vaib215/1f1cbd8237a46627297cac411cb0c203 to your computer and use it in GitHub Desktop.
Save Vaib215/1f1cbd8237a46627297cac411cb0c203 to your computer and use it in GitHub Desktop.
Prefix Sum => TC -> O(N^2)
public static void printMaxSumSubArr(int a[]){
int currSum=0, maxSum=Integer.MIN_VALUE, prefix[] = new int[a.length];
prefix[0] = a[0];
for (int i = 1; i < a.length; i++) {
prefix[i] = prefix[i-1] + a[i];
}
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
currSum = i==0?prefix[j]:prefix[j]-prefix[i-1];
}
maxSum = maxSum<currSum ? currSum : maxSum;
}
System.out.println(maxSum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment