Skip to content

Commit 961c1c8

Browse files
authored
Merge pull request #2 from ehsan-a-kian/AlphabetSoup
ArithGeo resolved
2 parents f460d39 + cecdc58 commit 961c1c8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/easy/ArithGeo.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// ArithGeo takes the array of numbers stored in arr and returns "Arithmetic" if the sequence follows an arithmetic pattern,
8+
// "Geometric" if it follows a geometric pattern, or -1 if neither pattern is found.
9+
func ArithGeo(arr []int) string {
10+
arithInterval := arr[1] - arr[0]
11+
geoInterval := arr[1] / arr[0]
12+
arithCount := 0
13+
geoCount := 0
14+
15+
for i := 0; i < len(arr)-1; i++ {
16+
if arr[i+1]-arr[i] == arithInterval {
17+
arithCount++
18+
}
19+
if arr[i+1]/arr[i] == geoInterval {
20+
geoCount++
21+
}
22+
}
23+
24+
if arithCount == len(arr)-1 {
25+
return "Arithmetic"
26+
}
27+
28+
if geoCount == len(arr)-1 {
29+
return "Geometric"
30+
}
31+
32+
return "-1"
33+
}
34+
35+
func main() {
36+
result1 := ArithGeo([]int{2, 4, 6, 8})
37+
fmt.Println(result1)
38+
result2 := ArithGeo([]int{2, 6, 18, 54})
39+
fmt.Println(result2)
40+
result3 := ArithGeo([]int{-3, -4, -5, -6, -7})
41+
fmt.Println(result3)
42+
result4 := ArithGeo([]int{3, 12, 48, 192, 768})
43+
fmt.Println(result4)
44+
}

0 commit comments

Comments
 (0)