- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2595-number-of-even-and-odd-bits.rb
40 lines (28 loc) · 906 Bytes
/
2595-number-of-even-and-odd-bits.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true
# 2595. Number of Even and Odd Bits
# https://leetcode.com/problems/number-of-even-and-odd-bits
=begin
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].
### Constraints:
* 1 <= n <= 1000
=end
# Runtime 89 ms, Beats 100%
# Memory 210.9 MB, Beats 100%
# @param {Integer} n
# @return {Integer[]}
defeven_odd_bit(n)
[(n & 341).digits(2).count(1),(n & 682).digits(2).count(1)]
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_even_odd_bit < Test::Unit::TestCase
deftest_
assert_equal[2,0],even_odd_bit(17)
assert_equal[0,1],even_odd_bit(2)
end
end