643. Maximum Average Subarray I Here’s a cleanly formatted version of the problem: ---…
- User
- Maximum Average Subarray I
Here’s a cleanly formatted version of the problem:
643. Maximum Average Subarray I (Easy)
Problem Description:
You are given an integer array
numsconsisting ofnelements and an integerk.Find a contiguous subarray whose length is equal to
kthat has the maximum average value and return this value. Any answer with a calculation error less than10^-5will be accepted.
Example 1:
Input:
nums = [1, 12, -5, -6, 50, 3]
k = 4Output:
12.75000Explanation:
Maximum average is calculated as:
(12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
Example 2:
Input:
nums = [5]
k = 1Output:
5.00000
Q- can you explain the exercise using your own words ?
—> so we are given a list of values of type int and k as that target length
Our task to find out the a “contigous” , “subarray” of length equal to k
Where it avg values if the biggest oky
Q- what is the input/output format ?
Input : List[int] Output : int
Q- can you go through an example ?
For example we have this list
[1,12,-5,-6,50,3] , k = 4
Q- one question you can ask given this example what are all the sub-contigous-array of length = k
The answer is like this Here are all the contiguous subarrays of length
k=4for the input arraynums = [1, 12, -5, -6, 50, 3]:[1, 12, -5, -6][12, -5, -6, 50][-5, -6, 50, 3]
There are only three such subarrays because the length of the original array is 6, and with a subarray length of 4, there are only
6 - 4 + 1 = 3possible starting positions for the subarrays.Q- next we have all the target values we need just for eeach one to compute the avg values for it
It can look like this
“[1, 12, -5, -6]: (1 + 12 + (-5) + (-6)) / 4 = 2 / 4 = 0.5 [12, -5, -6, 50]: (12 + (-5) + (-6) + 50) / 4 = 51 / 4 = 12.75 [-5, -6, 50, 3]: (-5 + (-6) + 50 + 3) / 4 = 42 / 4 = 10.5 So, the average values of the subarrays are 0.5, 12.75, and 10.5, respectively. “
- Assistant