You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Max_Average_Subarray.js
+66-68Lines changed: 66 additions & 68 deletions
Original file line number
Diff line number
Diff line change
@@ -16,41 +16,42 @@ Elements of the given array will be in the range [-10,000, 10,000].
16
16
*/
17
17
18
18
/* SOLUTION-1
19
-
A) If nums is number of elements in an array and k is each window size, then the number of windows possible is = N-W+1 . So in my below example that would be 6 - 4 + 1 i.e. 3
19
+
A) If N is number of elements in an array and k is each window size, then the number of windows possible is = N-k+1 . So in my below example that would be 6 - 4 + 1 i.e. 3
20
20
21
21
B) Kadane's Algo - Basically I have to look for all contiguous sub-arrays of size 4, and also keep track of the maximum sum contiguous sub-array until the end. Whenever I find a new contiguous sub-array, I check if the current sum is greater than the max_sum so far and updates it accordingly.
22
22
23
23
C) In the first loop is I am just generating the sum of the sub-array of the first 4 elements.
24
24
25
25
D) In the second loop, I am traversing a sliding window - at each iteration, I am deducting the first element from left and adding next element to the right. And after doing this, updating the max_so_far to its highest value, by comparing it to its previous highest value.
26
26
27
-
So for first loop of < curr_max += (nums[j] - nums[j - k]); > will do curr_max += nums[j] - muns[0]
27
+
So for first loop of the second for-loop in the below function -> curr_max += (nums[j] - nums[j - k]); -> will do curr_max += nums[j] - muns[0]
28
28
29
29
That is its adding one element on the right and deducting one element on the left.
30
30
*/
31
31
32
32
functionfindMaxAverage(nums,k){
33
+
letcurr_max=0
33
34
34
-
varcurr_max=0;
35
+
for(leti=0;i<k;i++){
36
+
curr_max+=nums[i]
37
+
}
35
38
36
-
for(leti=0;i<k;i++){
37
-
curr_max+=nums[i];
38
-
}
39
+
varmax_so_far=curr_max
39
40
40
-
varmax_so_far=curr_max;
41
+
// Now add one element to the front of the 'max_so_far' and delete one element from the back of 'max_so_far'
42
+
// For example if nums.length is 5 and my k is 3 then the first time 'max_so_far' is calculated it will be the
43
+
// first 3 items, then I have to add the 4-th item i.e. num[3] and delete the first item which will be
44
+
// nums[j - k] i.e. num[3 - 3]
45
+
for(varj=k;j<nums.length;j++){
46
+
curr_max+=nums[j]-nums[j-k]
41
47
42
-
for(varj=k;j<nums.length;j++){
43
-
44
-
curr_max+=(nums[j]-nums[j-k]);
45
-
46
-
// Each time we get a new curr_sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
47
-
max_so_far=Math.max(curr_max,max_so_far);
48
-
}
49
-
returnmax_so_far/k;
48
+
// Each time we get a new curr_sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
/* SOLUTION-2 - General Maximum subarray problem solved with Brute Force.
56
57
Find the contiguous subarray within a one-dimensional array of numbers which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.
@@ -60,37 +61,36 @@ In the below solutions we start at all positions of the array and calculate runn
60
61
61
62
*/
62
63
functionfindMaxSubArrayBruteForce2(arr){
64
+
varmax_so_far=Number.NEGATIVE_INFINITY
63
65
64
-
varmax_so_far=Number.NEGATIVE_INFINITY;
66
+
varleftIndex=0,
67
+
rightIndex=arr.length-1,
68
+
len=arr.length
65
69
66
-
varleftIndex=0,
67
-
rightIndex=arr.length-1,
68
-
len=arr.length;
70
+
for(vari=0;i<len;i++){
71
+
maxSum=0
69
72
70
-
for(vari=0;i<len;i++){
71
-
maxSum=0;
73
+
for(varj=i;j<len;j++){
74
+
maxSum+=arr[j]
72
75
73
-
for(varj=i;j<len;j++){
74
-
maxSum+=arr[j];
76
+
if(max_so_far<maxSum){
77
+
leftIndex=i
78
+
max_so_far=maxSum
79
+
rightIndex=j
80
+
}
81
+
}
82
+
}
75
83
76
-
if(max_so_far<maxSum){
77
-
leftIndex=i;
78
-
max_so_far=maxSum;
79
-
rightIndex=j;
80
-
}
81
-
}
82
-
}
84
+
// Inside the above, loop, the setting of leftIndex & rightIndex value is only for logging it in the final output. It has no impact on the final calculation of the maximum-sum-subarray
83
85
84
-
// Inside the above, loop, the setting of leftIndex & rightIndex value is only for logging it in the final output. It has no impact on the final calculation of the maximum-sum-subarray
The difference between the O(N^2) and O(N^3) functions is that in the O(N^2) function, the sum is computed implicitly every time the end index is incremented, while in the O(N^3) function, the sum is computed with a third explicit loop between start and end
A> currentMax = Math.max(currentMax+arr[i], arr[i]) => This line effectively implements the requirement that the sub-array should be contguous.
22
+
A> currentMax = Math.max(currentMax+arr[i], arr[i]) => This line effectively implements the requirement that the sub-array should be contiguous.
23
23
24
24
It adds the current index elements with the positive summation of previous contiguous elemtents. So, it will sometime become negative if the current index no is a large negative no.
25
25
26
-
And at any index, if this current index no is so large a positive no, that arr[i] > (currentMax + arr[i]) then effective, the caluculation of Sum is effectively reset from this position - which is what I want.
26
+
And at any index, if this current index no is so large a positive no, that arr[i] > (currentMax + arr[i]) then effective, the calculation of Sum is effectively reset from this position - which is what I want.
27
27
28
28
B> So, in the above test case, when i hits 3 where the element is 4 - currentMax becomes 4, i.e. sum calculation is freshly started from here for the next set of nos.
0 commit comments