Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Maximum Average Subarray I

Easy
sliding-windowarray

Given an integer array nums and an integer k, find the contiguous subarray of length exactly k with the maximum average, and return that average.

Input

  • Line 1: space-separated integers nums
  • Line 2: integer k

Output: the maximum average, printed with two decimal places (the template formats it for you).

Example 1
Input:
1 12 -5 -6 50 3
4
Output:
12.75

(12 - 5 - 6 + 50) / 4 = 12.75.

Example 2
Input:
5
1
Output:
5.00

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10^5
  • -10^4 ≤ nums[i] ≤ 10^4
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.