aboutsummaryrefslogtreecommitdiff
path: root/longest_subarr_max_bitwise_and.py
blob: 95d3a361d0872ae635f593d0749f195add8adcf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 2419. Longest Subarray With Maximum Bitwise AND
from itertools import groupby

"""
you are given an integer array 'nums' of size 'n'. consider a non-empty
subarray from 'nums' that has the maximum possible bitwise and. in other
words, let 'k' be the maximum value of the bitwise and of any subarray of
'nums'. then, only subarrays with a bitwise and equal to 'k' should be
considered. return the length of the longest such subarray. the bitwise and
of an array is the bitwise and of all the numbers in it. a subarray is a
contiguous sequence of elements within an array
"""


class Solution(object):
    def longestSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return max(len(list(i)) for n, i in groupby(nums) if n == max(nums))


if __name__ == "__main__":
    obj = Solution()
    print(obj.longestSubarray(nums=[1, 2, 3, 3, 2, 2]))
    print(obj.longestSubarray(nums=[1, 2, 3, 4]))