fork(18) download
  1. <?php
  2.  
  3. echo json_encode(solution(json_decode(file_get_contents("php://stdin"))));
  4.  
  5. function solution($A)
  6. {
  7. $int = int_of_base_neg2($A);
  8. $neg_int = - $int;
  9. $result = base_neg2_of_int($neg_int);
  10.  
  11. return $result;
  12. }
  13.  
  14. function int_of_base_neg2($a)
  15. {
  16. $count = count($a);
  17. $result = 0;
  18. for($i = 0; $i < $count; $i ++) {
  19. $result += $a[$i] * pow(-2, $i);
  20. }
  21.  
  22. return $result;
  23. }
  24.  
  25. function base_neg2_of_int($n)
  26. {
  27. $result = [];
  28. while($n !== 0)
  29. {
  30. $remainder = (int) ($n % -2);
  31. $n = (int) ($n / - 2);
  32.  
  33. if ($remainder < 0)
  34. {
  35. $remainder += 2;
  36. $n += 1;
  37. }
  38.  
  39. $result[] = $remainder;
  40. }
  41.  
  42. return $result;
  43. }
  44.  
Success #stdin #stdout 0.02s 24400KB
stdin
[1,0,0,1,1]
stdout
[1,1,0,1]