44Given an integer array nums and an integer k,
55find the maximum average value of a subarray of length k.
66
7- Example:
7+ Example
8+ -------
89nums = [1,12,-5,-6,50,3]
910k = 4
1011
11- Subarrays of length 4:
12- [1,12,-5,-6] -> average = 0.5
13- [12,-5,-6,50] -> average = 12.75
14- [-5,-6,50,3] -> average = 10.5
12+ Subarrays of size 4:
13+
14+ [1,12,-5,-6] -> avg = 0.5
15+ [12,-5,-6,50] -> avg = 12.75
16+ [-5,-6,50,3] -> avg = 10.5
1517
1618Maximum average = 12.75
1719
18- Concept Used:
20+
21+ Concept Used
22+ ------------
1923Sliding Window Technique
2024
21- Instead of recalculating the sum of every subarray,
22- we maintain a moving window of size k.
25+ Instead of recalculating the sum for every subarray,
26+ we maintain a window of size k.
2327
2428Steps:
25291. Add the next element to the window
26- 2. Calculate average when window size reaches k
30+ 2. When window size == k → calculate result
27313. Remove the leftmost element
28324. Slide the window forward
2933
30- Time Complexity: O(n)
31- Space Complexity: O(1)
34+ Time Complexity : O(n)
35+ Space Complexity : O(1)
36+
37+
38+ Sliding Window Visualization
39+ ----------------------------
40+
41+ nums = [1,12,-5,-6,50,3]
42+ k = 4
43+
44+ Step 1
45+
46+ [1,12,-5,-6] 50 3
47+ avg = 0.5
48+
49+
50+ Step 2
51+
52+ 1 [12,-5,-6,50] 3
53+ avg = 12.75
54+
55+
56+ Step 3
57+
58+ 1 12 [-5,-6,50,3]
59+ avg = 10.5
60+
61+
62+ Maximum = 12.75
3263"""
3364
3465
3566class Solution :
3667
37- # ------------------------------------------------------------
38- # Approach 1 : Sliding Window using FOR loop
39- # ------------------------------------------------------------
68+ # ---------------------------------------------------------
69+ # APPROACH 1 : Sliding Window using FOR loop
70+ # ---------------------------------------------------------
4071 def findMaxAverage_for_loop (self , nums , k ):
4172 """
42- This method uses a for loop with two pointers.
43- r -> right pointer (expands window)
44- l -> left pointer (shrinks window)
73+ Uses a for loop with two pointers.
74+
75+ l -> left pointer
76+ r -> right pointer
77+
78+ r expands the window
79+ l shrinks the window
4580 """
4681
47- l = 0 # left pointer
48- window_sum = 0 # stores current window sum
49- max_avg = float ('-inf' ) # stores maximum average found
82+ l = 0
83+ window_sum = 0
84+ max_avg = float ('-inf' )
5085
51- # r represents the right pointer of the sliding window
86+ # r moves through the array
5287 for r in range (len (nums )):
5388
54- # Step 1: Add the new element to the window
89+ # Add new element entering the window
5590 window_sum += nums [r ]
5691
57- # Step 2: Check if window size becomes k
58- if ( r - l + 1 ) == k :
92+ # Check if window size reached k
93+ if r - l + 1 == k :
5994
60- # Step 3: Compute average of current window
95+ # Calculate average
6196 current_avg = window_sum / k
6297
6398 # Update maximum average
6499 max_avg = max (max_avg , current_avg )
65100
66- # Step 4: Remove the leftmost element
101+ # Remove element leaving the window
67102 window_sum -= nums [l ]
68103
69- # Step 5: Move the left pointer forward
104+ # Move left pointer forward
70105 l += 1
71106
72107 return max_avg
73108
74- # ------------------------------------------------------------
75- # Approach 2 : Sliding Window using WHILE loop
76- # ------------------------------------------------------------
109+ # ---------------------------------------------------------
110+ # APPROACH 2 : Sliding Window using WHILE loop
111+ # ---------------------------------------------------------
77112 def findMaxAverage_while_loop (self , nums , k ):
78113 """
79- This method uses a while loop instead of a for loop.
80- The logic remains exactly the same.
114+ Same sliding window logic but implemented with a while loop.
81115 """
82116
83- l = 0 # left pointer
84- r = 0 # right pointer
85- window_sum = 0 # sum of elements inside window
86- max_avg = float ('-inf' ) # track maximum average
117+ l = 0
118+ r = 0
119+ window_sum = 0
120+ max_avg = float ('-inf' )
87121
88- # Continue until right pointer reaches the end
122+ # Continue until r reaches end of array
89123 while r < len (nums ):
90124
91- # Step 1: Add element at right pointer
125+ # Add element at right pointer
92126 window_sum += nums [r ]
93127
94- # Step 2: Check if window size becomes k
95- if ( r - l + 1 ) == k :
128+ # If window size becomes k
129+ if r - l + 1 == k :
96130
97- # Calculate current average
131+ # Calculate average
98132 current_avg = window_sum / k
99133
100- # Update maximum average
134+ # Update max average
101135 max_avg = max (max_avg , current_avg )
102136
103- # Step 3: Remove the leftmost element
137+ # Remove element leaving window
104138 window_sum -= nums [l ]
105139
106- # Step 4: Move left pointer forward
140+ # Move left pointer
107141 l += 1
108142
109- # Step 5: Move right pointer forward
143+ # Move right pointer
110144 r += 1
111145
112146 return max_avg
113147
114148
115- # ------------------------------------------------------------
116- # Example Test (for understanding / local testing )
117- # ------------------------------------------------------------
149+ # ---------------------------------------------------------
150+ # Example Test (for learning and debugging )
151+ # ---------------------------------------------------------
118152if __name__ == "__main__" :
119153
120154 nums = [1 , 12 , - 5 , - 6 , 50 , 3 ]
121155 k = 4
122156
123157 sol = Solution ()
124158
125- print ("Using FOR loop :" , sol .findMaxAverage_for_loop (nums , k ))
126- print ("Using WHILE loop :" , sol .findMaxAverage_while_loop (nums , k ))
159+ print ("FOR LOOP RESULT :" , sol .findMaxAverage_for_loop (nums , k ))
160+ print ("WHILE LOOP RESULT :" , sol .findMaxAverage_while_loop (nums , k ))
0 commit comments