Sum of All Subset XOR Totals | Simple Story To Code | Leetcode 1863 | codestorywithMIK

Sdílet
Vložit
  • čas přidán 1. 06. 2024
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 39th Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK
    Subsets - • Subsets | Simple Story...
    In this video we will try to solve an easy bit-magic Problem : Sum of All Subset XOR Totals | Simple Story To Code | Leetcode 1863 | codestorywithMIK
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Sum of All Subset XOR Totals | Simple Story To Code | Leetcode 1863 | codestorywithMIK
    Company Tags : Google, Meta
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/sum-of-...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My Recursion Concepts Playlist : • Introduction | Recursi...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Summary :
    Approach 1: Using Simple Backtracking to Generate Subsets
    **Time Complexity**: O(n * 2^n)
    **Space Complexity**: O(n * 2^n)
    In this approach, we use backtracking to generate all possible subsets of the input array `nums`. We recursively build subsets by either including or excluding each element. Once all subsets are generated, we iterate through each subset to compute their XOR sums and accumulate these results.
    This method is straightforward and easy to understand, but it has higher time and space complexity due to the need to store all subsets.
    Approach 2: Using Optimal Backtracking
    **Time Complexity**: O(2^n)
    **Space Complexity**: O(n)
    In this approach, we use an optimized form of backtracking that calculates the sum of XORs of all subsets without explicitly generating the subsets. Instead, we recursively compute the XOR sums by either including or excluding each element and sum up the results directly.
    This method is more efficient in terms of space since it does not need to store all subsets. It leverages recursion to directly compute the required sum, reducing the overall space complexity.
    Approach : Using Pattern
    The provided solution calculates the sum of the XORs of all subsets of an integer array efficiently by leveraging bitwise operations. Here’s a concise summary of the approach:
    1. **Bitwise OR Calculation**: The solution first computes the bitwise OR of all elements in the array. This captures all the bits that could possibly be set in any subset.
    2. **Shift Operation**: The result of the OR operation is then left-shifted by `(n - 1)` positions, where `n` is the number of elements in the array. This accounts for the fact that each bit's contribution appears in multiple subsets.
    This method simplifies the problem by avoiding the need to explicitly generate and XOR each subset, achieving a linear time complexity relative to the number of elements in the array. The approach takes advantage of the properties of XOR and the combinatorial structure of subsets.
    Short summary on Why This Works : The sum of the XORs of all subsets can be derived using bitwise properties:
    Each bit in the result will be set if it is set in any of the elements of the array.
    For an array of length n, each subset can include or exclude an element, leading to 2
    n−1
    subsets that include a particular element in some position.
    ✨ Timelines✨
    00:00 - Introduction
    02:22 - Approach-1
    09:15 - Approach-2
    17:49 - Approach-3
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024

