Longest Subarray With Maximum Bitwise AND - Maximum Bitwise AND = Maximum Number [JS]

Description 

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;
};

javascript

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]