<p>The <strong>bitwise AND</strong> of an array <code>nums</code> is the bitwise AND of all integers in <code>nums</code>.</p>

<ul>
	<li>For example, for <code>nums = [1, 5, 3]</code>, the bitwise AND is equal to <code>1 &amp; 5 &amp; 3 = 1</code>.</li>
	<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li>
</ul>

<p>You are given an array of positive integers <code>candidates</code>. Evaluate the <strong>bitwise AND</strong> of every <strong>combination</strong> of numbers of <code>candidates</code>. Each number in <code>candidates</code> may only be used <strong>once</strong> in each combination.</p>

<p>Return <em>the size of the <strong>largest</strong> combination of </em><code>candidates</code><em> with a bitwise AND <strong>greater</strong> than </em><code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> candidates = [16,17,71,62,12,24,14]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The combination [16,17,62,24] has a bitwise AND of 16 &amp; 17 &amp; 62 &amp; 24 = 16 &gt; 0.
The size of the combination is 4.
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
Note that more than one combination may have the largest size.
For example, the combination [62,12,24,14] has a bitwise AND of 62 &amp; 12 &amp; 24 &amp; 14 = 8 &gt; 0.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> candidates = [8,8]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The largest combination [8,8] has a bitwise AND of 8 &amp; 8 = 8 &gt; 0.
The size of the combination is 2, so we return 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= candidates.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= candidates[i] &lt;= 10<sup>7</sup></code></li>
</ul>


# Submitted by Samy Vilar <samy_vilar> on 07/31/2024

# We will leverage the monotonic nature of bitwise-and, essentially for any
# initial pattern of set-bits, we note each sub-sequent and-operation/item-inclussion
# a subset (possibly empty) of those set-bits will be cleared;
# implying at the very least before the "final" operation all preceeding
# patterns had to share some (non-empty) pattern, specifically the very "final"
# pattern that where cleared to reach 0, otherwise it would imply some
# preceeding operation failed to clear a member of said pattern or a some
# member(s) flipped/cycled both of which would be impossible;

# Implying our solution must be the maximal frequency of some set bit common
# to a subset of all candidates;

# Since for each of the n items, at worse, we could be considering log(max(nums))
# set bits, it would imply O(n + log(max(nums))) time w/ O(1) additional-space
# since we could consider each bit in "isolation";

# version 1.1 
# version 1.1.1
# version 1.2

# version 1.3 packed-operation(s)

class Solution:
    def largestCombination(self, nums, joined=partial(reduce, int.__or__), 
                           tails=0, solution=1):        
        mask = (1 << (span := len(nums).bit_length())) - 1    
        targets = joined(nums)
        while targets:
            others = targets        
            while others:
                tails += (tail := others & -others)
                others &= ~((tail << span) - 1)
            totals = sum(map(tails.__and__, nums))
            targets -= tails
            while tails:
                if solution < (candidate := (
                        totals >> ((tail := tails & -tails).bit_length() - 1)
                    ) & mask):
                    solution = candidate
                tails -= tail
        return solution

589