Skip to content

Commit

Permalink
geeksforgeeks: add minimum and maximum array elements
Browse files Browse the repository at this point in the history
  • Loading branch information
minizilla committed Nov 24, 2023
1 parent 20d224c commit 3c8ec2c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
12 changes: 12 additions & 0 deletions geeksforgeeks/minimum-and-maximum-array-elements/README.md
Original file line number Diff line number Diff line change
@@ -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).
32 changes: 32 additions & 0 deletions geeksforgeeks/minimum-and-maximum-array-elements/solution.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions geeksforgeeks/minimum-and-maximum-array-elements/solution_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/minizilla/minmax

go 1.21.3

require github.com/minizilla/testr v0.2.3
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 3c8ec2c

Please sign in to comment.