Step by step explanation || best Solution
Intuition\n1. We start with the most significant bit (30th bit assuming the maximum value of elements in nums is less than 2^30) and iterate downwards.\n2. In e
# Intuition\n1. We start with the most significant bit (30th bit assuming the maximum value of elements in `nums` is less than 2^30) and iterate downwards.\n2. In each iteration, we count the number of elements in `nums` that have the current bit set to 1.\n3. If the count is greater than `k` or if setting the current bit to 1 results in a value less than the current maximum value, we skip this bit.\n4. Otherwise, we update the maximum value by setting the current bit to 1 and subtract the count from `k`.\n5. We continue this process for each bit in descending order until we have exhausted `k` operations or iterated through all the bits.\n6. Finally, we return the maximum value obtained.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a variable named `max_val` to store the maximum possible value.\n2. Initialize a variable named `mask` to 0. This mask will help us keep track of the significant bits in the numbers.\n3. Iterate from the 30th bit (assuming the maximum value of nums elements is less than 2^30) to 0 in descending order. In each iteration, perform the following steps:\n    - Initialize a variable named `count` to 0. This variable will keep track of how many numbers in nums have the current bit set to 1.\n    - Iterate through each number in the nums array. If the bit at the current position is set to 1, increment the count variable.\n    - If count is greater than k or if (mask | (1 << bit)) is less than max_val, continue to the next iteration.\n    - Update max_val to (max_val | (1 << bit)).\n    - Subtract count from k.\n    - Set the bit at the current position in the mask to 1.\n4. Return the value of max_val as the maximum possible value.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumOr(vector<int>& nums, int k) {\n    int n=nums.size();\nlong long pre[n + 1], suf[n + 1];\n    long long sol, pow = 1;\n     for (int i = 0; i < k; i++){\n        pow*=2;\n     }\n \n    pre[0] = 0;\n    for (int i = 0; i < n; i++){\n        pre[i + 1] = pre[i] | nums[i];\n    }\n     suf[n] = 0;\n    for (int i = n - 1; i >= 0; i--){\n        suf[i] = suf[i + 1] | nums[i];\n    }\n     sol= 0;\n    for (int i = 0; i < n; i++)\n        sol = max(sol, pre[i] | (nums[i] * pow) | suf[i + 1]);\n \n    return sol;    \n    }\n};\n```\n![images.jpeg](https://assets.leetcode.com/users/images/8bc4223f-1602-420a-9c6b-853f5f198c6f_1683999525.4729867.jpeg)\n
['C++']