Longest Subarray With Maximum Bitwise AND - Maximum Bitwise AND = Maximum Number [JS]
Solution: Longest Consecutive Max Number
Solution: Longest Consecutive Max Number
The maximum bitwise AND is the maximum number in the array.
Find the length of the longest subarray consisting of the max number.
Time Complexity: O(n)
Space Complexity: O(1)
var longestSubarray = function(nums) {
let n = nums.length, max = Math.max(...nums);
let len = 0, maxLen = 0;
for (let i = 0; i < n; i++) {
if (nums[i] === max) len++;
else len = 0;
maxLen = Math.max(maxLen, len);
}
return maxLen;
};
Comments
Post a Comment