Skip to content

Commit

Permalink
Create 1611. Minimum One Bit Operations to Make Integers Zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Nov 30, 2023
1 parent 8d2d709 commit 5bfbbea
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 1611. Minimum One Bit Operations to Make Integers Zero
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
int minimumOneBitOperations(int n) {
int ind=0;
vector<int> ans;
while(n!=0){
if((n&1)==1){
int temp=pow(2,ind);
temp=temp*2;
temp--;
ans.push_back(temp);
}
ind++;
n=n>>1;
}
int res=0;
reverse(ans.begin(),ans.end());
bool flag=true;
for(auto it:ans){
if(flag){
res+=it;
flag=false;
}
else {
res-=it;
flag=true;
}
}
return res;
}
};

0 comments on commit 5bfbbea

Please sign in to comment.