From 8b4ceb57168461a82f9ebfe1c7db126f51f12d69 Mon Sep 17 00:00:00 2001 From: Billy Zaelani Malik Date: Fri, 24 Nov 2023 11:20:44 +0700 Subject: [PATCH] geeksforgeeks: add minimum and maximum array elements --- .../README.md | 12 +++++++ .../solution.go | 32 +++++++++++++++++++ .../solution_test.go | 30 +++++++++++++++++ .../minimum-swap-array-sort/README.md | 10 ++++++ go.mod | 2 ++ go.sum | 2 ++ 6 files changed, 88 insertions(+) create mode 100644 geeksforgeeks/minimum-and-maximum-array-elements/README.md create mode 100644 geeksforgeeks/minimum-and-maximum-array-elements/solution.go create mode 100644 geeksforgeeks/minimum-and-maximum-array-elements/solution_test.go create mode 100644 geeksforgeeks/minimum-swap-array-sort/README.md create mode 100644 go.sum diff --git a/geeksforgeeks/minimum-and-maximum-array-elements/README.md b/geeksforgeeks/minimum-and-maximum-array-elements/README.md new file mode 100644 index 0000000..871894f --- /dev/null +++ b/geeksforgeeks/minimum-and-maximum-array-elements/README.md @@ -0,0 +1,12 @@ +# Minimum and Maximum Array Elements + +Given an array of size **N**. The task is to find the maximum and the minimum element of the array. + +## Copyright Notice + +This problem is based on [content](https://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/) +from [GeeksforGeeks](https://www.geeksforgeeks.org) +written by GeeksforGeeks +and subject to [GeeksforGeeks copyright](https://www.geeksforgeeks.org/legal/copyright-information/). +The original content from GeeksforGeeks and any modifications made here are attributed to GeeksforGeeks contributors, +and this work is shared under [CC BY-SA 4.0](../LICENSE). diff --git a/geeksforgeeks/minimum-and-maximum-array-elements/solution.go b/geeksforgeeks/minimum-and-maximum-array-elements/solution.go new file mode 100644 index 0000000..9e1f443 --- /dev/null +++ b/geeksforgeeks/minimum-and-maximum-array-elements/solution.go @@ -0,0 +1,32 @@ +package minimum_and_maximum_array_elements + +func Solution(arr []int) (min, max int) { + length := len(arr) + + if length == 0 { + return 0, 0 + } + + if length == 1 { + return arr[0], arr[0] + } + + min, max = arr[0], arr[1] + if min > max { + min, max = max, min + } + + for i := 2; i < length; i++ { + v := arr[i] + + if v < min { + min = v + } + + if v > max { + max = v + } + } + + return min, max +} diff --git a/geeksforgeeks/minimum-and-maximum-array-elements/solution_test.go b/geeksforgeeks/minimum-and-maximum-array-elements/solution_test.go new file mode 100644 index 0000000..b8ca880 --- /dev/null +++ b/geeksforgeeks/minimum-and-maximum-array-elements/solution_test.go @@ -0,0 +1,30 @@ +package minimum_and_maximum_array_elements_test + +import ( + "testing" + + sut "github.com/minizilla/minmax/geeksforgeeks/minimum-and-maximum-array-elements" + "github.com/minizilla/testr" +) + +func TestMinimumAndMaximumArrayElements(t *testing.T) { + tests := map[string]struct { + arr []int + min, max int + }{ + "zero": {nil, 0, 0}, + "one": {[]int{5}, 5, 5}, + "ascending": {[]int{5, 7, 8, 10, 12}, 5, 12}, + "descending": {[]int{12, 10, 8, 7, 5}, 5, 12}, + "random": {[]int{7, 5, 12, 8, 10}, 5, 12}, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + assert := testr.New(t) + min, max := sut.Solution(tc.arr) + assert.Equal(min, tc.min) + assert.Equal(max, tc.max) + }) + } +} diff --git a/geeksforgeeks/minimum-swap-array-sort/README.md b/geeksforgeeks/minimum-swap-array-sort/README.md new file mode 100644 index 0000000..e8d3ef8 --- /dev/null +++ b/geeksforgeeks/minimum-swap-array-sort/README.md @@ -0,0 +1,10 @@ +# Minimum number of swaps required to sort an array + +## Copyright Notice + +This [questions](https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/) +is based on content from [GeeksforGeeks](https://www.geeksforgeeks.org) +written by Ayush Khanduri +and subject to [GeeksforGeeks copyright](https://www.geeksforgeeks.org/legal/copyright-information/). +The original content from GeeksforGeeks and any modifications made here are attributed to GeeksforGeeks contributors, +and this work is shared under [CC BY-SA 4.0](../LICENSE). diff --git a/go.mod b/go.mod index 2359e69..2f8225b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/minizilla/minmax go 1.21.3 + +require github.com/minizilla/testr v0.2.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0dbb932 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/minizilla/testr v0.2.3 h1:Fz6Xo8CBACAFeWygvyRZspt0tF5h6mNJg3duIsEZxgM= +github.com/minizilla/testr v0.2.3/go.mod h1:DdZPGzN8GgQbC2QZ24W/BaWI88xCKUKQ6YLcV/g3hvQ=