Created
September 25, 2022 23:04
-
-
Save Vaib215/1f1cbd8237a46627297cac411cb0c203 to your computer and use it in GitHub Desktop.
Prefix Sum => TC -> O(N^2)
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 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