-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.go
45 lines (39 loc) · 1.06 KB
/
solution.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
40
41
42
43
44
45
func resultsArray(nums []int, k int) []int {
n := len(nums)
result := []int{}
for i := 0; i <= n-k; i++ {
subarray := make([]int, k)
copy(subarray, nums[i:i+k])
// Sort the subarray
sortedSubarray := make([]int, k)
copy(sortedSubarray, subarray)
sort.Ints(sortedSubarray)
// Check if elements are consecutive
isConsecutive := true
for j := 1; j < k; j++ {
if sortedSubarray[j]-sortedSubarray[j-1] != 1 {
isConsecutive = false
break
}
}
// Add the result based on conditions
if isConsecutive && equal(subarray, sortedSubarray) {
result = append(result, sortedSubarray[k-1]) // Max element
} else {
result = append(result, -1)
}
}
return result
}
// Helper function to compare slices
func equal(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}