forked from TechyGuyAditya/Hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.java
More file actions
47 lines (41 loc) · 1.29 KB
/
dijkstra.java
File metadata and controls
47 lines (41 loc) · 1.29 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Java Program to Implement Kadane Algorithm
*/
import java.util.Scanner;
/* Class kadane */
public class Kadane
{
/* Function to largest continuous sum */
public int maxSequenceSum(int[] arr)
{
int maxSoFar = arr[0], maxEndingHere = arr[0];
for (int i = 1; i < arr.length; i++)
{
/* calculate maxEndingHere */
if (maxEndingHere < 0)
maxEndingHere = arr[i];
else
maxEndingHere += arr[i];
/* calculate maxSoFar */
if (maxEndingHere >= maxSoFar)
maxSoFar = maxEndingHere;
}
return maxSoFar;
}
/* Main function */
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Kadane Algorithm Test\n");
/* Make an object of Kadane class */
Kadane k = new Kadane();
System.out.println("Enter size of array :");
int N = scan.nextInt();
/* Accept two 2d matrices */
System.out.println("Enter "+ N +" elements");
int[] arr = new int[N];
for (int i = 0; i < N; i++)
arr[i] = scan.nextInt();
int sum = k.maxSequenceSum(arr);
System.out.println("\nMaximum Sequence sum = "+ sum);
}
}