-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary_Parity.java
More file actions
21 lines (20 loc) · 826 Bytes
/
Binary_Parity.java
File metadata and controls
21 lines (20 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t, n;
t = sc.nextInt();
while (t-- > 0) {
// Map < Integer, Integer > map = new HashMap < > ();
n = sc.nextInt();
String binaryBoi = Integer.toBinaryString(n);
int ans = 0;
for (int i = 0; i < binaryBoi.length(); i++) {
char c = binaryBoi.charAt(i);
if (c == '1') ans += 1;
}
sout(ans % 2 == 0 ? "Even" : "Odd");
}
}
// we need to convert the given number to binary first so i used in built function to get binary of number in string and then
// traversed throught that string and added 1 to sum of binary string if current character is '1' and check the sum parity
// and printed answer accordingly