Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hackathon;
- /**
- * @author mouamle & ameer
- * @version 0.1
- */
- public class First {
- public static void main(String[] args) {
- System.out.print(getPos(309));
- System.out.print(getPos(701));
- System.out.print(getPos(5353));
- }
- static int getPos(int n) {
- int pow = closePow(n);
- if (checkPow(n)) {
- return 1;
- }
- return (n - pow) * 2 + 1;
- }
- static boolean checkPow(int number) {
- return number > 0 && ((number & (number - 1)) == 0);
- }
- static int closePow(int v) {
- v--;
- v |= v >> 1;
- v |= v >> 2;
- v |= v >> 4;
- v |= v >> 8;
- v |= v >> 16;
- v++;
- v >>= 1;
- return v;
- }
- }
Add Comment
Please, Sign In to add comment