Killjee is solving an easy problem. Given an array containing positive integers. You need to find the magic value of the array.
Magic value of an array is . where, is number of set bits of .
Find and print the answer Modulo
INPUT CONSTRAINTS
INPUT FORMAT
First line of input contains a single integer . Next line contains space separated integers, elements of array .
First line of input contains a single integer . Next line contains space separated integers, elements of array .
OUTPUT FORMAT
Output a single integer, magic value of the array.
Output a single integer, magic value of the array.
Explanation
and so, summation of = .
Note: Your code should be able to convert the sample input into the sample output. However, this is not enough to pass the challenge, because the code will be run on multiple test cases. Therefore, your code must solve this problem statement.
// JAVA Solution
import java.util.Scanner;
public class CountSetBitsInInteger {
public static void main(String[] args) {
int b=0;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i0){
if(number%2 == 1){
counter++;
}
number = number/2; //or number = number >> 1
}
return counter;
}
}
Comments
Post a Comment