Number Complement | Multiple Approaches | Leetcode 476 | codestorywithMIK

preview_player
Показать описание
This is the 52nd Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve an easy problem : Number Complement | Multiple Approaches | Leetcode 476 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Number Complement | Multiple Approaches | Leetcode 476 | codestorywithMIK
Company Tags : Cloudera

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
Approach 1 (Using bit magic of XOR):

This approach calculates the total number of bits required to represent the given number num and then flips each bit one by one using the XOR operation with 1. This flips the bits, effectively giving the complement.
Time Complexity: O(log₂(num))
Space Complexity: O(1)
Approach 2 (Using XOR mask):

This method creates a mask with all bits set to 1 that matches the bit length of num. Then, it XORs num with this mask, flipping all bits to get the complement. The mask is generated by left-shifting and OR-ing bits until it covers all bits of num.
Time Complexity: O(log₂(num))
Space Complexity: O(1)
Approach 3 (Iterate digit by digit and create complement):

In this approach, you iterate through the bits of num and construct the complement bit by bit. If the current bit of num is 0, you set the corresponding bit in the result to 1. This continues until all bits are processed.
Time Complexity: O(log₂(num))
Space Complexity: O(1)
Each approach leverages different bit manipulation techniques to compute the complement efficiently.

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
Рекомендации по теме
Комментарии
Автор

reached knight because of you thank you mazhar bhaiya...

DevanshGupta-iorl
Автор

Bro..Your clear and concise explanation made it a breeze to understand

Code_eicher
Автор

THANK YOU SO MUCH BHAIYA ....AAP BAHUT ACHHA EXPLAIN KRTE HAIN

shrutichouksey
Автор

Loved the explanation. I always get to learn something from here.
Still wondering why this channel is so underrated

aws_handles
Автор

you are too good in explaination and logic building.
Thank you so much sir.

prathamkaushal
Автор

Thanks a lot bhaiya ❤❤
The new thumnail style looks good 🔥🔥

gauravbanerjee
Автор

thanks, bhai. for discussing multiple approaches to this question, if possible, please make videos on the daily challenge of leetcode also which is really helpful for us.

hydra_akyt
Автор

Wow. Never underestimating easy problems

hypewaali
Автор

Thanks bhai. for all your hardwork. People like me with ADHD find it really difficult to problem solve & your work helps us alot.

chaosNinja
Автор

crystal clear. noted all the important points.
Also thumbnail is good

EB-otuu
Автор

Please start giving follow up questions after every question, that will help alot

abhishekagrahari
Автор

My initial approach was finding the nearest power of 2 (strictly larger than input), and return the difference - 1. (not using binary in any way at all) and it worked

Desmnd
Автор

this is my solutiion, thank you for making such videos and teaching us to slove questions in multiple way..(My code doesn't look good)

class Solution {
public:
int binaryToInt(int n){
return (n, nullptr, 2);
}
int findComplement(int num) {
int ans=0;
string bin="";
string rev ="";
if(num==1) return 0;
while(num>0){
int temp = num%2;
num = num/2;
bin = to_string(temp);
}
reverse(bin.begin(), bin.end());
for(int i =0;i<bin.size();i++){
if(bin[i]=='1') rev+="0";
else rev+="1";
}
ans = stoi(rev);
int ans1 = binaryToInt(ans);
return ans1;
}
};

gyaneshwar
Автор

Pls bring a playlist on Bit Manipulation.

molyoxide
Автор

today's gfg potd is good, Alien dictoary, perhapos you can make a video in the future...

aizadiqbal
Автор

i used this...

class Solution {
public int findComplement(int num) {
int ans = 0, check=0;
int pow=1;
for(int i=0; i<32; i++){
if((num & (1 << i)) == 0){
// current bit is 0, so flip would be 1
ans += pow;
}
else{
check += pow;
}
if(check == num){
break;
}
pow = pow*2;
}
return ans;
}
}

aizadiqbal
Автор

solutions ko complicate karne ki habbit hai
class Solution {
public:
int findComplement(int num) {
vector<int> temp;
for(int i = 30; i >= 0; i--) {
temp.push_back(1 << i);
}

string s = "";
int x = num;
int i = 0;

while (i < 31 && temp[i] > x) {
i++;
}

x -= temp[i];
s.push_back('1');

if (x == 0) {
int a = 30 - i;
for (int j = 0; j < a; j++) {
s.push_back('0');
}
} else {
while (x != 0 ) {
i++;
if (temp[i] > x) {
s.push_back('0');
} else {
s.push_back('1');
x -= temp[i];
}
}
while(i!=30){
s.push_back('0');
i++;
}
}

int e = 30;
int n = s.length();
int ans = 0;

for (int j = n - 1; j >= 0; j--) {
if (s[j] == '0') {
ans += temp[e];
}
e--;
}

return ans;
}
};

suryapratap
Автор

1551.Minimum operations to make Array Equal
Can you make a video for this I have encountered many Question like this
But don't understand what to do
There are many other question like this but I don't understand from how to start what is the approach
Can you simply and also tell the general approach for solving this type of question please.

ayushigholase
Автор

Sir "4SUM" wali problem ka solution le ao please.. Mera pura questions solve ho gaya h bas target naya define kyu kiya usmein dikkat a rhi h.

MY CODE:

class Solution {
public:
void foursum(vector<int>& nums, vector<vector<int>>& ans,
int i, int j, long long e1, long long e2) {

long long target = -(e1 +e2);

while (i < j) {
long long sum = nums[i] + nums[j];

if (sum == target) {
ans.push_back({(int)e1, (int)e2, nums[i], nums[j]});
i++;
j--;

while (i < j && nums[i] == nums[i - 1]) {
i++;
}
while (i < j && nums[j] == nums[j + 1]) {
j--;
}
}
else if (sum < target) {
i++;
} else {
j--;
}
}
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {

// ans store kara
vector<vector<int>> ans;
// imp and foremost
int n = nums.size();
// check less than 4 elements
if (n < 4) {
return ans;
}
// sorting(Elements chaiye index nhi)
sort(nums.begin(), nums.end());

// do elements ko fix kardo
for (int i = 0; i < n - 3; i++) {

// Duplicate for fixed element
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}

for (int j = i + 1; j < n - 2; j++) {

if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}

long long e1 = nums[i];
long long e2 = nums[j];
foursum(nums, ans, j + 1, n - 1, e1, e2);
}
}

return ans;
}
};

hellsterkun
Автор

bro can you please explain leetcode 3241

m_.bsrikanth
welcome to shbcf.ru