Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

42. Maximum Average Subarray I

EasyDay 13 of 60Sliding Window

Given an integer array nums and an integer k, find the contiguous subarray of length exactly k with the largest average, and return that average. Answers within 10^-5 of the true value are accepted.

Example 1
Input: nums = [1,12,-5,-6,50,3], k = 4
Output: 12.75
The window [12,-5,-6,50] sums to 51, and 51 / 4 = 12.75.
Example 2
Input: nums = [5], k = 1
Output: 5.0
Constraints
  • 1 <= k <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
Hints
Frequently asked at: Google, Amazon, Zoho
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min