Advertisement
Guest User

RakutenTest

a guest
Feb 20th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /**
  2. * Created by lidiakulikova on 21/02/2018.
  3. */
  4. public class Binarian {
  5.  
  6. public static int getBinarian(int[] A){
  7. int total = 0;
  8. for (int x : A){
  9. total += (int)Math.pow(2, x);
  10. }
  11. return total;
  12. }
  13.  
  14.  
  15. public static int solution(int[] A) {
  16. // write your code in Java SE 8
  17. int k = getBinarian(A);
  18. int remainder = k;
  19. int count =0;
  20. while (remainder >= 2){
  21. double c = Math.log(remainder) / Math.log(2);
  22. remainder = remainder - (int)Math.pow(2, (int) c);
  23. count += 1;
  24. }
  25. if (remainder == 1){
  26. count +=1; //2^0
  27. }
  28. return count;
  29. }
  30.  
  31. public static void main(String[] args){
  32. System.out.println(solution(new int[]{2, 2}));
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement