Mouamle

hackathon quizzz

Feb 8th, 2019
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. package hackathon;
  2.  
  3. /**
  4.  * @author mouamle & ameer
  5.  * @version 0.1
  6.  */
  7. public class First {
  8.  
  9.     public static void main(String[] args) {
  10.         System.out.print(getPos(309));
  11.         System.out.print(getPos(701));
  12.         System.out.print(getPos(5353));
  13.     }
  14.  
  15.     static int getPos(int n) {
  16.         int pow = closePow(n);
  17.  
  18.         if (checkPow(n)) {
  19.             return 1;
  20.         }
  21.  
  22.         return (n - pow) * 2 + 1;
  23.     }
  24.  
  25.     static boolean checkPow(int number) {
  26.         return number > 0 && ((number & (number - 1)) == 0);
  27.     }
  28.  
  29.  
  30.     static int closePow(int v) {
  31.         v--;
  32.         v |= v >> 1;
  33.         v |= v >> 2;
  34.         v |= v >> 4;
  35.         v |= v >> 8;
  36.         v |= v >> 16;
  37.         v++;
  38.         v >>= 1;
  39.         return v;
  40.     }
  41.  
  42.  
  43. }
Add Comment
Please, Sign In to add comment