Binary Prefix Divisible By 5

🏠 ⬅️ ➑️

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.

Return an array of booleans answer where answer[i] is true if xi is divisible by 5.

Example 1:

Input: nums = [0,1,1] Output: [true,false,false] Explanation: The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.

Example 2:

Input: nums = [1,1,1] Output: [false,false,false]

Constraints:

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 3
    integer, dimension(n) :: nums
    logical, dimension(n) :: answer

    ! Example 1
    nums = [0, 1, 1]
    answer = solve(nums)
    write (*, '(A, L1)') 'Example 1: ', answer

    ! Example 2
    nums = [1, 1, 1]
    answer = solve(nums)
    write (*, '(A, L1)') 'Example 2: ', answer

contains

    function solve(nums) result(answer)
        implicit none
        integer, dimension(:), intent(in) :: nums
        logical, dimension(size(nums)) :: answer
        integer :: i

        do i = 1, size(nums)
            answer(i) = mod(xi(nums, i), 5) == 0
        end do
    end function solve

    function xi(nums, i) result(xi)
        implicit none
        integer, dimension(:), intent(in) :: nums
        integer, value :: i
        integer :: xi

        xi = 0
        do while (i > 0)
            xi = xi * 2 + nums(i)
            i = i - 1
        end do
    end function xi
end program main
❌ Compiled
❌ Executed
❌ Correct
module is_divisible_by_5
implicit none
contains

function is_divisible_by_5(nums) result(answer)
integer, intent(in) :: nums(:)
logical :: answer(size(nums))
integer :: i, xi

do i = 1, size(nums)
    xi = 0
    do j = i, size(nums)
        xi = xi * 2 + nums(j)
    end do
    answer(i) = mod(xi, 5) == 0
end do

end function is_divisible_by_5

end module is_divisible_by_5

program test_is_divisible_by_5
use is_divisible_by_5
implicit none
integer, parameter :: nums1(3) = [0, 1, 1]
integer, parameter :: nums2(3) = [1, 1, 1]
integer, parameter :: nums3(3) = [0, 0, 1]
integer, parameter :: nums4(3) = [1, 0, 1]
integer, parameter :: nums5(3) = [0, 1, 0]
integer, parameter :: nums6(3) = [1, 1, 0]
integer, parameter :: nums7(3) = [0, 0, 0]
integer, parameter :: nums8(3) = [1, 0, 0]
integer, parameter :: nums9(3) = [0, 1, 1]
integer, parameter :: nums10(3) = [1, 1, 1]

write (*,*) "Test 1:"
write (*,*) "Input: ", nums1
write (*,*) "Output: ", is_divisible_by_5(nums1)
write (*,*) "Expected: ", .true.
write (*,*) "Test 2:"
write (*,*) "Input: ", nums2
write (*,*) "Output: ", is_divisible_by_5(nums2)
write (*,*) "Expected: ", .false.
write (*,*) "Test 3:"
write (*,*) "Input: ", nums3
write (*,*) "Output: ", is_divisible_by_5(nums3)
write (*,*) "Expected: ", .false.
write (*,*) "Test 4:"
write (*,*) "Input: ", nums4
write (*,*) "Output: ", is_divisible_by_5(nums4)
write (*,*) "Expected: ", .true.
write (*,*) "Test 5:"
write (*,*) "Input: ", nums5
write (*,*) "Output: ", is_divisible_by_5(nums5)
write (*,*) "Expected: ", .false.
write (*,*) "Test 6:"
write (*,*) "Input: ", nums6
write (*,*) "Output: ", is_divisible_by_5(nums6)
write (*,*) "Expected: ", .false.
write (*,*) "Test 7:"
write (*,*) "Input: ", nums7
write (*,*) "Output: ", is_divisible_by_5(nums7)
write (*,*) "Expected: ", .false.
write (*,*) "Test 8:"
write (*,*) "Input: ", nums8
write (*,*) "Output: ", is_divisible_by_5(nums8)
write (*,*) "Expected: ", .true.
write (*,*) "Test 9:"
write (*,*) "Input: ", nums9
write (*,*) "Output: ", is_divisible_by_5(nums9)
write (*,*) "Expected: ", .false.
write (*,*) "Test 10:"
write (*,*) "Input: ", nums10
write (*,*) "Output: ", is_divisible_by_5(nums10)
write (*,*) "Expected: ", .true.

end program test_is_divisible_by_5
🌐 Data from online sources
def largestPerimeter(nums):
    nums.sort(reverse=True)

    for i in range(len(nums) - 2):
        if nums[i] < nums[i + 1] + nums[i + 2]:
            return nums[i] + nums[i + 1] + nums[i + 2]

    return 0

To find the largest perimeter of a triangle with a non-zero area, we first sort the array in descending order. This allows us to check if a triangle can be formed by taking the longest possible sides first. Then, we iterate through the sorted array and check if the current element and the next two elements form a triangle of non-zero area. To check if a triangle can be formed, we use the triangle inequality theorem, which states that for any triangle with sides a, b, and c, the sum of any two sides must be greater than the third side (a + b > c, a + c > b, b + c > a). Since we have the array sorted in descending order, we only need to check one case of the inequality (nums[i] < nums[i + 1] + nums[i + 2]) for each iteration. If we find a valid triangle, we return the sum of its sides as the largest perimeter. If no valid triangle is found, we return 0.

🌐 Data from online sources
#include <vector>
#include <algorithm>

int largestPerimeter(std::vector<int>& nums) {
    std::sort(nums.begin(), nums.end(), std::greater<int>());

    for (size_t i = 0; i < nums.size() - 2; ++i) {
        if (nums[i] < nums[i + 1] + nums[i + 2]) {
            return nums[i] + nums[i + 1] + nums[i + 2];
        }
    }

    return 0;
}

To find the largest perimeter of a triangle with a non-zero area, we first sort the array in descending order. This allows us to check if a triangle can be formed by taking the longest possible sides first. Then, we iterate through the sorted array and check if the current element and the next two elements form a triangle of non-zero area. To check if a triangle can be formed, we use the triangle inequality theorem, which states that for any triangle with sides a, b, and c, the sum of any two sides must be greater than the third side (a + b > c, a + c > b, b + c > a). Since we have the array sorted in descending order, we only need to check one case of the inequality (nums[i] < nums[i + 1] + nums[i + 2]) for each iteration. If we find a valid triangle, we return the sum of its sides as the largest perimeter. If no valid triangle is found, we return 0.