Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated and easily understandable Kadane's Alogorithm #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Kadane’s Algorithm/maxSubarraySum.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
public class maxSubarraySum {
// Function to find the maximum subarray sum
public static long maxSubarraySum(int[] arr, int n) {
long maxi = Long.MIN_VALUE; // Initialize maximum sum as Long.MIN_VALUE (negative infinity)
long sum = 0; // Initialize current sum as zero
long maxSum = Long.MIN_VALUE; // Initialize maximum sum as Long.MIN_VALUE (negative infinity)
long currentSum = 0; // Initialize current sum as zero

// Traverse through the array
//where n is the Size of thhe array
for (int i = 0; i < n; i++) {

sum += arr[i]; // Add the current element to the current sum
currentSum += arr[i]; // Add the current element to the current sum

if (sum > maxi) {
maxi = sum; // If the current sum is greater than the maximum sum, update the maximum sum
if (currentSum > maxSum) {
maxSum = currentSum; // If the current sum is greater than the maximum sum, update the maximum sum
}

// If the current sum becomes negative, reset it to zero
if (sum < 0) {
sum = 0;
if (currentSum < 0) {
currentSum = 0;
}
}

// Uncomment the following check if you want to consider the sum of an empty subarray as zero
//if (maxi < 0) maxi = 0;

return maxi; // Return the maximum subarray sum
return maxSum; // Return the maximum subarray sum
}

public static void main(String args[]) {
int[] arr = {-1,-2,-3,-4};
int[] arr = {-2,-3,4,-1,-2,1,5,-3};
int n = arr.length;
long maxSum = maxSubarraySum(arr, n);
System.out.println("The maximum subarray sum is: " + maxSum);
Expand Down
96 changes: 66 additions & 30 deletions Question 4/MaximumSubArray.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
package gfg_practice.arrays.easy;

import java.util.ArrayList;
import java.util.*;

public class MaximumSubArray {
public static void main(String[] args) {
int[] arr = {138, 101, 170, 125, 79, 159, 163, 65, 106, 146, 82, 28, 162, 92, 196, 143, 28, 37, 192, 5, 103, 154, 93, 183, 22, 117, 119, 96, 48, 127, 172, 139, 70, 113, 68, 100, 36, 95, 104, 12, 123, 134};
System.out.println(subArraySum(arr, 42, 468).toString());
int[] arr = {-2,-3,4,-1,-2,1,5,-3};
int n = arr.length;
//Assign masSum to maxSubArray1 if you want to follow brute - force approach
//int maxSum = maxSubArray1(arr, n);

//Assign masSum to maxSubArray1 if you want to follow prefixArray Approach to find Maximum SubArray Sum.
// int maxSum = maxSubArraySum2(arr);

//If you want to use Kadane's Algorith assign maxSum to kadane
int maxSum = kadane(arr);
System.out.println("The maximum subarray sum is: " + maxSum);

}
// Using O(n^3) Time Complexity
public static int maxSubArray1(int[] arr, int n){
int currentSum = 0;
int maxSum = Integer.MIN_VALUE;

for(int i= 0;i<n;i++){
for(int j = i; j<n;j++){
currentSum = 0;
for(int k = i;k<=j;k++){
currentSum += arr[k];
}

if(maxSum<currentSum){
maxSum = currentSum;
}
}
}
return maxSum;
}
//To improve complexity, we use prefixSum Array
//Time complexity of prefixSum Array is -> O(n^2)

static ArrayList<Integer> subArraySum(int[] arr, int n, int s) {
ArrayList<Integer> a = new ArrayList<>();
int l = 0, r = 0;
int sum = arr[0];
if (s == 0) {
a.add(-1);
return a;
public static int maxSubArraySum2(int[] arr){
int currentSum = 0;
int maxSum = Integer.MIN_VALUE;
int n = arr.length;
int[] prefix = new int[n];
prefix[0] = arr[0];

for(int i= 1;i<prefix.length;i++){
prefix[i] = prefix[i-1]+arr[i];
}
boolean isFound = false;
while (r < n) {
if (sum == s) {
isFound = true;
break;
} else if (sum < s) {
r++;
if (r < n) sum += arr[r];
} else {
sum -= arr[l];
l++;
for(int i = 1;i<n;i++){
for(int j = i; j<n;j++){
if(i==0){
currentSum = prefix[i];
}
currentSum = prefix[j]-prefix[i-1];
if(maxSum<currentSum){
maxSum = currentSum;
}
}
}
if (isFound) {
a.add(l + 1);
a.add(r + 1);
return a;
}
return maxSum;
}

a.add(-1);
return a;
//Using Kadane's Algorithm, Time complexity is reduced to -> O(n)
public static int Kadane(int[] arr){
int maxSum = Integer.MIN_VALUE;
int currentSum = 0;
for(int i = 0;i<arr.length;i++){
currentSum+=arr[i];
if(currentSum<0){
currentSum = 0;
}
maxSum = Math.max(currentSum, maxSum);
}
return maxSum;
}
}