101 Logo
onenoughtone

Code Implementation

Turbulence Detector Implementation

Below is the implementation of the turbulence detector:

solution.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Find the length of the longest turbulent subarray.
* Dynamic Programming approach.
*
* @param {number[]} arr - The input array
* @return {number} - The length of the longest turbulent subarray
*/
function maxTurbulenceSize(arr) {
const n = arr.length;
if (n === 1) {
return 1;
}
// Initialize state variables
let inc = 1; // Length of turbulent subarray ending with increasing comparison
let dec = 1; // Length of turbulent subarray ending with decreasing comparison
let maxLength = 1;
for (let i = 1; i < n; i++) {
if (arr[i - 1] < arr[i]) {
// Current comparison is increasing
inc = dec + 1;
dec = 1;
} else if (arr[i - 1] > arr[i]) {
// Current comparison is decreasing
dec = inc + 1;
inc = 1;
} else {
// Elements are equal, reset both
inc = 1;
dec = 1;
}
maxLength = Math.max(maxLength, Math.max(inc, dec));
}
return maxLength;
}
/**
* Find the length of the longest turbulent subarray.
* Sliding Window approach.
*
* @param {number[]} arr - The input array
* @return {number} - The length of the longest turbulent subarray
*/
function maxTurbulenceSizeWindow(arr) {
const n = arr.length;
if (n === 1) {
return 1;
}
let anchor = 0;
let maxLength = 1;
let prevComp = 0; // Previous comparison result
for (let i = 1; i < n; i++) {
// Calculate current comparison result
let currComp = 0;
if (arr[i - 1] < arr[i]) {
currComp = 1;
} else if (arr[i - 1] > arr[i]) {
currComp = -1;
}
// Adjust the window
if (currComp === 0) {
// Elements are equal, reset the window
anchor = i;
} else if (i === 1 || currComp * prevComp !== -1) {
// First comparison or comparison sign doesn't flip, reset the window
anchor = i - 1;
}
maxLength = Math.max(maxLength, i - anchor + 1);
prevComp = currComp;
}
return maxLength;
}
/**
* Find the length of the longest turbulent subarray.
* Count Consecutive Flips approach.
*
* @param {number[]} arr - The input array
* @return {number} - The length of the longest turbulent subarray
*/
function maxTurbulenceSizeFlips(arr) {
const n = arr.length;
if (n === 1) {
return 1;
}
let currentLength = 1;
let maxLength = 1;
let prevComp = 0; // Previous comparison result
for (let i = 1; i < n; i++) {
// Calculate current comparison result
let currComp = 0;
if (arr[i - 1] < arr[i]) {
currComp = 1;
} else if (arr[i - 1] > arr[i]) {
currComp = -1;
}
if (currComp === 0) {
// Elements are equal, reset the counter
currentLength = 1;
} else if (prevComp * currComp >= 0) {
// Comparison sign doesn't flip, reset the counter but count the current pair
currentLength = 2;
} else {
// Comparison sign flips, extend the current turbulent subarray
currentLength++;
}
maxLength = Math.max(maxLength, currentLength);
prevComp = currComp;
}
return maxLength;
}
// Test cases
console.log(maxTurbulenceSize([9, 4, 2, 10, 7, 8, 8, 1, 9])); // 5
console.log(maxTurbulenceSize([4, 8, 12, 16])); // 2
console.log(maxTurbulenceSize([100])); // 1
console.log(maxTurbulenceSizeWindow([9, 4, 2, 10, 7, 8, 8, 1, 9])); // 5
console.log(maxTurbulenceSizeWindow([4, 8, 12, 16])); // 2
console.log(maxTurbulenceSizeWindow([100])); // 1
console.log(maxTurbulenceSizeFlips([9, 4, 2, 10, 7, 8, 8, 1, 9])); // 5
console.log(maxTurbulenceSizeFlips([4, 8, 12, 16])); // 2
console.log(maxTurbulenceSizeFlips([100])); // 1

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that we need to find the length of the longest subarray where the comparison sign (< or >) alternates between adjacent elements.
  2. Define State Variables: Define state variables to track the length of turbulent subarrays ending with increasing and decreasing comparisons.
  3. Iterate Through the Array: Iterate through the array, updating the state variables based on the comparison between adjacent elements.
  4. Handle Edge Cases: Handle edge cases such as equal elements and single-element arrays.
  5. Track Maximum Length: Keep track of the maximum length of a turbulent subarray encountered during the iteration.
ProblemSolutionCode
101 Logo
onenoughtone