-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathis_monotonic.go
39 lines (34 loc) · 1.01 KB
/
is_monotonic.go
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
/*
An array is said to be monotonic in nature if it is either continuously increasing or continuously decreasing.
Mathematically, An array A is continuously increasing if for all i <= j, A[i] <= A[j].
*/
package main
import "fmt"
func IsMonotonic(Arr []int) bool {
if len(Arr) < 2 {
return true
}
isIncreasing := 0
for i := 1; i < len(Arr); i++ {
if isIncreasing == 0 {
if Arr[i - 1] > Arr[i] {
isIncreasing = -1 // means we will check for decreasing
} else if Arr[i - 1] < Arr[i] {
isIncreasing = 1 // means we will check for increasing
}
}
if isIncreasing == 1 && Arr[i - 1] > Arr[i] {
return false // in increasing array element before other element cannot be less so return false
}
if isIncreasing == -1 && Arr[i - 1] < Arr[i] {
return false // in decreasing array element after other element cannot be greater so return false
}
}
return true
}
func main() {
Arr := []int{1, 2, 3, 4, 5}
Arr2 := []int{5, 4, 3, 2, 1}
fmt.Println(IsMonotonic(Arr))
fmt.Println(IsMonotonic(Arr2))
}