Tomorrow's quotes from my side: “Every single day you waste wishing things were easier, someone else is grinding to take what you’re dreaming of. Success doesn’t wait for anyone. Either you step up now, or you’ll be left behind forever.”
Aj ka question bhi instantly ban gya bhaiya. Mai to believe hi nhi ker paa rha hu. 1 mahina pahle khudse nhi bana pta tha thik se. Aur ab dekhte hi intuition aa jaa rha hai. Thanks a lot bhaiya ❤❤ A long way to go. Today I did slightly different solution using visited hashmap. Pahle count == 2 wala hi aya tha dimag me. phir ye wala click kiya. vector findThePrefixCommonArray(vector& A, vector& B) { int n = A.size(); vector visited(n + 1, false); vector result(n, 0); int count = 0; for(int i=0; i< n; ++i){ if(visited[A[i]]){ count++; }else{ visited[A[i]] = true; } if(visited[B[i]]){ count++; }else{ visited[B[i]] = true; } result[i] = count; } return result;
Thank you sir!! I first stopped the video in 2 mins to think and I use sets and intersection as 1st approach and after seeing bool array then paused and optimized it O(n). the set approach and boolean approach are below: set -- class Solution { public int[] findThePrefixCommonArray(int[] A, int[] B) { int n = A.length; int[] res = new int[n]; Set a = new HashSet(); Set b = new HashSet(); for(int i=0; i
i have come up with a similiar kind of solution as you discussed in approach 3.. only difference is that it uses the concept of visited array, since it is mentioned in ques that range of nos is from 1 to n.class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n = A.size(); vectorvis(n+1,0); vectorans(n); int cnt=0; for(int i=0;i
Bhaiya, I Solved it with O(50) space complexity and O(50)time complexity Only Because of You ❤, Thankyou so much for all the efforts you are making for us.
class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n = A.size(); vector v1(n+1,-1); vector ans; for(int i = 0; i < n ; i++){ v1[A[i]]++; v1[B[i]]++;
int cnt = 0; for(int i = 0; i < n+1; i++ ){ if(v1[i]==1)cnt++; } ans.push_back(cnt); } return ans; } };
sir this is how i solved this question : class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n = A.size(); vectorans(n,0); unordered_setst;
Thank you so much bhaiyya.. I tried to solve by own but bhot jagah phasss rha tha then aapke paas aya.. My 2 obervation in this problem was - 1. res[0] will always be either 0 or 1 2. res[n-1] will always = n Even though ye observation kuch kaam nahi aaye question solve krne me but ye ek mind me tha bs... Thank you bhaiyya.
I liked that. 2nd observation also came to my mind that it will always be = n However it could only do a small optimisation that we won’t run the loop till n-1 And in the end we will populate res[n-1] = n but that won’t be a very big optimisation. But it’s good to know you also came to think of it ❤️❤️
Mik bhaiya want 2 things we the students are requesting u apka samjaya hua pura thought process dimag me jata hai even kabhi pehla consistent nhi tha apka subha motivation and then video thnku for all this but 1 apki sde sheet 2 dsa pattern recognition ispr video laao 3 for beginners to advance dsa course again ye app pr hai ki app launch karna chahta ho kuki honestly apka jesa ytber mene nhi dekha if u will for course jisme sabkuch included i will be infact bahut log course ke member hoga Again thanku if possible for u then it will be great help as u travel daily firbhi itna quality teaching content thought process salute u men 🫡
I did something similar for optimal approach . but i used a visited set that will check whether the element was present or not and if the element is present then we increase count .here is the code class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n=A.size(); unordered_set st; vectorres(n,0); int count=0; for(int i=0;i
My approach :- Find No of unique element till i'th index using set and subtract it by total no of elements till index i by combining both array A and B. class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { unordered_set st; int size=A.size(); vector result(size); for(int i=0;i
/*APPROACH-1 , WE TOOK 2 BOOLEAN ARRAYS, FOR EACH INDEX 1) mark A[i]= true mark B[i]=true; 2) now from the starting check if both boolean arrays are marked true; -> if yes , increase the count -> if no, eat five star */ class Solution2 { public int[] findThePrefixCommonArray(int[] A, int[] B) { int n= A.length; boolean isPresentA[]= new boolean[n+1]; boolean isPresentB[]= new boolean[n+1]; int C[]= new int[n]; for(int i=0;i
I solved it myself using unordered set , my approach: class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n = A.size(); unordered_set visited; unordered_set seen; vector result(n, 0); for (int i = 0; i < n; i++) { if (seen.count(A[i])) visited.insert(A[i]); else seen.insert(A[i]); if (seen.count(B[i])) visited.insert(B[i]); else seen.insert(B[i]); result[i] = visited.size(); } return result; } };
I have done it with the help of a single unordered set and with a time complexity of O(n) .Here's my code:- class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { unordered_set mp; vector res; int cnt=0; for(int i=0;i
Solved this problem using O(n^2) but come here to learn most optimised approach and learnt it. Thanks. Can you also explain the BITS approach for this?
Here is my Approach T.C = O(N) S.C = O(51) ~ O(1) class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { vectormp(51,0); int n = A.size(); vectorC(n); int common = 0; for(int i = 0; i < n; i++) { if(A[i] == B[i]) { common++; } else{ mp[A[i]]++; if(mp[A[i]] == 2) common++; mp[B[i]]++; if(mp[B[i]] == 2) common++; } C[i] = common; } return C; } }; Solved by myself thank you sir
How about this solution class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n = A.size(); set seen; vector rs(n, 0); int commonCount = 0;
for (int i = 0; i < n; i++) { commonCount=commonCount+2; seen.insert(A[i]); seen.insert(B[i]); rs[i] = commonCount-seen.size(); } return rs; } };
My diff approach tried my own: TC:O(n) SC:O(n) public int[] findThePrefixCommonArray(int[] A, int[] B) { boolean[] aTable=new boolean[51]; boolean [] bTable=new boolean[51]; int[] ans=new int[A.length]; int i=0,count=0; while(i
Sir I got confused while reading that question, I was thinking what count he was asking? wehn you explained the example you said the count of "common" numbers in a and b before or at index i tab smj aaya. Valid doubt hai kya sir, if not then please clarify where I misunderstood the question please.
No that’s totally fine. Even during interviews, you can ask them to explain the problem with an example. They always give an example to explain. Don’t worry, the problem statement was not that good. It was indeed a little confusing.
@@codestorywithMIK That's a relief! Thanks a lot. I solved it myself using this method right after understanding question, will see the video for optimal after trying few improvements
got optimal approach on my own ..thank u MIK sir. for these daily vdos.❣ but sir mene 1)ans.pushback(count) kia to beats 64% aaya or jb 2) ans[i]= count kia to beat 100% aaya pushback ki time complexit bhi O(n) nhi hoti??🫠
mik bhaiya ,did it by myslef but space complexity jayada ho gayi thodi , my solution:- class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { unordered_setst1; unordered_setst2; vectorC; int count=0; for(int i=0;i
1)2 sets -> vector arr...arr.first for s1 and second for set s2... 2) c.pushabck ki jagah c[i] = count krke dekho ...( mere me bhi yhi pushback se dikat thi)
Bhaiya,mere mind m Direct optimized app. Aya question samjhte hi....but failed to code.....ese m interview m kya kre ..agar kisi question ka optimized solution hi click kre mind m at a instance
Hello bhaiya please reply me I hit the logic of the question but code nhi kr pta hu bhaiya kaise thik kr iss phase please help kr dijiye isme bhaiya mai Mtlb bhaiya ye phase ko kaise deal kr ..
bhai pehle easy qns ko implement karo C++ me. Kahi stuck ho to use chatgpt and then see where you went wrong, then correct karo. aise try kar kar ke hi you will improve bro. perfection takes time and a lot of practice. start karo, slowly slowly mistake karte karte khud seekh jaoge
Focus on mastering the basics of C++ syntax, such as loops, functions, and STL (Standard Template Library), as these are the building blocks for DSA implementation. Start with simple problems and gradually move to more complex ones. Remember, logic is the hardest part, and since you already have that, implementation is just a matter of time and persistence. Stick with it, and you’ll get there
you can try implementation-based problems (A, B of Div.3 & 4 contests on Codeforces) to improve implementation skills. I also did this to improve my implementation skill.
When I saw the problem, I thought of iterating from the back and solving it in one iteration without using any extra space. Couldn't pass all the testcases. Is it possible to solve it without using extra space? Gave up and solved it using the last approach. Here is the Python implementation: class Solution: def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: n = len(A) freq, res = [0 for _ in range(n+1)], [0 for _ in range(n)] for i in range(n): cnt = 0 freq[A[i]] += 1 if freq[A[i]] == 2: cnt += 1 freq[B[i]] += 1 if freq[B[i]] == 2: cnt += 1 res[i] = res[i-1]+cnt if i>0 else cnt return res
We can definitely solve it in O(1) space. Instead of map or set for storing elements, we can use a bit mask for marking element’s corresponding bit position in the mask. You can try. If I get some time this week, I will try to put a video on it ❤️🙏
@@codestorywithMIK Thanks, could solve it using bit mask. Here is the Python implementation: class Solution: def findThePrefixCommonArray(self, A: list[int], B: list[int]) -> list[int]: n = len(A) mask_a, mask_b = 0, 0 res = [0] * n for i in range(n): mask_a |= (1
idk y, using arrays gives a faster response just leetcode things.. class Solution { public int[] findThePrefixCommonArray(int[] A, int[] B) { int n = A.length; int[] result = new int[n]; int[] freq = new int[n+1]; int count = 0; for(int i = 0; i < n; i++) { freq[A[i]]++; if(freq[A[i]] == 2) { count++; } freq[B[i]]++; if(freq[B[i]] == 2) { count++; } result[i] = count; } return result; } }
Yes, you're absolutely correct! Using arrays in C++ is generally faster than other data structures like std::vector or std::list in many scenarios. Arrays are allocated on the stack (for fixed sizes), which is much faster compared to dynamic allocation. Even when dynamically allocated, arrays have a simpler memory layout compared to std::vector. Arrays don't have the overhead of extra features like dynamic resizing or bounds checking, which std::vector provides. Arrays have contiguous memory allocation, making them more cache-friendly. This leads to better performance due to spatial locality And many more reasons make Array faster ❤️😇
I would like to share 1 more approach whose time complexity is O(n) and auxiliary space is O(1). Following is the code - class Solution: """ Time Complexity: O(n) where n is length of array 'A' Auxiliary Space: O(1) """ def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: n = len(A) space = [0]*51 count = [0]*n for i in range(n): i1 = A[i] i2 = B[i] space[i1] += 1 space[i2] += 1 currrent_count = 0 for j in range(51): if space[j] == 2: currrent_count += 1 count[i] = currrent_count return count
class Solution { public int[] findThePrefixCommonArray(int[] A, int[] B) { int []c= new int[A.length]; int n=A.length; HashSet set1= new HashSet(); HashSet set2= new HashSet(); if(A[0]!=B[0])c[0]=0; else c[0]=1; set1.add(A[0]); set2.add(B[0]); for(int i=1;i
how approach-3 is working. I wrote the same type of code but when A[i]==B[i] it will increase extra count so we have to add one extra if condition class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n=A.size(); vector hash(n+1,0); vector result; int currCommon=0; for(int i=0;i
bhai honestly batau to, start studying concepts of the topics first. for example : array, map, set, tree, linkedlist, dp, graph etc. Then solve easy qns, then move to medium slowly and so on. Practice as many qns as possible jitna qns bana sakte ho starting me banao. it will help a lot
I did it in below approach. Is the really efficient ?? class Solution { public int[] findThePrefixCommonArray(int[] A, int[] B) { int n = A.length; int[] freq = new int[n+1]; int[] ans = new int[n]; for(int i=0;i
Bhai aap samjhate ho toh bahut ache se samajh aajata hai kabhi kabhi sirf explaination se mai code bhi kardeta hu par khud se ye logic develop hi nhi hota...i have just started leetcode please help !!!
If you have just started leetcode, then you are doing good man. Keep practicing, with time it will all come. Mai bhi intermediate level ka dsa karleta hu, pehle Easy bhi nahi hote the mujhse. Ab practice karte karte hone laga hai Medium bhi. But it takes time and practice bro
Thank you for your kind words! I understand that developing logic can feel tough initially, but don't worry-it's a skill that improves with consistent practice. Here's a structured way to approach DSA: Pick one topic at a time (e.g., Arrays, Strings, Stacks, etc.). Start with 10-15 easy problems to build your understanding and confidence. Move to 7-10 medium problems to strengthen your logic and problem-solving. Finally, try 2-3 hard problems to challenge yourself. Once you feel comfortable with a topic, move on to the next one while revisiting older topics occasionally to reinforce your learning. Always try solving problems on your own first before looking at explanations. Break problems into smaller steps, use pen and paper to visualize, and analyze why a solution works. Practice consistently on platforms like LeetCode, and you'll see steady improvement. Remember, persistence and patience are the key!
My approach using set and traversing from backwards class Solution { public: vector findThePrefixCommonArray(vector& A, vector& B) { int n=A.size(); vectorresult(n,0); unordered_setst; for(int i=1;i=0;i--){ if(A[i]==B[i]){ result[i]=st.size(); st.erase(A[i]); } else{ result[i]=st.size(); st.erase(A[i]); st.erase(B[i]); } } return result; } };
Tomorrow's quotes from my side: “Every single day you waste wishing things were easier, someone else is grinding to take what you’re dreaming of. Success doesn’t wait for anyone. Either you step up now, or you’ll be left behind forever.”
कर्मण्येवाधिकारस्ते मा फलेषु कदाचन।
मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि॥ Tomorrow's quotes from my side -- motivation in sanskrit 🥰🥰🥰
1:27 - Hell Yeahhhhhhh. "It's Me vs Me" 🔥🔥🔥🔥
Ekdm josh ajata hai sir aapke motivation sunkar. THanks a lot
Thank you MIK and Congratulations 🎊 it's 80k strong community now!
Thank you so much Bhaiya ji 🙏🙏🙏
Aj ka question bhi instantly ban gya bhaiya. Mai to believe hi nhi ker paa rha hu. 1 mahina pahle khudse nhi bana pta tha thik se. Aur ab dekhte hi intuition aa jaa rha hai. Thanks a lot bhaiya ❤❤ A long way to go.
Today I did slightly different solution using visited hashmap. Pahle count == 2 wala hi aya tha dimag me. phir ye wala click kiya.
vector findThePrefixCommonArray(vector& A, vector& B) {
int n = A.size();
vector visited(n + 1, false);
vector result(n, 0);
int count = 0;
for(int i=0; i< n; ++i){
if(visited[A[i]]){
count++;
}else{
visited[A[i]] = true;
}
if(visited[B[i]]){
count++;
}else{
visited[B[i]] = true;
}
result[i] = count;
}
return result;
}
Thanks aise hi java me continue rakhiye
Thank you sir!!
I first stopped the video in 2 mins to think and I use sets and intersection as 1st approach and after seeing bool array then paused and optimized it O(n). the set approach and boolean approach are below:
set --
class Solution {
public int[] findThePrefixCommonArray(int[] A, int[] B) {
int n = A.length;
int[] res = new int[n];
Set a = new HashSet();
Set b = new HashSet();
for(int i=0; i
Done using my own approach without watching your video, thanks mike for the confidence
Done this problem using O(n^2) , thanks for providing more optimised approch. Your explanation is really awesome
ooo i didnt realise i too have did it in optimal approach 🥳🥳🥳
Third Approach with O(n) is Mind Opening Approach
Nice 👍
Woow mza aa gya😇
Completed by myself buddy, Very thanks for the intuition for these type of question.
I did it in O(n²) TC, without seeing soln and code, kudos to you MIK sir👏
Did O(n) soln also
completed the problem in O(n) but still came to know other appoaches to improve my knowledge .
Bhaiya, thank you for making me more consistent!! Love your motivation and problem-solving approach!!😇😀
Thanks for also coding in java, please continue the same and keep up the good work, learning a lot from you.
Thanks a lot MIK. Mai 3rd approach k lie aya tha. Goat ho aap.
Esy one today also❤ thanks sir for your teachings
class Solution {
public:
vector findThePrefixCommonArray(vector& a, vector& b) {
int n = a.size();
vector c(n,0), freq(52,0);
for(int i = 0; i
i have come up with a similiar kind of solution as you discussed in approach 3.. only difference is that it uses the concept of visited array, since it is mentioned in ques that range of nos is from 1 to n.class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n = A.size();
vectorvis(n+1,0);
vectorans(n);
int cnt=0;
for(int i=0;i
Solved it Optimally in O(N) TC on my own using unordered_set Data Structure!! Thanks to you for helping me build intuition and analyse patterns!!
yay! 80k soon🥳🤯🥳
Bhaiya, I Solved it with O(50) space complexity and O(50)time complexity Only Because of You ❤, Thankyou so much for all the efforts you are making for us.
Happy Makar Sankranti mik bhaiya❤❤❤
Happy Makar Sakranti 😇🙏❤️🎉🎉
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n = A.size();
vector v1(n+1,-1);
vector ans;
for(int i = 0; i < n ; i++){
v1[A[i]]++;
v1[B[i]]++;
int cnt = 0;
for(int i = 0; i < n+1; i++ ){
if(v1[i]==1)cnt++;
}
ans.push_back(cnt);
}
return ans;
}
};
Thank you for the motivation
sir this is how i solved this question :
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n = A.size();
vectorans(n,0);
unordered_setst;
if(A[0] == B[0]){
st.insert(A[0]);
ans[0] = 1;
}
else{
st.insert(A[0]);
st.insert(B[0]);
}
for(int i=1;i
Thank you so much bhaiyya.. I tried to solve by own but bhot jagah phasss rha tha then aapke paas aya..
My 2 obervation in this problem was -
1. res[0] will always be either 0 or 1
2. res[n-1] will always = n
Even though ye observation kuch kaam nahi aaye question solve krne me but ye ek mind me tha bs...
Thank you bhaiyya.
I liked that. 2nd observation also came to my mind that it will always be = n
However it could only do a small optimisation that we won’t run the loop till n-1
And in the end we will populate res[n-1] = n but that won’t be a very big optimisation.
But it’s good to know you also came to think of it ❤️❤️
Thank you...
Thanks bhaiya
Today I solved this problem on my own in an optimized way without watching any tutorial
Here to support you man.
I did with O(n^2) on my own. Then some mistakes k baad, O(n) me karliya.
yayyyyyyy 🤩
Credit goes to you MIK
MIK sir it's my request if it 's possible than please upload the explanation video of Leetcode contest questions, it will help us alot. Thank you
th-cam.com/video/B5aIqLq50_8/w-d-xo.htmlsi=PTJMTu2Q8RwrmbP8
I will definitely try to squeeze more time to upload more ❤️🙏
Mik bhaiya want 2 things we the students are requesting u apka samjaya hua pura thought process dimag me jata hai even kabhi pehla consistent nhi tha apka subha motivation and then video thnku for all this but
1 apki sde sheet
2 dsa pattern recognition ispr video laao
3 for beginners to advance dsa course again ye app pr hai ki app launch karna chahta ho kuki honestly apka jesa ytber mene nhi dekha if u will for course jisme sabkuch included i will be infact bahut log course ke member hoga
Again thanku if possible for u then it will be great help as u travel daily firbhi itna quality teaching content thought process salute u men 🫡
SDE sheet and DSA pattern video are good suggestions.
But I don't think MIK will ever promote Paid course. He always teaches DSA for free.
Soon ❤️ will update on this
@@codestorywithMIK Thanks alottt sir😭😭❤️❤️❤️❤️
If sir starts leetcode contests soolutions toh usme main consistency aa jayegi
@@codestorywithMIK also cpp oops playlist 🤗🤗
I did something similar for optimal approach . but i used a visited set that will check whether the element was present or not and if the element is present then we increase count .here is the code
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n=A.size();
unordered_set st;
vectorres(n,0);
int count=0;
for(int i=0;i
awesome...
My approach :- Find No of unique element till i'th index using set and subtract it by total no of elements till index i by combining both array A and B.
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
unordered_set st;
int size=A.size();
vector result(size);
for(int i=0;i
/*APPROACH-1 , WE TOOK 2 BOOLEAN ARRAYS, FOR EACH INDEX
1) mark A[i]= true mark B[i]=true;
2) now from the starting check if both
boolean arrays are marked true;
-> if yes , increase the count
-> if no, eat five star
*/
class Solution2 {
public int[] findThePrefixCommonArray(int[] A, int[] B) {
int n= A.length;
boolean isPresentA[]= new boolean[n+1];
boolean isPresentB[]= new boolean[n+1];
int C[]= new int[n];
for(int i=0;i
I solved it myself using unordered set , my approach:
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n = A.size();
unordered_set visited;
unordered_set seen;
vector result(n, 0);
for (int i = 0; i < n; i++) {
if (seen.count(A[i]))
visited.insert(A[i]);
else
seen.insert(A[i]);
if (seen.count(B[i]))
visited.insert(B[i]);
else
seen.insert(B[i]);
result[i] = visited.size();
}
return result;
}
};
Present sir ❤
I have done it with the help of a single unordered set and with a time complexity of O(n) .Here's my code:-
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
unordered_set mp;
vector res;
int cnt=0;
for(int i=0;i
This can be done using bit manipulation, can we have a video on that. This would be a good problem for bit manipulation practice
For O(n) solution the most important word is "permutation".
1:25 " Comparison se bhiya bs bdti h baicheni" -- Dino james
Solved this problem using O(n^2) but come here to learn most optimised approach and learnt it. Thanks. Can you also explain the BITS approach for this?
thanks bhai
Itna effort kaise dete ho sir aap har din. ek baar life me aapse zaroor milna hai mujhe
Bhaiya Please solve -->
3291. Minimum Number of Valid Strings to Form Target I
3292. Minimum Number of Valid Strings to Form Target II
My Brute Force is Optimal 😅....xd :)
Kindly Discuss BIt Manipulation Solution
Bhaiya please make a playlist on combinatorics concept and questions. Bahhto jarurat hai iski bhaiya
Here is my Approach
T.C = O(N)
S.C = O(51) ~ O(1)
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
vectormp(51,0);
int n = A.size();
vectorC(n);
int common = 0;
for(int i = 0; i < n; i++) {
if(A[i] == B[i]) {
common++;
}
else{
mp[A[i]]++;
if(mp[A[i]] == 2)
common++;
mp[B[i]]++;
if(mp[B[i]] == 2)
common++;
}
C[i] = common;
}
return C;
}
};
Solved by myself thank you sir
How about this solution class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n = A.size();
set seen;
vector rs(n, 0);
int commonCount = 0;
for (int i = 0; i < n; i++) {
commonCount=commonCount+2;
seen.insert(A[i]);
seen.insert(B[i]);
rs[i] = commonCount-seen.size();
}
return rs;
}
};
My diff approach tried my own: TC:O(n) SC:O(n)
public int[] findThePrefixCommonArray(int[] A, int[] B) {
boolean[] aTable=new boolean[51];
boolean [] bTable=new boolean[51];
int[] ans=new int[A.length];
int i=0,count=0;
while(i
Sir I got confused while reading that question, I was thinking what count he was asking? wehn you explained the example you said the count of "common" numbers in a and b before or at index i tab smj aaya. Valid doubt hai kya sir, if not then please clarify where I misunderstood the question please.
meri question understanding hi gandi hai bahut.
No that’s totally fine. Even during interviews, you can ask them to explain the problem with an example. They always give an example to explain. Don’t worry, the problem statement was not that good. It was indeed a little confusing.
@@codestorywithMIK That's a relief! Thanks a lot. I solved it myself using this method right after understanding question, will see the video for optimal after trying few improvements
Tomorrow's quotes from my side:""Motivation may fade, but discipline and obsession are the fuel that never run dry.""
got optimal approach on my own ..thank u MIK sir. for these daily vdos.❣
but sir mene
1)ans.pushback(count) kia to beats 64% aaya
or jb
2) ans[i]= count kia to beat 100% aaya
pushback ki time complexit bhi O(n) nhi hoti??🫠
mik bhaiya ,did it by myslef but space complexity jayada ho gayi thodi , my solution:-
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
unordered_setst1;
unordered_setst2;
vectorC;
int count=0;
for(int i=0;i
1)2 sets -> vector arr...arr.first for s1 and second for set s2...
2) c.pushabck ki jagah c[i] = count krke dekho ...( mere me bhi yhi pushback se dikat thi)
Bhaiya,mere mind m Direct optimized app. Aya question samjhte hi....but failed to code.....ese m interview m kya kre ..agar kisi question ka optimized solution hi click kre mind m at a instance
Hello bhaiya please reply me
I hit the logic of the question but code nhi kr pta hu bhaiya kaise thik kr iss phase please help kr dijiye isme bhaiya mai
Mtlb bhaiya ye phase ko kaise deal kr ..
bhai pehle easy qns ko implement karo C++ me. Kahi stuck ho to use chatgpt and then see where you went wrong, then correct karo. aise try kar kar ke hi you will improve bro. perfection takes time and a lot of practice. start karo, slowly slowly mistake karte karte khud seekh jaoge
Focus on mastering the basics of C++ syntax, such as loops, functions, and STL (Standard Template Library), as these are the building blocks for DSA implementation. Start with simple problems and gradually move to more complex ones.
Remember, logic is the hardest part, and since you already have that, implementation is just a matter of time and persistence. Stick with it, and you’ll get there
you can try implementation-based problems (A, B of Div.3 & 4 contests on Codeforces) to improve implementation skills. I also did this to improve my implementation skill.
Thanks to everyone and especially mik bhaiya
When I saw the problem, I thought of iterating from the back and solving it in one iteration without using any extra space. Couldn't pass all the testcases. Is it possible to solve it without using extra space? Gave up and solved it using the last approach. Here is the Python implementation:
class Solution:
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
n = len(A)
freq, res = [0 for _ in range(n+1)], [0 for _ in range(n)]
for i in range(n):
cnt = 0
freq[A[i]] += 1
if freq[A[i]] == 2:
cnt += 1
freq[B[i]] += 1
if freq[B[i]] == 2:
cnt += 1
res[i] = res[i-1]+cnt if i>0 else cnt
return res
We can definitely solve it in O(1) space. Instead of map or set for storing elements, we can use a bit mask for marking element’s corresponding bit position in the mask. You can try.
If I get some time this week, I will try to put a video on it ❤️🙏
@@codestorywithMIK Yes, I suppose we can use bit masking. Will try on my own and let you know.
@@codestorywithMIK Thanks, could solve it using bit mask. Here is the Python implementation:
class Solution:
def findThePrefixCommonArray(self, A: list[int], B: list[int]) -> list[int]:
n = len(A)
mask_a, mask_b = 0, 0
res = [0] * n
for i in range(n):
mask_a |= (1
So glad to you solved it. Well done and keep up this curiosity always ❤️
@@codestorywithMIK Thanks bhaiya, means a lot!
idk y, using arrays gives a faster response
just leetcode things..
class Solution {
public int[] findThePrefixCommonArray(int[] A, int[] B) {
int n = A.length;
int[] result = new int[n];
int[] freq = new int[n+1];
int count = 0;
for(int i = 0; i < n; i++) {
freq[A[i]]++;
if(freq[A[i]] == 2) {
count++;
}
freq[B[i]]++;
if(freq[B[i]] == 2) {
count++;
}
result[i] = count;
}
return result;
}
}
Yes, you're absolutely correct! Using arrays in C++ is generally faster than other data structures like std::vector or std::list in many scenarios.
Arrays are allocated on the stack (for fixed sizes), which is much faster compared to dynamic allocation.
Even when dynamically allocated, arrays have a simpler memory layout compared to std::vector.
Arrays don't have the overhead of extra features like dynamic resizing or bounds checking, which std::vector provides.
Arrays have contiguous memory allocation, making them more cache-friendly. This leads to better performance due to spatial locality
And many more reasons make Array faster ❤️😇
I would like to share 1 more approach whose time complexity is O(n) and auxiliary space is O(1). Following is the code -
class Solution:
"""
Time Complexity: O(n) where n is length of array 'A'
Auxiliary Space: O(1)
"""
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
n = len(A)
space = [0]*51
count = [0]*n
for i in range(n):
i1 = A[i]
i2 = B[i]
space[i1] += 1
space[i2] += 1
currrent_count = 0
for j in range(51):
if space[j] == 2:
currrent_count += 1
count[i] = currrent_count
return count
class Solution {
public int[] findThePrefixCommonArray(int[] A, int[] B) {
int []c= new int[A.length];
int n=A.length;
HashSet set1= new HashSet();
HashSet set2= new HashSet();
if(A[0]!=B[0])c[0]=0;
else c[0]=1;
set1.add(A[0]);
set2.add(B[0]);
for(int i=1;i
how approach-3 is working. I wrote the same type of code but when A[i]==B[i] it will increase extra count so we have to add one extra if condition
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n=A.size();
vector hash(n+1,0);
vector result;
int currCommon=0;
for(int i=0;i
public int[]findThePrefixCommonArray(A[],B[]){
int n=A.length,cnt=0;
int[]ans=new int[n];
int[]frq=new int[n+1];
for(int i=0;i
लॉर्ड 🐘
bhai isme bhi logic nhi bn rahan hain kya karoon anyone???
bhai honestly batau to, start studying concepts of the topics first. for example : array, map, set, tree, linkedlist, dp, graph etc.
Then solve easy qns, then move to medium slowly and so on. Practice as many qns as possible jitna qns bana sakte ho starting me banao. it will help a lot
I did it in below approach. Is the really efficient ??
class Solution {
public int[] findThePrefixCommonArray(int[] A, int[] B) {
int n = A.length;
int[] freq = new int[n+1];
int[] ans = new int[n];
for(int i=0;i
Bhai aap samjhate ho toh bahut ache se samajh aajata hai kabhi kabhi sirf explaination se mai code bhi kardeta hu par khud se ye logic develop hi nhi hota...i have just started leetcode please help !!!
If you have just started leetcode, then you are doing good man. Keep practicing, with time it will all come. Mai bhi intermediate level ka dsa karleta hu, pehle Easy bhi nahi hote the mujhse. Ab practice karte karte hone laga hai Medium bhi. But it takes time and practice bro
Thank you for your kind words! I understand that developing logic can feel tough initially, but don't worry-it's a skill that improves with consistent practice. Here's a structured way to approach DSA:
Pick one topic at a time (e.g., Arrays, Strings, Stacks, etc.).
Start with 10-15 easy problems to build your understanding and confidence.
Move to 7-10 medium problems to strengthen your logic and problem-solving.
Finally, try 2-3 hard problems to challenge yourself.
Once you feel comfortable with a topic, move on to the next one while revisiting older topics occasionally to reinforce your learning.
Always try solving problems on your own first before looking at explanations. Break problems into smaller steps, use pen and paper to visualize, and analyze why a solution works. Practice consistently on platforms like LeetCode, and you'll see steady improvement. Remember, persistence and patience are the key!
@@gui-codes Great bro
My approach using set and traversing from backwards
class Solution {
public:
vector findThePrefixCommonArray(vector& A, vector& B) {
int n=A.size();
vectorresult(n,0);
unordered_setst;
for(int i=1;i=0;i--){
if(A[i]==B[i]){
result[i]=st.size();
st.erase(A[i]);
}
else{
result[i]=st.size();
st.erase(A[i]);
st.erase(B[i]);
}
}
return result;
}
};