diff --git a/go/maximum-beauty-of-an-array-after-applying-operation.go b/go/maximum-beauty-of-an-array-after-applying-operation.go new file mode 100644 index 0000000..d9fde18 --- /dev/null +++ b/go/maximum-beauty-of-an-array-after-applying-operation.go @@ -0,0 +1,25 @@ +package main + +import "slices" + +func maximumBeauty(nums []int, k int) int { + if len(nums) == 1 { + return 1 + } + + maxValue := slices.Max(nums) + + count := make([]int, maxValue+1) + for _, n := range nums { + count[max(0, n-k)]++ + count[min(n+k+1, maxValue)]-- + } + + maxBeauty, runningSum := 0, 0 + for _, x := range count { + runningSum += x + maxBeauty = max(maxBeauty, runningSum) + } + + return maxBeauty +}