Skip to content

Commit 14d92aa

Browse files
committed
small changes in max-sub-subarray
1 parent 1c9b65c commit 14d92aa

File tree

2 files changed

+68
-70
lines changed

2 files changed

+68
-70
lines changed

Max_Average_Subarray.js

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,42 @@ Elements of the given array will be in the range [-10,000, 10,000].
1616
*/
1717

1818
/* 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
2020
2121
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.
2222
2323
C) In the first loop is I am just generating the sum of the sub-array of the first 4 elements.
2424
2525
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.
2626
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]
2828
2929
That is its adding one element on the right and deducting one element on the left.
3030
*/
3131

3232
function findMaxAverage(nums, k) {
33+
let curr_max = 0
3334

34-
var curr_max = 0;
35+
for (let i = 0; i < k; i++) {
36+
curr_max += nums[i]
37+
}
3538

36-
for (let i = 0; i < k; i++) {
37-
curr_max += nums[i];
38-
}
39+
var max_so_far = curr_max
3940

40-
var max_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 (var j = k; j < nums.length; j++) {
46+
curr_max += nums[j] - nums[j - k]
4147

42-
for (var j = 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-
return max_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
49+
max_so_far = Math.max(curr_max, max_so_far)
50+
}
51+
return max_so_far / k
5052
}
5153

52-
// console.log(findMaxAverage([1, 12, -5, -6, 50, 3], 4));
53-
54+
// console.log(findMaxAverage([1, 12, -5, -6, 50, 3], 4)); // => 12.75
5455

5556
/* SOLUTION-2 - General Maximum subarray problem solved with Brute Force.
5657
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
6061
6162
*/
6263
function findMaxSubArrayBruteForce2(arr) {
64+
var max_so_far = Number.NEGATIVE_INFINITY
6365

64-
var max_so_far = Number.NEGATIVE_INFINITY;
66+
var leftIndex = 0,
67+
rightIndex = arr.length - 1,
68+
len = arr.length
6569

66-
var leftIndex = 0,
67-
rightIndex = arr.length - 1,
68-
len = arr.length;
70+
for (var i = 0; i < len; i++) {
71+
maxSum = 0
6972

70-
for (var i = 0; i < len; i++) {
71-
maxSum = 0;
73+
for (var j = i; j < len; j++) {
74+
maxSum += arr[j]
7275

73-
for (var j = 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+
}
7583

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
8385

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
85-
86-
return {
87-
left: leftIndex,
88-
right: rightIndex,
89-
final_max_sum_subArray: max_so_far
90-
};
86+
return {
87+
left: leftIndex,
88+
right: rightIndex,
89+
final_max_sum_subArray: max_so_far,
90+
}
9191
}
9292

93-
var array = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
93+
var array = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
9494

9595
// console.log(findMaxSubArrayBruteForce2(array));
9696

@@ -99,35 +99,33 @@ var array = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
9999
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
100100
*/
101101

102-
103102
function findMaxSubArrayBruteForce3(arr) {
104-
var max_so_far = Number.NEGATIVE_INFINITY;
105-
var leftIndex = 0,
106-
rightIndex = arr.length - 1,
107-
len = arr.length;
108-
109-
for (var i = 0; i < len; i++) {
110-
111-
for (var j = i; j < len; j++) {
112-
maxSum = 0;
113-
for (var k = i; k <= j; k++) {
114-
maxSum += arr[k];
115-
116-
if (max_so_far < maxSum) {
117-
leftIndex = i;
118-
max_so_far = maxSum;
119-
rightIndex = j;
120-
}
121-
}
122-
}
123-
}
124-
return {
125-
left: leftIndex,
126-
right: rightIndex,
127-
final_max_sum_subArray: max_so_far
128-
};
103+
var max_so_far = Number.NEGATIVE_INFINITY
104+
var leftIndex = 0,
105+
rightIndex = arr.length - 1,
106+
len = arr.length
107+
108+
for (var i = 0; i < len; i++) {
109+
for (var j = i; j < len; j++) {
110+
maxSum = 0
111+
for (var k = i; k <= j; k++) {
112+
maxSum += arr[k]
113+
114+
if (max_so_far < maxSum) {
115+
leftIndex = i
116+
max_so_far = maxSum
117+
rightIndex = j
118+
}
119+
}
120+
}
121+
}
122+
return {
123+
left: leftIndex,
124+
right: rightIndex,
125+
final_max_sum_subArray: max_so_far,
126+
}
129127
}
130128

131-
var array = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
129+
var array = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
132130

133-
console.log(findMaxSubArrayBruteForce3(array));
131+
console.log(findMaxSubArrayBruteForce3(array))

max-contiguous-subarray-general-Solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ let myArr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; // => 6
1919
console.log(maxContiguousSubArray(myArr));
2020

2121
/*Explanation
22-
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.
2323
2424
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.
2525
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.
2727
2828
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.
2929

0 commit comments

Comments
 (0)