Skip to content

Latest commit

 

History

History
41 lines (35 loc) · 1.72 KB

File metadata and controls

41 lines (35 loc) · 1.72 KB

Intuition

  1. Max Bitwise AND: The maximum bitwise AND will always involve the largest number in the array (or numbers equal to it), since any bitwise AND operation with a smaller number will result in a smaller or equal value.
  2. Longest Subarray: Once we find the maximum element in the array, we need to find the longest contiguous subarray consisting only of this element to ensure the bitwise AND remains maximum.

Approach

  1. Traverse the array and keep track of the maximum element seen so far (currMax).
  2. If we find an element larger than currMax, update currMax, reset the size of the current subarray to 1, and reset the result.
  3. If we find another element equal to currMax, increase the size of the subarray and update the result to reflect the longest subarray of the maximum element.
  4. If the element is smaller than currMax, reset the size to 0 since the subarray is broken.
  5. Return the result at the end of the traversal.

Complexity

  • Time complexity: The solution involves a single pass over the array, so the time complexity is O(n) where n is the number of elements in the input array.

  • Space complexity: The space complexity is O(1) because only a few integer variables are used for tracking state.

Code

class Solution {
    public int longestSubarray(int[] nums) {
        int currMax = 0;
        int res = 0;
        int size = 0;

        for (int num : nums) {
            if (num > currMax) {
                currMax = num;
                size = 1;
                res = 1;  
            } else if (num == currMax) {
                size++;
                res = Math.max(res, size);
            } else {
                size = 0;
            }
        }

        return res;
    }
}