Number of Even and Odd Bits

🏠 ⬅️ ➑️

You are given a positive integer n.

Let even denote the number of even indices in the binary representation of n (0-indexed) with value 1.

Let odd denote the number of odd indices in the binary representation of n (0-indexed) with value 1.

Return an integer array answer where answer = [even, odd].

Example 1:

Input: n = 17 Output: [2,0] Explanation: The binary representation of 17 is 10001. It contains 1 on the 0th and 4th indices. There are 2 even and 0 odd indices.

Example 2:

Input: n = 2 Output: [0,1] Explanation: The binary representation of 2 is 10. It contains 1 on the 1st index. There are 0 even and 1 odd indices.

Constraints:

  • 1 <= n <= 1000

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

    integer, parameter :: n = 17
    integer :: answer(2)

    call solve(n, answer)

    write (*, '(A, I0, A, I0)') 'Answer: ', answer(1), ',', answer(2)

contains

subroutine solve(n, answer)
    implicit none
    integer, intent(in) :: n
    integer, intent(out) :: answer(:)

    integer :: i, j, k, l, m, n_bits
    logical :: found

    n_bits = floor(log(real(n)) / log(2.0)) + 1

    do i = 1, n_bits
        if (mod(i, 2) == 0) then
            ! Even index
            found = .false.
            do j = 1, n_bits
                if (mod(j, 2) == 0) then
                    ! Even bit
                    if (mod(n, 2) == 1) then
                        ! Bit is set
                        found = .true.
                        exit
                    end if
                else
                    ! Odd bit
                    if (mod(n, 2) == 0) then
                        ! Bit is set
                        found = .true.
                        exit
                    end if
                end if
            end do
            if (found) then
                answer(1) = answer(1) + 1
            end if
        else
            ! Odd index
            found = .false.
            do j = 1, n_bits
                if (mod(j, 2) == 0) then
                    ! Even bit
                    if (mod(n, 2) == 1) then
                        ! Bit is set
                        found = .true.
                        exit
                    end if
                else
                    ! Odd bit
                    if (mod(n, 2) == 0) then
                        ! Bit is set
                        found = .true.
                        exit
                    end if
                end if
            end do
            if (found) then
                answer(2) = answer(2) + 1
            end if
        end if
    end do
end subroutine solve

end program main
❌ Compiled
❌ Executed
❌ Correct
module binary_representation

implicit none

contains

function get_even_odd_indices(n) result(answer)

integer, intent(in) :: n
integer :: answer(2)

! Declare variables
integer :: i, j, k, l, m, n_binary(1000), even, odd

! Initialize variables
even = 0
odd = 0

! Convert n to binary
do i = 1, 1000
    n_binary(i) = mod(n, 2)
    n = n / 2
end do

! Count even and odd indices
do j = 1, 1000
    if (n_binary(j) == 0) then
        even = even + 1
    else
        odd = odd + 1
    end if
end do

! Return answer
answer = [even, odd]

end function get_even_odd_indices

end module binary_representation

program main

use binary_representation

implicit none

! Declare variables
integer :: n, answer(2)

! Read input
read *, n

! Call function
answer = get_even_odd_indices(n)

! Print output
write (*, *) answer

end program main
🌐 Data from online sources
def even_odd_indices(n: int) -> List[int]:
    answer = [0, 0]
    index = 0
    while n > 0:
        if n % 2:
            answer[index % 2] += 1
        n //= 2
        index += 1
    return answer

The algorithm iterates through the bits of the input number n one by one. For each bit, it checks whether the bit is set (1) or not. If the bit is set and its index (0-indexed) is even, it increments the even count in answer. If the bit is set and its index is odd, it increments the odd count in answer. The algorithm continues until all bits of the number have been processed. Finally, it returns the answer array containing the counts of even and odd indices with the value of 1 in the binary representation of n.

🌐 Data from online sources
#include <vector>

std::vector<int> evenOddIndices(int n) {
    std::vector<int> answer(2, 0);
    int index = 0;
    while (n > 0) {
        if (n % 2)
            answer[index % 2] += 1;
        n /= 2;
        index += 1;
    }
    return answer;
}

The algorithm iterates through the bits of the input number n one by one. For each bit, it checks whether the bit is set (1) or not. If the bit is set and its index (0-indexed) is even, it increments the even count in answer. If the bit is set and its index is odd, it increments the odd count in answer. The algorithm continues until all bits of the number have been processed. Finally, it returns the answer array containing the counts of even and odd indices with the value of 1 in the binary representation of n.