forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKadane_Algorithm.go
55 lines (44 loc) · 953 Bytes
/
Kadane_Algorithm.go
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
48
49
50
51
52
53
54
55
// Go program to print largest contiguous array sum
package main
import "fmt"
func KAlgo(a []int, n int) int {
var max int
var check_max int
max = 0
check_max = 0
for i := 0; i < n; i++ {
check_max = check_max + a[i]
// Minimum sum will be 0
if check_max < 0 {
check_max = 0
}
if max < check_max {
max = check_max
}
}
return max
}
//Main Function
func main() {
var n int
fmt.Print("Enter number of Elements ")
fmt.Scan(&n)
ope(n)
}
func ope(n int) {
fmt.Print("\nEnter Elements ")
a := make([]int, n)
// Array input
for i := 0; i < n; i++ {
fmt.Scan(&a[i])
}
var output int
// Function Calling for the Algo
output = KAlgo(a, n)
fmt.Println("\nSum of the sub array: ", output)
}
/*
Enter Number of Elements 6
Enter Elements 2 -4 -5 -6 8 10
Sum of the sub array: 18
*/