Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to come up with a solution that did not loop over the collection N times and I believe I've found a novel divide and conquer approach:</p> <pre><code>int n0_, n1_, n2_, n3_, n4_, n5_, n6_, n7_; // Input data int n0 = 0; int n1 = 64; int n2 = 8; int n3 = 8; int n4 = 0; int n5 = 12; int n6 = 224; int n7 = 0; //Subdivide into four groups of 2 (trivial to solve each pair) n0_ = n0 &amp; n1; n1_ = n0 | n1; n2_ = n2 &amp; n3; n3_ = n2 | n3; n4_ = n4 &amp; n5; n5_ = n4 | n5; n6_ = n6 &amp; n7; n7_ = n6 | n7; //Merge into two groups of 4 n0 = (n0_ &amp; n2_); n1 = (n0_ &amp; n3_) | (n1_ &amp; n2_); n2 = (n0_ | n2_) | (n1_ &amp; n3_); n3 = (n1_ | n3_); n4 = (n4_ &amp; n6_); n5 = (n4_ &amp; n7_) | (n5_ &amp; n6_); n6 = (n4_ | n6_) | (n5_ &amp; n7_); n7 = (n5_ | n7_); //Merge into final answer n0_ = (n0 &amp; n4); n1_ = (n0 &amp; n5) | (n1 &amp; n4); n2_ = (n0 &amp; n6) | (n1 &amp; n5) | (n2 &amp; n4); n3_ = (n0 &amp; n7) | (n1 &amp; n6) | (n2 &amp; n5) | (n3 &amp; n4); n4_ = (n0) | (n1 &amp; n7) | (n2 &amp; n6) | (n3 &amp; n5) | (n4); n5_ = (n1) | (n2 &amp; n7) | (n3 &amp; n6) | (n5); n6_ = (n2) | (n3 &amp; n7) | (n6); n7_ = (n3 | n7); </code></pre> <p>This approach requires just 56 bitwise operations, which is considerably fewer than the other solutions provided.</p> <p>It is important to understand the cases in which bits will be set in the final answer. For example, a column in n5 is 1 if there are three <em>or more</em> bits set in that column. These bits can arranged in any order, which makes counting them efficiently fairly difficult.</p> <p>The idea is to break the problem into sub-problems, solve the sub-problems and then merge the solutions together. Every time we merge two blocks, we know the bits will have been correctly "dropped" in each. This means <em>we won't have to check every possible permutation</em> of bits at each stage.</p> <p>Although I didn't realize it until now, this is really similar to Merge Sort which capitalizes on sorted sub-arrays when merging. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload