diff --git a/Number Theory/kdane's algorithm b/Number Theory/kdane's algorithm new file mode 100644 index 0000000..ca889bb --- /dev/null +++ b/Number Theory/kdane's algorithm @@ -0,0 +1,28 @@ +// To find the maximum value of subarray +#include +using namespace std; + +// Function to find the sum of subarray with maximum sum +int maxSubarraySum(vector &arr) { + int res = arr[0]; + + // Outer loop for starting point of subarray + for(int i = 0; i < arr.size(); i++) { + int currSum = 0; + + // Inner loop for ending point of subarray + for(int j = i; j < arr.size(); j++) { + currSum = currSum + arr[j]; + + // Update res if currSum is greater than res + res = max(res, currSum); + } + } + return res; +} + +int main() { + vector arr = {2, 3, -8, 7, -1, 2, 3}; + cout << maxSubarraySum(arr); + return 0; +}