forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kadanesAlgorithm.java
33 lines (27 loc) · 972 Bytes
/
kadanesAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
class KadanesAlgorithm {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int maxSum = kadanesAlgorithm(arr);
System.out.println("The maximum sum of a contiguous subarray is: " + maxSum);
sc.close();
}
public static int kadanesAlgorithm(int[] arr) {
int maxCurrent = arr[0];
int maxGlobal = arr[0];
for (int i = 1; i < arr.length; i++) {
maxCurrent = Math.max(arr[i], maxCurrent + arr[i]);
if (maxCurrent > maxGlobal) {
maxGlobal = maxCurrent;
}
}
return maxGlobal;
}
}