Title: #3344 - Maximum Sized Array - LeetCode JavaScript Solutions
Open Graph Title: #3344 - Maximum Sized Array - LeetCode JavaScript Solutions
X Title: #3344 - Maximum Sized Array - LeetCode JavaScript Solutions
Description: Given a positive integer s, let A be a 3D array of dimensions n × n × n, where each element A[i][j][k] is defined as: A[i][j][k] = i * (j O...
Open Graph Description: Given a positive integer s, let A be a 3D array of dimensions n × n × n, where each element A[i][j][k] is defined as: A[i][j][k] = i * (j O...
X Description: Given a positive integer s, let A be a 3D array of dimensions n × n × n, where each element A[i][j][k] is defined as: A[i][j][k] = i * (j O...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/maximum-sized-array
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/maximum-sized-array","name":"#3344 - Maximum Sized Array - LeetCode JavaScript Solutions","description":"Given a positive integer s, let A be a 3D array of dimensions n × n × n, where each element A[i][j][k] is defined as: A[i][j][k] = i * (j O...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} s\n * @return {number}\n */\nvar maxSizedArray = function(s) {\n let left = Math.floor(Math.pow(4 * s, 0.2) / 2);\n let right = Math.floor(Math.sqrt(2 * Math.sqrt(s))) + 2;\n\n while (left < right) {\n const mid = Math.floor((left + right) / 2);\n\n if (calculateArraySum(mid) > s) {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n\n return left - 1;\n\n function getBitOnesCount(n, bitPosition) {\n const blockSize = 1 << bitPosition;\n const cycleSize = 1 << (bitPosition + 1);\n const fullCycles = Math.floor(n / cycleSize);\n const remainder = n % cycleSize;\n\n return fullCycles * blockSize + (remainder > blockSize ? remainder - blockSize : 0);\n }\n\n function calculateArraySum(n) {\n let totalSum = 0;\n\n for (let bitPos = 0; (1 << bitPos) < n; bitPos++) {\n const onesCount = getBitOnesCount(n, bitPos);\n const zerosCount = n - onesCount;\n\n totalSum += (n * n - zerosCount * zerosCount) * (1 << bitPos);\n }\n\n return totalSum * (n - 1) * n / 2;\n }\n};","keywords":"LeetCode 3344, #3344 - Maximum Sized Array, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/maximum-sized-array |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/maximum-sized-array/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/3344-maximum-sized-array.js |
| Binary Search | https://leetcodejavascript.com/tags/binary-search |
| Bit Manipulation | https://leetcodejavascript.com/tags/bit-manipulation |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow