Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find Max Slice Of Array | JavaScript
In JavaScript, finding the maximum slice of an array with at most two different numbers is a common problem that can be efficiently solved using the sliding window technique. This approach maintains a dynamic window that expands and contracts to find the longest contiguous subarray containing no more than two distinct elements.
How It Works
The sliding window algorithm uses two pointers (start and end) to define a window boundary. We track distinct elements using a map and adjust the window size when the distinct count exceeds two.
Example
const arr = [1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8, 1, 1, 1, 1, 8, 1,
1, 8, 8];
const map = {
length: 0
};
let required = [];
for(start = 0, end = 0; end <= arr.length; ){
if(map.length > 2){
if(map[arr[start]] === 1){
delete map[arr[start]];
map.length--;
}else{
map[arr[start]]--;
};
start++;
}else{
if(end - start > required.length){
required = arr.slice(start, end);
};
if(map[arr[end]]){
map[arr[end]]++;
}else{
map[arr[end]] = 1;
map.length++;
}
end++;
}
}
console.log(required);
[
1, 8, 1, 1, 1,
1, 8, 1, 1, 8,
8
]
Algorithm Breakdown
The algorithm maintains a map to store the count of distinct characters within the current window. When the distinct count exceeds 2, it slides the window rightward by incrementing the start pointer. At each valid window, it compares the current subarray length with the previously stored maximum slice.
Alternative Implementation
Here's a cleaner version with better variable names and structure:
function findMaxSlice(arr) {
let maxSlice = [];
let left = 0;
let elementCount = new Map();
for (let right = 0; right < arr.length; right++) {
// Add current element to window
elementCount.set(arr[right], (elementCount.get(arr[right]) || 0) + 1);
// Shrink window if more than 2 distinct elements
while (elementCount.size > 2) {
elementCount.set(arr[left], elementCount.get(arr[left]) - 1);
if (elementCount.get(arr[left]) === 0) {
elementCount.delete(arr[left]);
}
left++;
}
// Update max slice if current window is larger
if (right - left + 1 > maxSlice.length) {
maxSlice = arr.slice(left, right + 1);
}
}
return maxSlice;
}
const testArray = [1, 1, 2, 2, 3, 1, 1, 1, 2, 2];
console.log(findMaxSlice(testArray));
[ 1, 1, 2, 2 ]
Key Points
The sliding window technique is optimal for this problem with O(n) time complexity. The algorithm expands the window by moving the right pointer and contracts by moving the left pointer when constraints are violated. Using a Map provides cleaner code compared to custom object manipulation.
Conclusion
The sliding window algorithm efficiently finds the maximum slice containing at most two distinct elements. This technique is particularly useful for subarray problems with constraints and maintains optimal performance.
