<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>. In an operation, you can choose an element and multiply it by <code>2</code>.</p>

<p>Return <em>the maximum possible value of </em><code>nums[0] | nums[1] | ... | nums[n - 1]</code> <em>that can be obtained after applying the operation on nums at most </em><code>k</code><em> times</em>.</p>

<p>Note that <code>a | b</code> denotes the <strong>bitwise or</strong> between two integers <code>a</code> and <code>b</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [12,9], k = 1
<strong>Output:</strong> 30
<strong>Explanation:</strong> If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
</pre>

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

<pre>
<strong>Input:</strong> nums = [8,1,2], k = 2
<strong>Output:</strong> 35
<strong>Explanation:</strong> If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= 15</code></li>
</ul>

class Solution:
    def maximumOr(self, nums: List[int], k: int) -> int:
        if len(nums) == 1:
            return nums[0] * (2**k)
        nums_most_significant = [(el, math.floor(math.log2(el))) for el in nums]
        nums_most_significant.sort(key= lambda x: -x[1])
        if nums_most_significant[0][1] > nums_most_significant[1][1]:
            res = nums_most_significant[0][0] * (2**k)
            for i in range(1, len(nums)):
                res |= nums_most_significant[i][0]
            return res
        
        nums_same_sig = {
            nums_most_significant[0][0]: 1
        }
        others_or = 0
        for i in range(1, len(nums)):
            if nums_most_significant[i][1] == nums_most_significant[0][1]:
                nums_same_sig[nums_most_significant[i][0]] = nums_same_sig.get(nums_most_significant[i][0], 0) + 1
            else:
                others_or|=nums_most_significant[i][0]

        m = 0
        nums_same_sig = [(k,v) for k,v in nums_same_sig.items()]
        
        prefix_or = [0]*len(nums_same_sig)
        suffix_or = [0]*len(nums_same_sig)

        for i in range(len(nums_same_sig)):
            if i != 0:
                prefix_or[i]|= prefix_or[i-1]
            prefix_or[i] |=  nums_same_sig[i][0]

        for i in range(len(nums_same_sig)-1,-1,-1):
            if i != len(nums_same_sig)-1:
                suffix_or[i]|= suffix_or[i+1]
            suffix_or[i] |=  nums_same_sig[i][0]

        for i in range(len(nums_same_sig)):
            res = nums_same_sig[i][0] * (2**k)
            if nums_same_sig[i][1] > 1:
                res |= nums_same_sig[i][0]
            if i != 0:
                res |= prefix_or[i-1]
            if i != len(nums_same_sig)-1:
                res |= suffix_or[i+1]
            m = max(m, res | others_or)
        return m
        



        
741