Title: #2401 - Longest Nice Subarray - LeetCode JavaScript Solutions
Open Graph Title: #2401 - Longest Nice Subarray - LeetCode JavaScript Solutions
X Title: #2401 - Longest Nice Subarray - LeetCode JavaScript Solutions
Description: You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements th...
Open Graph Description: You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements th...
X Description: You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements th...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/longest-nice-subarray
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/longest-nice-subarray","name":"#2401 - Longest Nice Subarray - LeetCode JavaScript Solutions","description":"You are given an array nums consisting of positive integers. We call a subarray of nums nice if the bitwise AND of every pair of elements th...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @return {number}\n */\nvar longestNiceSubarray = function(nums) {\n let result = 1;\n let left = 0;\n let usedBits = 0;\n\n for (let right = 0; right < nums.length; right++) {\n while ((usedBits & nums[right]) !== 0) {\n usedBits ^= nums[left];\n left++;\n }\n usedBits |= nums[right];\n result = Math.max(result, right - left + 1);\n }\n\n return result;\n};","keywords":"LeetCode 2401, #2401 - Longest Nice Subarray, 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/longest-nice-subarray |
| 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/longest-nice-subarray/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/2401-longest-nice-subarray.js |
| Array | https://leetcodejavascript.com/tags/array |
| Bit Manipulation | https://leetcodejavascript.com/tags/bit-manipulation |
| Sliding Window | https://leetcodejavascript.com/tags/sliding-window |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow