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 <= 1000program 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
Answer: 871069698,-2124738402
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
temp.f95:22:4:
22 | n = n / 2
| 1
Error: Dummy argument βnβ with INTENT(IN) in variable definition context (assignment) at (1)
temp.f95:43:5:
43 | use binary_representation
| 1
Fatal Error: Cannot open module file βbinary_representation.modβ for reading at (1): No such file or directory
compilation terminated.
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.
#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.