Count Triplets That Can Form Two Arrays of Equal XOR - Leetcode 1442 - Python

Поделиться
HTML-код
  • Опубликовано: 25 июн 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🐦 Twitter: / neetcode1
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    Problem Link: leetcode.com/problems/count-t...
    0:00 - Read the problem
    0:50 - O(n^4) Explanation
    4:15 - O(n^4) Coding
    6:13 - O(n^3) Explanation
    7:58 - O(n^3) Coding
    9:25 - O(n^2) Explanation
    14:45 - O(n^2) Coding
    15:58 - O(n) Explanation
    27:28 - O(n) Coding
    leetcode 1442
    #neetcode #leetcode #python

Комментарии • 55

  • @NeetCodeIO
    @NeetCodeIO  27 дней назад +36

    Just so you know, there's a reason that the 4th solution took half the video to explain. You've been warned :)

    • @satyamjha68
      @satyamjha68 27 дней назад +3

      I also took some time to think O(n) approach , I guess I wouldn't be able to code the O(n) during 45 minutes interview

    • @jonsneep
      @jonsneep 26 дней назад +6

      Love this new format of you covering multiple options with different time-complexities

    • @nirmalgurjar8181
      @nirmalgurjar8181 26 дней назад

      O(n) is simple maths we can derive equation from O(n * n) solution, here is how> Once you code O(n * n) solution and check the line where you are updating the count, by simple maths you will notice that you are updating res count in the loop matching idx times and subtracting each matching index, so that equation comes to count += (number of matching idx * current idx - sum of all matching idx) , once you get this you can convert O(n * n) solution to n solution using 2 hashmaps.

    • @user-sx7zc3ez4w
      @user-sx7zc3ez4w 26 дней назад

      God damn GENIUS 4th solution. BTW, No need to feel sorry since we can grab your explanation.

    • @erminiottone
      @erminiottone 26 дней назад

      Let's suffer together

  • @satyamjha68
    @satyamjha68 27 дней назад +5

    4 approaches , amazing problem ! Perfect for a dsa interview!

  • @redfinance3403
    @redfinance3403 26 дней назад +3

    i * prev_xor_cnt[prefix] - prev_xor_index_sum[prefix] is certainly a pattern to remember!

  • @sShield
    @sShield 27 дней назад +8

    I could not wrap my head around this problem, thanks bro :)

    • @NeetCodeIO
      @NeetCodeIO  27 дней назад +10

      ofcourse, but no promises that my video will gurantee that either, it's a difficult one :)

  • @theteacher010
    @theteacher010 26 дней назад +1

    Watched well over 250 of your videos and this is honestly one of THE best ones yet. Thanks for being an ever-helpful presence in my hunt for a new gig!

    • @NeetCodeIO
      @NeetCodeIO  26 дней назад +1

      really appreciate that, thank you :)

  • @SudharsanSankaranarayanaPrasad
    @SudharsanSankaranarayanaPrasad 27 дней назад +1

    you just dropped the video at the exact same time when I was looking for it! Awesome explanation!

  • @sachethkoushik2265
    @sachethkoushik2265 26 дней назад +2

    if anyone's lookin for a lil more pythony but simplified code, here's one
    res = curr = 0
    d = defaultdict(lambda: (0, 0))
    d[0] = (1, 0) # count, index sum called as total henceforth
    for i, x in enumerate(arr):
    curr ^= x
    cnt, total = d[curr]
    res += (i * cnt) - total
    d[curr] = (cnt + 1, total + i + 1)
    return res
    ps: haven't completely wrapped my head and this one still :)

  • @MP-ny3ep
    @MP-ny3ep 27 дней назад +3

    Beautifully explained. Thank you

  • @michael._.
    @michael._. 27 дней назад +22

    who else was waiting for neetcode's explanation on O(n) solution

  • @chien-yuyeh9386
    @chien-yuyeh9386 27 дней назад +3

    Thanks for sharing! 🎉

  • @saianuragone828
    @saianuragone828 26 дней назад

    Great Explanation man, Loved it🙌

  • @mounirkanane8083
    @mounirkanane8083 26 дней назад

    Thanks bro you've really become a veteran at leetcode and that is reflected in your explanations nowadays

  • @pastori2672
    @pastori2672 26 дней назад

    a lot of effort was put into this one thank you

  • @TenzDelek
    @TenzDelek 27 дней назад +3

    I understood the n2 solution better than the n3,n4 one haha

  • @Perry-tf2pr
    @Perry-tf2pr 27 дней назад +1

    last solution res += i * prev_xor_cnt[prefix] - prev_xor_index_sum[prefix] likely summation iterations formula

  • @PhanNghia-fk5rv
    @PhanNghia-fk5rv 26 дней назад +2

    Leetcode 560 -> Leetcode 1074 -> Leetcode 1442
    def countTriplets(self, ar: List[int]) -> int:
    res = 0
    prefix = {0: [0]}
    curXor = 0
    for i in range(len(ar)):
    curXor ^= ar[i]
    if curXor in prefix:
    for v in prefix[curXor]:
    res += (i - v)
    prefix[curXor].append(i + 1)
    else:
    prefix[curXor] = [i + 1]
    return res

  • @protodimbo
    @protodimbo 27 дней назад +7

    you can also feel good about yourself because I'm about to probably make you feel bad 🤣🤣🤣

    • @MasterOwen69
      @MasterOwen69 26 дней назад

      Exactly what i am about to comment..🤣🤣

  • @rlm3227
    @rlm3227 26 дней назад

    bro I was searching for this in the morning, even though I later found some bit manipulation concept I didn't remember, as I was looking for some hints which helped me solve this problem.

  • @nathan_ca
    @nathan_ca 27 дней назад

    Good mental exercise! I think the real tricky part is the i * prev_cnt, you could explain that a bit more while you draw, also during interviews I think the most difficult part is to figure out those index + 1 for the index_sum in order for math to work out. BUT nice explanation, if I were to read code myself it would at least 30 mins for me to figure out

  • @melvinbijman2337
    @melvinbijman2337 26 дней назад

    @NeetCodeIO, thanks for another great explanation but one point though.
    Writing N capitalised would denote it as a global, (stored on the heap) accessible by every function and method.
    As such lowercase would be correct way.

  • @limsiryuean5040
    @limsiryuean5040 27 дней назад

    The O(n) solution seems like a happy coincidence between array and XOR properties, it is pretty hard to explain a coincidence. But either way your solution is pretty clear and thank you for torturing my brain

  • @deadlyecho
    @deadlyecho 26 дней назад +1

    I came up with the N^3 first and then the N^2... and then saw the shit that's going through people's minds in the leetcode solutions section 😂

  • @ish90917
    @ish90917 27 дней назад

    I think after seeing the code of the 4th solution, it became a little bit easier to understand. So watch the whole thing for a better understanding.

  • @GameFlife
    @GameFlife 26 дней назад

    When i heard neet say hopefully interview they dont ask you to come up with this am just fing froze hahaha

  • @evalyly9313
    @evalyly9313 27 дней назад +1

    Falling in love with your brain❤

  • @EduarteBDO
    @EduarteBDO 26 дней назад

    I gave up trying to understand after second solution, but watched the video anyway. I'm just tired of xor math for this month.

  • @saranshthukral4021
    @saranshthukral4021 26 дней назад

    "u like suffering as much as I do" , we on a right path

  • @lancetv4826
    @lancetv4826 27 дней назад

    @NeetCodeIO please add timer and notes on neetcode

  • @mounirkanane8083
    @mounirkanane8083 26 дней назад

    Question: 14:14 you say k is also fixed but cant k also = j? So k could either be at index 1 or index 2?

  • @HuanjiaLiang
    @HuanjiaLiang 26 дней назад

    Hello, I have a question, in 2:46, shouldn't b be 0 since b = arr[j] XOR arr[k], and arr[j] == arr[k] == 3, so 3 XOR 3 == 0? But in the video it says b == 3, i am confused, thanks!

  • @ajayprabhu465
    @ajayprabhu465 26 дней назад

    can anyone help me with a doubt:
    what was the significance of that x1 ^ x2 ... in commning up with n^2 solution? and why k - i , on what basis?
    thank you in advance.

  • @mdnoor4750
    @mdnoor4750 24 дня назад

    3143. Maximum Points Inside the Square

  • @Julius-db1ph
    @Julius-db1ph 26 дней назад

    bro I think at this point i am in love with suffering with you

  • @fraserdab
    @fraserdab 27 дней назад

    Came up with n^2 solution but was irritated i couldn't come up with n

  • @swarnimvarshneya6944
    @swarnimvarshneya6944 27 дней назад

    why a^= arr[j-1] and not j?

    • @bedminer1
      @bedminer1 26 дней назад

      portion a starts from i (since first j is i + 1, take j - 1) and it goes up until the index before j (a doesn't include element at index j)

  • @user-vk1tc6cu5t
    @user-vk1tc6cu5t 27 дней назад

    how 2 Xor 2 is 2 its 0 not understand the N^4 solution

  • @dudeyouhavenoidea
    @dudeyouhavenoidea 26 дней назад +1

    why why why why why why why whyyyyyyyyyyyy

  • @shklbor
    @shklbor 26 дней назад

    in your defense, I'll say those who make it till the end of the video would finally get it lol

  • @robertweekes5783
    @robertweekes5783 25 дней назад

    What the hell is XOR

  • @eddiecode100
    @eddiecode100 27 дней назад

    she grokking on my data structure until i leetcode