-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax-sum-arr.js
More file actions
38 lines (29 loc) · 952 Bytes
/
max-sum-arr.js
File metadata and controls
38 lines (29 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// // this solution uses a method called sliding window pattern
// // where we loop through the arr once and then add the next value to
// // the current temporaly sum without also forgetting to remove the first index at 0.
// // so that arrangement is what known as sliding window pattern, with a linear time complexity.
// function maxSumArr(arr, n){
// let maxSum = 0;
// let tempSum = 0;
// for(let i = 0; i < n; i++){
// maxSum += arr[i];
// }
// tempSum = maxSum;
// for(let i = n; i < arr.length; i++){
// tempSum += arr[i] - arr[i - n];
// maxSum = Math.max(tempSum, maxSum);
// }
// return (arr.length < n) ? null : maxSum;
// }
// console.log(maxSumArr([2,6,9,2,1,8,5,6,3], 3))
function countUnique(arr){
let i = 0;
let count = 0;
while(i < arr.length){
if(arr[i] !== arr[i+1]) count++;
i++;
}
return count;
}
console.log(countUnique([-2,-1,-1,0,1]));
console.log(countUnique([]))