summaryrefslogtreecommitdiff
path: root/exercises/4.1-2/max_subarray_test.go
blob: 920a3fbdb3797966500bfa185bc45042ea41552b (plain)
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
package main

import (
	"fmt"
	"testing"
)

func MaxSubArray(a []int, low int, high int) (int, int, int) {
	left := low
	right := high
	sum := -1

	for i := low; i < high; i++ {
		tempSum := 0
		for j := i; j < high; j++ {
			tempSum = tempSum + a[j]
			if tempSum > sum {
				sum = tempSum
				left = i
				right = j
			}
		}
	}
	return left, right, sum
}

func TestMergeSort(t *testing.T) {
	t.Run("", func(t *testing.T) {
		items := []int{100, 113, 110, 85, 105, 102, 86, 63, 81, 101, 94, 106, 101, 79, 94, 90, 97}
		l, r, total := MaxSubArray(items, 0, len(items))

		fmt.Println(l, r, total)
		if l != 0 {
			t.Errorf("Expected: %v, Got: %v", 0, l)
		}
		if r != 16 {
			t.Errorf("Expected: %v, Got: %v", 16, r)
		}
		if total != 1607 {
			t.Errorf("Expected: %v, Got: %v", 1607, total)
		}
	})
}