Examples :
Input :
arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
k = 3
Output :
3 3 4 5 5 5 6
package main
import "fmt"
var n int
func main() {
k := 3
s := []int{1, 2, 3, 1, 4, 5, 2, 3, 6}
n = k
slidingWindowMaximum(s, 0, k)
}
func slidingWindowMaximum(arr []int, i int, k int) {
//Check Array size greater then k or not !
if len(arr) < k {
return
}
// Find Max Value in Sub Arrays
max := 0
for ; i < k; i++ {
//Find Max Value
max = findmax(max, arr[i])
}
//Print Max Sub Arrays Element
fmt.Print(max, " ")
//Find in next Sub Arrays in Max Call function
slidingWindowMaximum(arr, i-n+1, k+1)
}
func findmax(a int, b int) int {
if a < b {
return b
} else {
return a
}
}
Comments
Post a Comment