| Bytes | Lang | Time | Link |
|---|---|---|---|
| 102 | Rust | 250608T182715Z | corvus_1 |
| 077 | Haskell | 250601T220705Z | Ibozz91 |
| nan | Go | 250531T025659Z | Ahamad |
| 024 | Uiua 0.17.0dev.1 | 250531T205000Z | chunes |
| 033 | APL+WIN | 250530T093806Z | Graham |
| 062 | Ruby | 250530T055404Z | G B |
| 024 | APLDyalog Unicode | 250531T110344Z | Mat_rdv |
| 085 | JavaScript ES6 | 250530T143023Z | Arnauld |
| 123 | Google Sheets | 250530T044906Z | z.. |
| 007 | Jelly | 250530T192050Z | Jonathan |
| 077 | Maple | 250530T191944Z | dharr |
| 025 | Charcoal | 250530T160113Z | Neil |
| 085 | Desmos | 250530T120551Z | Aiden Ch |
| 011 | Vyxal 3 l | 250530T084826Z | lyxal |
| 010 | 05AB1E | 250530T081823Z | Kevin Cr |
| 068 | Python 2 | 250530T073349Z | tata |
Rust, 102 bytes
|a,k|(k..=a.len()).flat_map(|l|a.windows(l).map(move|x|x.iter().sum::<i64>().div_floor(l
as _))).max()
Haskell, 98 96 77 bytes
k!a=maximum[(`div`x).sum.take x.drop y$a|x<-[k..length a],y<-[0..length a-x]]
This is my first time golfing, so there might be more to save.
Edit: Saved more by using the list monad
Go, 213 188 178 bytes
Saved (25+10=35) bytes thanks to @ceilingcat
Golfed version. Attempt This Online!
import"slices"
func f(l[]int,k int)int{if len(l)<k{return 0}
s:=[]int{}
for i:=range-^len(l)-k{t:=0
for j:=range k{t+=l[i+j]}
s=append(s,t)}
return max(slices.Max(s)/k,f(l,k+1))}
Ungolfed version. Attempt This Online!
package main
import "fmt"
// This function calculates the maximum average of consecutive elements in an array using integer division
func f(list []int, windowSize int) int {
// Base case: if the window size is larger than the list, return 0
if len(list) < windowSize {
return 0
}
// Calculate the sum of each consecutive window of elements
var sumsOfWindows []int
for i := 0; i <= len(list)-windowSize; i++ {
sum := 0
for j := 0; j < windowSize; j++ {
sum += list[i+j]
}
sumsOfWindows = append(sumsOfWindows, sum)
}
// Find the maximum sum and calculate the average (integer division)
maxSum := max(sumsOfWindows)
currentMaxAverage := maxSum / windowSize
// Recursively find the max average with a larger window size
nextWindowMaxAverage := f(list, windowSize+1)
// Return the larger of the two averages
if currentMaxAverage > nextWindowMaxAverage {
return currentMaxAverage
}
return nextWindowMaxAverage
}
// Helper function to find maximum in a slice of integers
func max(slice []int) int {
if len(slice) == 0 {
return 0
}
maxVal := slice[0]
for _, v := range slice {
if v > maxVal {
maxVal = v
}
}
return maxVal
}
func main() {
fmt.Println(f([]int{7, 1, 6, 2, 8}, 2)) // Should output 7
fmt.Println(f([]int{7, 1, 7}, 2)) // Should output 4
}
Uiua 0.17.0-dev.1, 24 bytes SBCS
⌊/↥/◇⊂⍚⌟⧈(÷⊃⧻/+)⊂⊸⍜-⇡⊙⊸⧻
Explanation
⊙⊸⧻ # place length of arr beneath k
⊂⊸⍜-⇡ # inclusive range from k to len(arr)
⍚⌟ # map over range, fixing input arr and boxing output
⧈(÷⊃⧻/+) # windows by mean
/◇⊂ # concat boxed arrays
/↥ # maximum
⌊ # floor
APL+WIN, 33 bytes
Prompts for k followed by vector of integers.
⌈/⌊(∊⌈/¨k+/¨⊂n)÷k←k+0,⍳(⍴n←⎕)-k←⎕
Try it online! Thanks to Dyalog Classic
Explanation
k←⎕ Prompt for k and assign to the variable
n←⎕ Prompt for n and assign to variable
⍴n Length of n
k←k+0,⍳ k becomes a vector of lengths k to ⍴n
k+/¨⊂n Sum successive elements of n by lengths k
∊⌈/¨ Find the maximum of each sum
(...)÷k Divide each sum by its length
⌈/⌊ Round down the resulting averages and find the maximum
Ruby, 62 bytes
->l,k{(k..l.size).map{|n|l.each_cons(n).map(&:sum).max/n}.max}
Thanks Value Ink for the fixes and for -2 bytes on the final version.
APL(Dyalog Unicode), 24 bytes SBCS
{⌈/∊(⍺↓0,⍳≢⍵)(⌊+/÷⊣)¨⊂⍵}
K is the left argument and an array is the right.
Explanation
⍵ the right argument (an array)
⊂ Enclose (to treat the entire array as one scalar)
¨ Each
( ) tacit function: X (e f g h) Y ←→ e (X f Y) g (X h Y)
⊣ Left argument
÷ Divide
/ (n-wise) Reduction
+
+/ n-wise sum
⌊ Round down
(⌊+/÷⊣) rounded down rolling average
( )
⍵
≢ Tally (length)
⍳ Index Generator (numbers from 1 to a right argument)
, Concatenate
0
↓ Drop
⍺ the left argument (K)
(⍺↓0,⍳≢⍵) numbers from K to the length of array
(⍺↓0,⍳≢⍵)(⌊+/÷⊣)¨⊂⍵ averages of all contiguous subarrays of length at least K, combined by the length
∊ Enlist (list all values in one vector)
/ Reduce
⌈ Maximum
{⌈/∊(⍺↓0,⍳≢⍵)(⌊+/÷⊣)¨⊂⍵} solution
JavaScript (ES6), 85 bytes
Expects (K)(array).
k=>g=([v,...a],n=0,s=0,i)=>q=!i/v?g(a,n+1,s+v)<g(a,n,s,n,v=q)?q:v:n<k?g:s/n-(s%n<0)|0
Commented
k => // outer function taking k
g = ( // g = inner recursive function taking:
[ v, // v = current value from the input array
...a // a[] = remaining values
], //
n = 0, // n = number of values in the subarray
s = 0, // s = sum of all values in the subarray
i // i = flag set when the subarray is closed
) => //
q = // save the result of this call in q
!i / v ? // if i is not set and v is defined:
g( // 1st recursive call where:
a, n + 1, s + v // v is included in the subarray
) //
< // (compare)
g( // 2nd recursive call where:
a, n, s, // v is not included in the subarray
n, // i is set if n is not 0
v = q // the result of the 1st call is saved in v
) ? // if v (1st call) is less than q (2nd call):
q // return q
: // else:
v // return v
: // else:
n < k ? // if n is less than the target k:
g // invalidate this result
: // else:
s / n // return s / n
- (s % n < 0) // -1 if this is a negative non-integer
| 0 // rounded toward 0 (*)
(*) This is a twisted and 2-byte shorter way of just doing Math.floor(s/n).
Google Sheets, 123 bytes
=SORT(MAX(MAP(A2:A,LAMBDA(a,MAP(SEQUENCE(1,COUNT(A2:A)-A1+1,A1),LAMBDA(i,IFERROR(INT(1/AVERAGE(N(OFFSET(a,,,i)))^-1))))))))
Expects K in A1 and the array from A2 onwards.
Jelly, 7 bytes
ẆṫƇÆmṀḞ
A dyadic Link that accepts the list of \$n\$ integers on the left and the minimal sublist length, \$k\$, on the right and yields the maximal floored mean of the sublists of at least length \$k\$.
How?
ẆṫƇÆmṀḞ - Link: list of integers, L; positive integer, K
Ẇ - all non-empty, contiguous sublists of L
Ƈ - keep those for which:
ṫ - tail from 1-index K (an empty list is falsey)
Æm - mean (vectorises)
Ṁ - maximum
Ḟ - floor (towards -inf)
Maple, 77 bytes
(A,K)->max(seq(seq(floor(add(A[i..i+k-1])/k),i=1..n-k+1),k=K..(n:=nops(A))));
Input is list A and positive integer K.
Charcoal, 25 bytes
I⌈E…·θLη÷⌈E…·ιLηΣ✂η⁻λιλ¹ι
Try it online! Link is to verbose version of code. Explanation:
E Map over
…· Inclusive range from
θ Input `K` to
η Input array
L Length
E Map over
…· Inclusive range from
ι Current value to
η Input array
L Length
η Input array
✂ ¹ Sliced from
λ Inner value
⁻ Subtract
ι Outer value
λ To inner value
Σ Take the sum
⌈ Take the maximum
÷ Integer divided by
ι Current value
⌈ Take the maximum
I Cast to string
Implicitly print
Desmos, 85 bytes
l=L.count
a=floor(L.total/l)
f(L,k)=\{l>k:[f(L[2...],k),f(L[1...l-1],k),a],[a]\}.\max
Port of tata's Python 2 answer so make sure to upvote that as well.
Vyxal 3 -l, 11 (literate) bytes
sublists filter<
length second-input >=
}
each: arithmetic-mean max-of
floor
Could be a lot shorter if it wasn't at least K :p
05AB1E, 10 bytes
ŒʒgI@}ÅAàï
Inputs in the order \$list,K\$.
Try it online or verify both test cases.
Explanation:
Œ # Get all sublists of the first (implicit) input-list
ʒ # Filter it by:
g # Get the length of the current sublist
I@ # Check that it's >= the second input-integer
}ÅA # After the filter: get the average of each remaining sublist
à # Pop and keep the maximum
ï # Floor it to an integer
# (which is output implicitly as result)
Python 2, 68 bytes
f=lambda a,k:max([sum(a)/len(a)]+(a[k:]and[f(a[1:],k),f(a[:-1],k)]))