-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
geeksforgeeks: add minimum and maximum array elements
- Loading branch information
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
geeksforgeeks/minimum-and-maximum-array-elements/README.md
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
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
32
geeksforgeeks/minimum-and-maximum-array-elements/solution.go
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
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
30
geeksforgeeks/minimum-and-maximum-array-elements/solution_test.go
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
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) | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
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
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 |
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
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= |