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
Longest subarray with unit difference in JavaScript
We need to find the length of the longest subarray where the difference between maximum and minimum values is exactly 1. This means the subarray can only contain two consecutive numbers (like 3 and 4) or just one unique number repeated.
Problem Statement
Given an array of numbers, find the length of the longest subarray where the difference between its maximum and minimum values is exactly 1.
For example, with input array:
const arr = [2, 4, 3, 3, 6, 3, 4, 8];
The longest valid subarray is [4, 3, 3, 3, 4] with length 5.
Approach: Frequency Count Method
We can solve this efficiently by counting frequencies of each number, then checking consecutive number pairs.
const arr = [2, 4, 3, 3, 6, 3, 4, 8];
const longestSequence = (arr = []) => {
// Count frequency of each number
const map = arr.reduce((acc, num) => {
acc[num] = (acc[num] || 0) + 1;
return acc;
}, {});
// Check each number and its consecutive number
return Object.keys(map).reduce((max, key) => {
const nextKey = parseInt(key, 10) + 1;
if (map[nextKey] >= 0) {
return Math.max(
max,
map[key] + map[nextKey]
);
}
return max;
}, 0);
};
console.log(longestSequence(arr));
5
How It Works
The algorithm works in two phases:
- Frequency Counting: Create a map counting occurrences of each number
- Consecutive Pair Check: For each number, check if its consecutive number (n+1) exists and sum their frequencies
For our example [2, 4, 3, 3, 6, 3, 4, 8], the frequency map becomes:
const exampleArr = [2, 4, 3, 3, 6, 3, 4, 8];
const frequencyMap = {};
exampleArr.forEach(num => {
frequencyMap[num] = (frequencyMap[num] || 0) + 1;
});
console.log("Frequency map:", frequencyMap);
// Check consecutive pairs
console.log("3 + 4:", frequencyMap[3] + (frequencyMap[4] || 0));
console.log("2 + 3:", frequencyMap[2] + (frequencyMap[3] || 0));
Frequency map: { '2': 1, '3': 3, '4': 2, '6': 1, '8': 1 }
3 + 4: 5
2 + 3: 4
Alternative: Sliding Window Approach
Here's another approach using a sliding window technique:
const slidingWindowApproach = (arr) => {
let maxLength = 0;
for (let i = 0; i < arr.length; i++) {
let min = arr[i];
let max = arr[i];
for (let j = i; j < arr.length; j++) {
min = Math.min(min, arr[j]);
max = Math.max(max, arr[j]);
if (max - min === 1) {
maxLength = Math.max(maxLength, j - i + 1);
} else if (max - min > 1) {
break; // No point continuing this subarray
}
}
}
return maxLength;
};
const testArr = [2, 4, 3, 3, 6, 3, 4, 8];
console.log(slidingWindowApproach(testArr));
5
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Frequency Count | O(n) | O(n) | Large arrays with many duplicates |
| Sliding Window | O(n²) | O(1) | Small arrays or memory-constrained scenarios |
Conclusion
The frequency count method is more efficient for this problem, achieving O(n) time complexity. It works by counting occurrences of each number and summing frequencies of consecutive number pairs to find the maximum subarray length.
