Created
June 28, 2021 15:12
-
-
Save terencechain/39e3ee27ef67482353317318295ad2c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem: Sum Swap | |
// | |
// Given two array of integers, find a pair of values (one value from each array) that you can swap | |
// to give the two arrays the same sum. | |
// | |
// Input:{4,1,2,1,1,2} and {3, 6, 3, 3} | |
// Output: {1, 3} | |
package main | |
import ( | |
"reflect" | |
"testing" | |
) | |
const maxInt = int(^uint(0) >> 1) | |
func TestSumSwap(t *testing.T) { | |
type tc struct { | |
A []int | |
B []int | |
output []int | |
} | |
tests := []tc{ | |
tc{[]int{1, 1}, []int{1, 1}, nil}, | |
tc{[]int{1, 2}, []int{1, 3}, nil}, | |
tc{[]int{1, 3, 4}, []int{3, 4, 5}, []int{1,3}}, | |
tc{[]int{100, 0}, []int{150, 50}, []int{100, 150}}, | |
tc{[]int{4, 1, 2, 1, 1, 2}, []int{3, 6, 3, 3}, []int{4,6}}, | |
} | |
for _, tc := range tests { | |
if actual := sumSwapBruteForce(tc.A, tc.B); !reflect.DeepEqual(actual, tc.output) { | |
t.Errorf("%v %v failed, wanted %v, got %v", tc.A, tc.B, tc.output, actual) | |
} | |
if actual := sumSwapEfficient(tc.A, tc.B); !reflect.DeepEqual(actual, tc.output) { | |
t.Errorf("%v %v failed, wanted %v, got %v", tc.A, tc.B, tc.output, actual) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment