August 07, 2022

Given two non negative integers A and B, returns the number of bits set to 1 in java 8 | interview question

/**
 * Write a function that given two non negative integers A and B, returns the number of bits set to 1 in the binary representation of the number A * B
 */
package com.learning.leetcode;

/**
 * @author Biju perambra blogger
 * IHRD CAS kozhikode
 */
public class SolutionBijuApp {

public int solution(int A, int B) {
int count = 0;
int product = Math.multiplyExact(A, B);
while (product > 0) {
int value = product % 2;
product = product / 2;
if (value == 1)
count++;
}
return count;
}


/**
* @param args
*/
public static void main(String[] args) {

System.out.println(new SolutionBijuApp().solution(3, 7));


}

}


BijuApp on stockmarket

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments