Блог пользователя HEIR_OF_SLYTHERIN

Автор HEIR_OF_SLYTHERIN, история, 2 года назад, По-английски

Can someone explain the optimal solution for finding the sum of XOR of all sub-arrays of the array. Example:

Input : arr[] = {3, 8, 13} Output : 46

XOR of {3} = 3 XOR of {3, 8} = 11 XOR of {3, 8, 13} = 6 XOR of {8} = 8 XOR of {8, 13} = 5 XOR of {13} = 13

Sum = 3 + 11 + 6 + 8 + 5 + 13 = 46

Thanks in Advance.

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It's a pretty standard problem you may get it here

  • »
    »
    2 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I didn't understand the O(N) solution can you please explain.

    • »
      »
      »
      2 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Yeah, sure! Let's assume the array as bits and you want to find the no of subarrays having a particular bit (say ith bit) set.

      so let's assume the above example {3, 8, 13} as,

      • for 0th bit, {1, 0, 1}
      • for 1th bit, {1, 0, 0}
      • for 2nd bit, {0, 0, 1}
      • for 3rd bit, {0, 1, 1}

      so, now find no of subarrays with odd 1s for each of the bits.

      so how can we do this? see we can count no of subarrays starting with 0th index and have odd 1s then we can iterate over all $$$i$$$ letting it to be the starting index of the subarrays.

      so let say, for ith index have $$$x$$$ subarrays with odd 1s.

      if we encounter 1 at $$$ith$$$ index then for upcoming subarrays the behavior would become opposite (i.e. even 1s subarrays would act like odd 1s subarrays) wo we would do $$${x = n - i - x}$$$ where $$${n - i}$$$ is the number of subarrays starting with $$$ith$$$ index.

      So, we will sum-up all the no of subarrays with odd 1s for each bit (say $$$sum_i$$$).

      so our answer would be $$${\sum sum_i * 2 ^ i}$$$. here $$$2^i$$$ is the contribution of $$$ith$$$ bit.

      • »
        »
        »
        »
        2 года назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        I have a variation of this problem whose statement is as follows :
        You are given an array of int values, of length N. For a subarray it's score is defined as xor of all elements present in the subarray. Your goal is to choose any two non-overlapping subarrays such that sum of their scores is maximum possible. Print the maximum sum.

        How will You solve this problem ?

        Note: By non overlapping i mean : 0<=i1<=j1<i2<=j2<N

        • »
          »
          »
          »
          »
          2 года назад, # ^ |
          Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

          I think we can easily do this problem using $$$Trie$$$ data structure as let say I am iterating on i [0...n-1] and storing all the $$$prefixs$$$ in the trie and at $$$ith$$$ index I would try to find the $$$maximumXor$$$ with current $$$prefix_i$$$ in the trie as

          $$${maximumXor = prefix_i}$$$ ^ $$${prefix_j, (0 \le j < i)}$$$

          which would give me $$$Xor$$$ of subarray [j...i]

          which would be the maximum xor subarray possible in [0...i]. and then I may store it in an array (say, $$$prefixMax$$$).

          where $$${prefixMax_i = max(maximumXor, prefixMax_{i - 1})}$$$ Similarly I may do this with suffix one as well after clearing the trie. so now we can find our answer as maximum over all $$${prefixMax_i + suffixMax_{i + 1}}$$$.

          UPD : My Code

        • »
          »
          »
          »
          »
          2 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Btw can you share the problem link?

      • »
        »
        »
        »
        14 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I'm so dumb, I still can't get it.

        • »
          »
          »
          »
          »
          14 месяцев назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Me as well . it needs some prerequisites in dp , specifically number of subarrays with odd sum here's a leetcode problem

          • »
            »
            »
            »
            »
            »
            4 месяца назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Thank you bro. The problem you gave, helped me write an implementation for the logic. After solving it, I was able to modify my LC submission for sum of XOR one as well.