Longest Subarray With Maximum Bitwise AND - Leetcode 2419 - Python

Поділитися
Вставка
  • Опубліковано 5 лис 2024

КОМЕНТАРІ • 22

  • @ankitdimri629
    @ankitdimri629 Місяць тому +15

    Focus on the maximum element. Find the longest contiguous subarray where all elements are equal to this maximum value.

  • @MikPosp
    @MikPosp Місяць тому +2

    Thanks for your video!
    Here are a couple of oneliners:
    def longestSubarray(self, a):
    return max((v,len([*p])) for v,p in groupby(a))[1]
    def longestSubarray(self, a):
    return max(accumulate([0]+a,lambda l,v,M=max(a):(l+1)*(v==M)))

  • @akashverma5756
    @akashverma5756 Місяць тому +6

    Surprisingly, Solution of this problem don't require use of bitwise and operator.

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 Місяць тому +2

    Hi Navdeep. Solved it on my own its a tricky leetcode question.
    My code below --
    class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
    N = len(nums)
    maxVal = max(nums)
    i = 0
    maxLen = 0
    while i < N:
    num = nums[i]
    if num == maxVal:
    count = 0
    while i < N and nums[i] == maxVal:
    count += 1
    i += 1
    maxLen = max(maxLen, count)
    i+= 1
    return maxLen

  • @cherryayethihan
    @cherryayethihan Місяць тому

    Thanks a ton, your videos help me a lot. I am trying to polish my coding skills.

  • @MP-ny3ep
    @MP-ny3ep Місяць тому

    Beautifully explained. Thank you !

  • @orepajic2625
    @orepajic2625 Місяць тому +4

    why is the old channel not active anymore?

  • @colec1494
    @colec1494 Місяць тому

    great explanation

  • @dmitrykarpenko2271
    @dmitrykarpenko2271 Місяць тому

    How about NEGATIVE numbers?
    -1 is all 1s in binary, so (-1) & n == n for every n. Do we need to use a proper sliding window then?

  • @nethpot
    @nethpot Місяць тому

    At 8:29, you say it is just finding the longest subarray with the maximum element, but what if the subarray was 3,3,3,7,3? The 3 ANDed with the 7 still produces a 3 so you wouldn't want to restart your window when you see the 7, right?

  • @ShinAkuma
    @ShinAkuma Місяць тому

    I was able to solve it in O(n) after reading the first hint.

  • @henokassalif8175
    @henokassalif8175 Місяць тому

    I think this question is just better phrased as “Find the maximum bitwise value with the longest streak”

  • @v0nnyboy
    @v0nnyboy Місяць тому +1

    All you need to do is to find the largest number.. and then , find the length of the longest subarray that has all values as the largest number !
    As , once you have the largest number , that is the maximum And value that can be achieved in that array and , the And maximum value can only stay maximum if it's anded to itself , hence you need to find the length of the longest subarray that has all values equal to the max value
    Code Below :
    def longestSubarray(self, nums: List[int]) -> int:
    max_ct=0
    ct = 0
    maxx = max(nums)
    for n in nums:
    if n == maxx:
    ct+=1
    max_ct=max(ct,max_ct)
    else:
    ct=0
    return max_ct

  • @phantomhawk489
    @phantomhawk489 Місяць тому +2

    why i cant visualize things like this on my own , like ive solved like in 700+problem but still 1720 rated never solved hard question in contest is there a way i can improve myself like topics on which i have to focus to improve myself ?

    • @freecourseplatformenglish2829
      @freecourseplatformenglish2829 Місяць тому +2

      If it's one off problem then don't worry, but if you are finding it hard to comeup with intuition quite frequently after solving 700+ questions means that you are either solving too easy question or similar pattern question. I will recommend you to solve Neetcode 150 questions and make sure you don't watch solution before 1 hour and try to understand solution in case you need to watch solution.

    • @sachinnair1648
      @sachinnair1648 Місяць тому +2

      Do you revisit and resolve the problems you've solved before ? If you don't then start doing them now. There's always an element of memorization when you want to understand something but don't overuse it.

    • @freecourseplatformenglish2829
      @freecourseplatformenglish2829 Місяць тому

      @@sachinnair1648 excellent point made. rather than solving 700+ problem its better to solve 350 questions twice so that you can recall pattern much quickly.

  • @Spongebob-gt7rg
    @Spongebob-gt7rg Місяць тому

    Pls do Longest Subarray with sum K | [Postives and Negatives]

  • @_PranavDesai
    @_PranavDesai Місяць тому

    i was almost going for a dynamic programming solution until I saw the constraints and noped out

  • @kartikeyaarun2490
    @kartikeyaarun2490 Місяць тому

    First solution is at least 40ms faster according to leetcode lol. So much for optimization 😂.