Advertisement
joespi

Untitled

Aug 3rd, 2021
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. // Solución utilizando binaryParse y un arreglo binario.
  5. class Solution {
  6.     public int solution(int A, int B) {
  7.         int calculo = A * B;
  8.  
  9.         String binaryParse = Integer.toString(calculo,2); // base2 me permitirá convertirlo a binario al resultado del cálculo.
  10.         String[] binaryArray = binaryParse.split("");
  11.  
  12.         int resultado = 0;
  13.  
  14.         // Si cada objeto del arreglo binario es 1, entonces sumo al resultado.
  15.         for (String a : binaryArray){
  16.             if(a.equals("1")){
  17.                 resultado++;
  18.             }
  19.         }
  20.  
  21.         return resultado;
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement