Created
November 24, 2025 21:58
-
-
Save tatsuyax25/9c2b5e30cedb3b92e9deb34d9ab546f2 to your computer and use it in GitHub Desktop.
You are given a binary array nums (0-indexed). We define xi as the number whose binary representation is the subarray nums[0..i] (from most-significant-bit to least-significant-bit). For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {boolean[]} | |
| */ | |
| var prefixesDivBy5 = function(nums) { | |
| // Result array to store true/false for each prefix | |
| let answer = []; | |
| // We'll keep track of the current number modulo 5 | |
| // This avoids dealing with huge binary numbers directly | |
| let currentMod = 0; | |
| // Iterate through each bit in nums | |
| for (let i = 0; i < nums.length; i++) { | |
| // Shift left (multiply by 2) and add the new bit | |
| // Example: if currentMod = 2 (binary '10') and nums[i] = 1, | |
| // new number = '101' -> decimal 5 | |
| currentMod = (currentMod * 2 + nums[i]) % 5; | |
| // If currentMod is 0, then the number is divisible by 5 | |
| answer.push(currentMod === 0); | |
| } | |
| return answer; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment