-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.go
More file actions
192 lines (167 loc) · 3.06 KB
/
sample.go
File metadata and controls
192 lines (167 loc) · 3.06 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
var scanner *bufio.Scanner
func initScanner() {
scanner = bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
}
func readString() string {
if scanner == nil {
initScanner()
}
if scanner.Scan() {
return scanner.Text()
}
return ""
}
func readInt() int {
n, _ := strconv.Atoi(readString())
return n
}
func readInts(n int) []int {
arr := make([]int, n)
for i := 0; i < n; i++ {
arr[i] = readInt()
}
return arr
}
func readIntsUnknown() []int {
var arr []int
arr = make([]int, 0)
for {
num := readInt()
if num == -1 {
break
}
arr = append(arr, num)
}
return arr
}
func findMin(arr []int) int {
if len(arr) == 0 {
return 0
}
min := arr[0]
for i := 1; i < len(arr); i++ {
if arr[i] < min {
min = arr[i]
}
}
return min
}
func findMax(arr []int) int {
if len(arr) == 0 {
return 0
}
max := arr[0]
for i := 1; i < len(arr); i++ {
if arr[i] > max {
max = arr[i]
}
}
return max
}
type minheap struct {
heap []int
}
func NewMinHeap() *minheap {
return &minheap{
heap: make([]int, 0),
}
}
func (h *minheap) Insert(n int) {
h.heap = append(h.heap, n)
h.heapifyUp(len(h.heap) - 1)
}
func (h *minheap) heapifyUp(index int) {
parentIdx := (index - 1) / 2
for index > 0 && h.heap[parentIdx] > h.heap[index] {
h.heap[parentIdx], h.heap[index] = h.heap[index], h.heap[parentIdx]
index = parentIdx
parentIdx = (index - 1) / 2
}
}
func (h *minheap) heapifyDown(index int) {
for {
left := 2*index + 1
right := 2*index + 2
small := index
if left < len(h.heap) && h.heap[left] < h.heap[small] {
small = left
}
if right < len(h.heap) && h.heap[right] < h.heap[small] {
small = right
}
if small == index {
break
}
h.heap[small], h.heap[index] = h.heap[index], h.heap[small]
index = small
}
}
func (h *minheap) ExtractMin() int {
min := h.heap[0]
index := len(h.heap) - 1
h.heap[0] = h.heap[index]
h.heap = h.heap[:index]
h.heapifyDown(0)
return min
}
func (h *minheap) isEmpty() bool {
if len(h.heap) == 0 {
return true
}
return false
}
func (h *minheap) peak() int {
return h.heap[0]
}
func cost(arr []int) int {
return arr[len(arr)-1] - arr[0]
}
func solve(arr []int, n int, k int) int {
heap := NewMinHeap()
for i := 0; i < n; i++ {
heap.Insert(arr[i])
}
//minEle := findMin(arr)
maxEle := findMax(arr)
ans := -1
for !heap.isEmpty() {
temp := make([]int, 0)
for i := 0; i < k && !heap.isEmpty(); i++ {
temp = append(temp, heap.ExtractMin())
}
temmin := temp[0]
for !heap.isEmpty() && heap.peak()-temmin < maxEle-heap.peak() {
temp = append(temp, heap.ExtractMin())
}
ans = max(ans, cost(temp))
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
testCases := readInt()
for t := 0; t < testCases; t++ {
n := readInt()
arr := readInts(n)
maxele := readInt()
fmt.Println(solve(arr, n, maxele))
// my logic goes herefor each test case goes here
// For example:
// answer := yourFunction(arr)
// Output the answer for each test case
// fmt.Println(answer)
}
}