TheRealSamuel/LeetCodeProblem
0561
1{2 "id": 2291,3 "name": "maximum_and_sum_of_array",4 "difficulty": "Hard",5 "link": "https://leetcode.com/problems/maximum-and-sum-of-array/",6 "date": "1644105600000",7 "task_description": "You are given an integer array `nums` of length `n` and an integer `numSlots` such that `2 * numSlots >= n`. There are `numSlots` slots numbered from `1` to `numSlots`. You have to place all `n` integers into the slots such that each slot contains at **most** two numbers. The **AND sum** of a given placement is the sum of the **bitwise** `AND` of every number with its respective slot number. For example, the **AND sum** of placing the numbers `[1, 3]` into slot `1` and `[4, 6]` into slot `2` is equal to `(1 AND 1) + (3 AND 1) + (4 AND 2) + (6 AND 2) = 1 + 1 + 0 + 2 = 4`. Return _the maximum possible **AND sum** of _`nums`_ given _`numSlots`_ slots._ **Example 1:** ``` **Input:** nums = [1,2,3,4,5,6], numSlots = 3 **Output:** 9 **Explanation:** One possible placement is [1, 4] into slot 1, [2, 6] into slot 2, and [3, 5] into slot 3. This gives the maximum AND sum of (1 AND 1) + (4 AND 1) + (2 AND 2) + (6 AND 2) + (3 AND 3) + (5 AND 3) = 1 + 0 + 2 + 2 + 3 + 1 = 9. ``` **Example 2:** ``` **Input:** nums = [1,3,10,4,7,1], numSlots = 9 **Output:** 24 **Explanation:** One possible placement is [1, 1] into slot 1, [3] into slot 3, [4] into slot 4, [7] into slot 7, and [10] into slot 9. This gives the maximum AND sum of (1 AND 1) + (1 AND 1) + (3 AND 3) + (4 AND 4) + (7 AND 7) + (10 AND 9) = 1 + 1 + 3 + 4 + 7 + 8 = 24. Note that slots 2, 5, 6, and 8 are empty which is permitted. ``` **Constraints:** `n == nums.length` `1 <= numSlots <= 9` `1 <= n <= 2 * numSlots` `1 <= nums[i] <= 15`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "nums = [1,2,3,4,5,6], numSlots = 3",12 "output": "9 "13 },14 {15 "label": "Example 2",16 "input": "nums = [1,3,10,4,7,1], numSlots = 9",17 "output": "24 "18 }19 ],20 "private_test_cases": [21 {22 "input": [23 [24 625 ],26 127 ],28 "output": 029 },30 {31 "input": [32 [33 234 ],35 336 ],37 "output": 238 },39 {40 "input": [41 [42 8,43 5,44 15,45 346 ],47 948 ],49 "output": 2550 },51 {52 "input": [53 [54 10,55 8,56 2,57 12,58 6,59 1,60 9,61 13,62 15,63 1564 ],65 766 ],67 "output": 3568 },69 {70 "input": [71 [72 3,73 9,74 5,75 4,76 6,77 178 ],79 380 ],81 "output": 882 },83 {84 "input": [85 [86 9,87 15,88 12,89 10,90 6,91 6,92 10,93 10,94 14,95 1296 ],97 998 ],99 "output": 66100 },101 {102 "input": [103 [104 7,105 3,106 7,107 12,108 5,109 6110 ],111 3112 ],113 "output": 11114 },115 {116 "input": [117 [118 6119 ],120 2121 ],122 "output": 2123 },124 {125 "input": [126 [127 6,128 3,129 9,130 7131 ],132 3133 ],134 "output": 9135 },136 {137 "input": [138 [139 13,140 10,141 13,142 7,143 9,144 8,145 3146 ],147 5148 ],149 "output": 20150 }151 ],152 "haskell_template": "maximumANDSum :: [Int] -> Int -> Int\nmaximumANDSum nums numSlots ",153 "ocaml_template": "let maximumANDSum (nums: int list) (numSlots: int) : int = ",154 "scala_template": "def maximumANDSum(nums: List[Int],numSlots: Int): Int = { \n \n}",155 "java_template": "public static int maximumANDSum(List<Integer> nums, int numSlots) {\n\n}",156 "python_template": "class Solution(object):\n def maximumANDSum(self, nums, numSlots):\n \"\"\"\n :type nums: List[int]\n :type numSlots: int\n :rtype: int\n \"\"\"\n "157}