forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
33 lines (30 loc) · 1.09 KB
/
Solution.java
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
//Problem: https://www.hackerrank.com/challenges/sum-vs-xor
//Java 8
/*
Initial Thoughts: Brute force would be to check the condition
for all numbers between 0 -> n and keep a
counter of which ones satisfied it. To make
this faster, we can simply count the number
of zeros after converting n to a binary number.
Time Complexity: O(n log(n)) //It takes n log(n) time to convert to binary using two's division
Space Complexity: O(1) //There is no ddynamically allocated variables
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long count = 0;
//This performs a basic conversion from int to binary using divide by two and checking even or odd
while(n != 0){
count += (n%2 == 0)?1:0;
n/=2;
}
count = (long) Math.pow(2,count);
System.out.println(count);
}
}