PROBLEM LINK:
Author: Praji Benerjee
Tester: Md Shahid
Editorialist: Md Shahid
DIFFICULTY:
Cakewalk
PREREQUISITES:
Bits
PROBLEM:
Given any positive number N and you need to find total number of unset bits
EXPLANATION:
This problem is very easy. First of all convert the number N into string of binary numbers( 0 or 1). Now count the number of zeros from binary form. For example, you have number N=10, binary form of this number will be S="1010", now count number of zeros i.e, 2 so answer will be 2.
Time Complexity :
Converting a decimal number into binary takes O(logN) time.
SOLUTIONS:
Author's Solution
#include<bits/stdc++.h>
#define int long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
signed main(){
fast
int t ;cin>>t;
while(t--){
int n; cin>>n;
int ans=__builtin_popcount(n);
int total=(int)log2(n)+1;
int nigga=total-ans;
cout<<nigga<<endl;
}
return 0;
}
Tester & Editorialist Solution
for _ in range(int(input())):
N = int(input())
b = bin(N)
print(b.count('0')-1)
All the suggestions are welcome.