-
Notifications
You must be signed in to change notification settings - Fork 1
/
testaa.js
45 lines (45 loc) · 1.09 KB
/
testaa.js
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
39
40
41
42
43
44
45
let n = 5;
let m = 4;
let array = [10,1,3,5,9];
/* for(let i = 0; i < n; i++){
let temp = readInt();
array.push(temp);
} */
let i = 0, j = 0;
let maxlen = 0;
let result = 1, max = array[i], min = array[i];
function searchMax(i, j){
let tempMax = array[i];
for(let index = i; index<=j; index++){
if(array[index] > tempMax) tempMax = array[index];
}
return tempMax;
}
function searchMin(i, j){
let tempMin = array[i];
for(let index = i; index<=j; index++){
if(array[index] < tempMin) tempMin = array[index];
}
return tempMin
}
while(j<n){
console.log(max,min,i, j, result);
if(array[j] > max && (array[j] - min) <=m){
max = array[j];
j++;
}else if (array[j] < min && (max - array[j]) <= m){
min = array[j];
j++;
}else if ((array[j] > max && (array[j] - min) > m )|| (array[j] < min && (max - array[j]) > m)){
i++;
min = searchMin(i,j);
max = searchMax(i,j);
continue;
}else{
j++;
}
if(result < j - i + 1){
result = j-i;
}
}
console.log(result);