diff options
author | Jack Sangdahl <jack@pngu.org> | 2024-09-13 18:23:35 -0600 |
---|---|---|
committer | Jack Sangdahl <jack@pngu.org> | 2024-09-13 18:23:35 -0600 |
commit | 7d6ce37fbb409bc5c6675eeabb58166abe463aef (patch) | |
tree | 607fc6f4fffec8688b8b070368cff3dd807c2b83 | |
parent | f6175e306d3fb120d8f83804f57467fca72c9baf (diff) | |
download | leetcode-7d6ce37fbb409bc5c6675eeabb58166abe463aef.tar.gz leetcode-7d6ce37fbb409bc5c6675eeabb58166abe463aef.zip |
2419. longest subarray with maximum bitwise and
-rw-r--r-- | longest_subarr_max_bitwise_and.c | 35 | ||||
-rw-r--r-- | longest_subarr_max_bitwise_and.py | 27 |
2 files changed, 62 insertions, 0 deletions
diff --git a/longest_subarr_max_bitwise_and.c b/longest_subarr_max_bitwise_and.c new file mode 100644 index 0000000..17b9155 --- /dev/null +++ b/longest_subarr_max_bitwise_and.c @@ -0,0 +1,35 @@ +// 2419. Longest Subarray With Maximum Bitwise AND +#include "leetcode.h" + +/* + * 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 + */ + +int longestSubarray(int *nums, int numsSize) { + int max = 0, ans = 0; + for (int i = 0, j = 0; i < numsSize; i++) { + if (nums[i] > max) { + max = nums[i]; + j = 1; + ans = 1; + } else if (nums[i] == max) + j++; + else + j = 0; + if (j > ans) + ans = j; + } + return ans; +} + +int main() { + int n1[] = {1, 2, 3, 3, 2, 2}, n2[] = {1, 2, 3, 4}; + printf("%d\n", longestSubarray(n1, ARRAY_SIZE(n1))); // expect: 2 + printf("%d\n", longestSubarray(n2, ARRAY_SIZE(n2))); // expect: 1 +} diff --git a/longest_subarr_max_bitwise_and.py b/longest_subarr_max_bitwise_and.py new file mode 100644 index 0000000..95d3a36 --- /dev/null +++ b/longest_subarr_max_bitwise_and.py @@ -0,0 +1,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])) |