Komentáře • 68

  • @satendra6200
    @satendra6200 Před 13 dny +34

    please make the video on the intuition behind the 3rd appraoch..

    • @deepaksingh-qd7xm
      @deepaksingh-qd7xm Před 13 dny

      yes

    • @theprnvanand
      @theprnvanand Před 13 dny

      exactly bhai kya chu&iyap hai.. intuition dekhne aaya tha behind shifting by n-1. Aur pattern ke name pe skip krdiya

    • @deepaksingh-qd7xm
      @deepaksingh-qd7xm Před 13 dny +4

      @@theprnvanand to isme gali dene wala logic to kuch nahi hua atleast usne bataya to it's your duty to find it sab kuch hath p nahi milta h bro thoda hath pair chalao google is all yours

    • @codestorywithMIK
      @codestorywithMIK  Před 12 dny +5

      Hi guys,
      Apologies for any inconvenience. Like I mentioned in the video that I will create a separate video for that in my free times. It’s difficult to make detailed video early morning because of my office and other morning activities.
      Hope you guys will understand ❤️🙏
      But if I get some free time, I will create a detailed video on why it works.
      Thank you again for your patience ❤️

  • @VaibhavSingh-kh3jr
    @VaibhavSingh-kh3jr Před 13 dny +3

    Please make detailed video on 3rd approach

  • @best_0111
    @best_0111 Před 13 dny +3

    sir i would like to provide one suggestion 🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
    plz add 3 to 4 similar types question it helps alot like we learn from you solve the question and excel ourself with your help so plz do this one i am following you since a month and i am loving you content too much like i am addicted to your content i come i learn i solve and repeat this process
    💥❤‍🔥❤‍🔥❤‍🔥❤‍🔥❤‍🔥💥💥💥

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar Před 13 dny +4

    Respected MIK Sir,
    Whenever I watch your videos, I understand how easily & efficiently one can solve difficult questions.
    Thank you so much for sharing this knowledge for free. 🙏🏼

  • @user13443fg
    @user13443fg Před 13 dny +3

    how 3rd approach works please make a video on that.

  • @Akashkumar_12
    @Akashkumar_12 Před 13 dny +3

    3rd approach is awesome 😅❤

  • @KUMARBISHWAJEET-iq6ym
    @KUMARBISHWAJEET-iq6ym Před 13 dny +1

    yes need the video on the intuition behind the 3rd appraoch..

  • @elakstein
    @elakstein Před 13 dny +1

    Would love an explanation on proof of 3rd approach, if you have time then please consider

  • @shaktlonda12
    @shaktlonda12 Před 13 dny +1

    Yes , please make a video on how to get this intuition

  • @akshaychavan5511
    @akshaychavan5511 Před 13 dny +1

    Did this question using recursion. However, came here to see new approaches and I am quite amazed seeing the 3rd approach. Indeed a tough intuition to come up with.

  • @Shivam_7488
    @Shivam_7488 Před 13 dny +2

    3rd approch man 👍fabulous.just looking like Wow...

  • @Inspire_with_SM
    @Inspire_with_SM Před 13 dny +2

    Great Bhaiya, Bhaiya aaj ka question easy marked hai but aise hi question mey mey fass jata hu like mey subset create karke to kiya but Approach -3 wala mujhse nahi hit hua mind mey to ye kaise develop kare, aur phir aise hi problem se moral down ho jata hai ki solve to kiye but best approach nahi tha, and phir self doubt bhi hone galta hai

  • @BiswajitRout-dl5ry
    @BiswajitRout-dl5ry Před 13 dny +1

    Bhiya, please post a video of the last approach on the reason of following pattern the last one approach is very awesome.

  • @focusman8801
    @focusman8801 Před 13 dny +3

    Bhaiya pls make video why pattern work in the 3rd approach

  • @pokeindia5361
    @pokeindia5361 Před 13 dny +1

    Sir please the video on the intuition behind the 3rd approach.

  • @rahulharsh545
    @rahulharsh545 Před 13 dny +1

    this guy will make me fall in love with dsa

  • @ManojKrVerma-vw4dx
    @ManojKrVerma-vw4dx Před 13 dny +1

    Yes, please make a video on 3rd approach.

  • @wearevacationuncoverers
    @wearevacationuncoverers Před 13 dny +2

    Day-1 of asking "How do you manage teaching DSA, Office Hours, Doing sports as well as travel and handling social media" 😅

  • @spdh6325
    @spdh6325 Před 13 dny +1

    Mik Bhai, ye kar diya approach -3 pura hila diya. Approach - 3 actually blow my mind. But ek kuch explanation do how to build this type of Intuition!

  • @tutuimam3381
    @tutuimam3381 Před 13 dny +2

    Amazing explanation

  • @nirmalgurjar8181
    @nirmalgurjar8181 Před 13 dny

    Good solution but would have been better if logic behind 3rd approach was also covered in the video, we can watch 1 hr long videos of such amazing content.

  • @notjod4948
    @notjod4948 Před 13 dny +2

    finding the last approach is not at all intutive unless you understand that much maths in the interview!

  • @pradyumnmulegaon385
    @pradyumnmulegaon385 Před 13 dny

    approach 3 was really nice one🔥🔥

  • @dayashankarlakhotia4943

    Good explanation 👏 👍

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 13 dny

    Thanks a lot bhaiya ❤❤

  • @user-sn1oe5hr2j
    @user-sn1oe5hr2j Před 10 dny

    Bro please make a video on tarjan's algorithm, Leetcode 1192. Great content brother, lots of respect

  • @codingkart245
    @codingkart245 Před 13 dny

    please make the video on the intuition behind the last approach please.

  • @027_CSE_AbhishekSingh
    @027_CSE_AbhishekSingh Před 13 dny

    Thank u so much ❤

  • @prithammahato1915
    @prithammahato1915 Před 13 dny

    Thank You Bhiya :)

  • @aizad786iqbal
    @aizad786iqbal Před 13 dny

    please explain the 3rd approach, any proof apart from obsering patterns.... what's the logic behind it, why it works

  • @Strawcontamination
    @Strawcontamination Před 12 dny

    bhaiya please give us 3-4 home work questions it would be really helpful

  • @SatyamYadav-oz5ge
    @SatyamYadav-oz5ge Před 13 dny

    sir plz make a video on detailed explanation of approach 3 🙏

  • @nish0798
    @nish0798 Před 12 dny

    bro what are you plaanning to finish first dp concept or start backtracking concepts

  • @kumkumslab5811
    @kumkumslab5811 Před 13 dny +1

    damn good mann!!

  • @_rugung
    @_rugung Před 13 dny

    3rd approach ke upar video bana dijiye 🙏

  • @aizad786iqbal
    @aizad786iqbal Před 13 dny

    how is this marked easy :)
    so much to learn....
    should I start with recursion and dp first ?

  • @Coder_Buzz07
    @Coder_Buzz07 Před 13 dny

    Bhaiya last wale approach ke lie video banado plsssss

  • @Akmabedinkadersafi-O
    @Akmabedinkadersafi-O Před 13 dny

    Please Explain why the third approach worked!

  • @mayank1522
    @mayank1522 Před 13 dny

    sir at 5.07 apne by reference num kyu lia usko koi reason h ? and can we use onli auto there

  • @vinayaksinghal
    @vinayaksinghal Před 13 dny

    bhaiya video bana dena approach intuition pe

  • @pankajkashyap2892
    @pankajkashyap2892 Před 13 dny

    what is intution behind third approach that it works,please explain how it works.

  • @robot3.077
    @robot3.077 Před 13 dny +1

    power set algorithm

  • @SEVERANCE850
    @SEVERANCE850 Před 13 dny

    But can you please explain why the third approach works?

  • @yogesh9974
    @yogesh9974 Před 12 dny

    Please Intuiton behind 3rd approach

  • @Dezy_Kri
    @Dezy_Kri Před 13 dny +1

    Done ✔️

  • @gui-codes
    @gui-codes Před 13 dny +2

    I was able to solve using approach-1 on my own 🤩
    Thank you

  • @davidmiller814
    @davidmiller814 Před 13 dny

    Third approach, albeit simple, is hard to come up with in an interview setting I think.

  • @aizad786iqbal
    @aizad786iqbal Před 13 dny

    result

  • @souravjoshi2293
    @souravjoshi2293 Před 13 dny

    We can also find the subsets using bit manipulation technique

  • @jevinmakwana6811
    @jevinmakwana6811 Před 13 dny

    Can anyone explain the intuition behind the 3rd approach?

  • @sauravchandra10
    @sauravchandra10 Před 13 dny

    This can also be done using bit manipulation.

  • @utkarshsahay9908
    @utkarshsahay9908 Před 13 dny

    3rd Approach Kaise Socha Sir ????

  • @ShyamSundar-kz5oz
    @ShyamSundar-kz5oz Před 13 dny

    Sir dp series please

  • @69_memes
    @69_memes Před 13 dny

    approach 3rd is OR not XOR
    ?

  • @027_CSE_AbhishekSingh
    @027_CSE_AbhishekSingh Před 13 dny

    Bhaiya ❤

  • @chirag7694
    @chirag7694 Před 13 dny

    Why does the third approach work, Can anybody please share a link or a video for the proof of that

    • @user-ub2is4rs4x
      @user-ub2is4rs4x Před 13 dny

      It’s available on official solution but I could not understand. I also saw vortubrac’s leetcode solution in Discuss, but he has not explained it.

  • @ratishjain2718
    @ratishjain2718 Před 13 dny

    man wtf was that last inuition 🤯

  • @vagabondfoodie5788
    @vagabondfoodie5788 Před 13 dny

    sir, it might sound funny, but please its a request help me to understand how can i , iterate vector subset to find all the xor , basically i am asking about approach number one

  • @vikassingh8598
    @vikassingh8598 Před 13 dny

    int solve(vector& nums, int i){
    if(i>=nums.size()){
    return 0;
    }

    int include=nums[i]^solve(nums, i+1);
    int exclude=0^solve(nums, i+1);

    return include+exclude;
    }
    int subsetXORSum(vector& nums) {
    return solve(nums, 0);
    }
    why this is incorrect