Find the Prefix Common Array of Two Arrays | 3 Detailed Approach | Leetcode 2657 | codestorywithMIK

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ม.ค. 2025

ความคิดเห็น • 110

  • @riteshhhhh_01
    @riteshhhhh_01 วันที่ผ่านมา +27

    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.”

  • @Krishnesh_Tiwari
    @Krishnesh_Tiwari วันที่ผ่านมา +18

    कर्मण्येवाधिकारस्ते मा फलेषु कदाचन।
    मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि॥ Tomorrow's quotes from my side -- motivation in sanskrit 🥰🥰🥰

  • @souravjoshi2293
    @souravjoshi2293 วันที่ผ่านมา +4

    1:27 - Hell Yeahhhhhhh. "It's Me vs Me" 🔥🔥🔥🔥
    Ekdm josh ajata hai sir aapke motivation sunkar. THanks a lot

  • @randomly-unique
    @randomly-unique 10 ชั่วโมงที่ผ่านมา +1

    Thank you MIK and Congratulations 🎊 it's 80k strong community now!

  • @rockykumarverma980
    @rockykumarverma980 12 ชั่วโมงที่ผ่านมา +1

    Thank you so much Bhaiya ji 🙏🙏🙏

  • @joydeep-halder
    @joydeep-halder 12 ชั่วโมงที่ผ่านมา +1

    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;

    }

  • @ankitatiwary4663
    @ankitatiwary4663 วันที่ผ่านมา +2

    Thanks aise hi java me continue rakhiye

  • @s..3189
    @s..3189 15 ชั่วโมงที่ผ่านมา +2

    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

  • @madhubabu3482
    @madhubabu3482 วันที่ผ่านมา +1

    Done using my own approach without watching your video, thanks mike for the confidence

  • @Letstudy-p7y
    @Letstudy-p7y วันที่ผ่านมา +4

    Done this problem using O(n^2) , thanks for providing more optimised approch. Your explanation is really awesome

  • @bhupendrakalal1727
    @bhupendrakalal1727 20 ชั่วโมงที่ผ่านมา +1

    ooo i didnt realise i too have did it in optimal approach 🥳🥳🥳

  • @_PRANAYMATE
    @_PRANAYMATE 14 ชั่วโมงที่ผ่านมา +1

    Third Approach with O(n) is Mind Opening Approach

  • @joydeepgharami403
    @joydeepgharami403 วันที่ผ่านมา +2

    Nice 👍

  • @xyzhacker00999
    @xyzhacker00999 23 ชั่วโมงที่ผ่านมา +2

    Woow mza aa gya😇

  • @YouTubeNTorQ
    @YouTubeNTorQ 21 ชั่วโมงที่ผ่านมา +1

    Completed by myself buddy, Very thanks for the intuition for these type of question.

  • @SatyamSingh-xq1zk
    @SatyamSingh-xq1zk 16 ชั่วโมงที่ผ่านมา +2

    I did it in O(n²) TC, without seeing soln and code, kudos to you MIK sir👏

    • @SatyamSingh-xq1zk
      @SatyamSingh-xq1zk 15 ชั่วโมงที่ผ่านมา

      Did O(n) soln also

  • @vikadjaat1392
    @vikadjaat1392 19 ชั่วโมงที่ผ่านมา +3

    completed the problem in O(n) but still came to know other appoaches to improve my knowledge .

  • @imPriyansh77
    @imPriyansh77 วันที่ผ่านมา +2

    Bhaiya, thank you for making me more consistent!! Love your motivation and problem-solving approach!!😇😀

  • @sahilshah4698
    @sahilshah4698 วันที่ผ่านมา +1

    Thanks for also coding in java, please continue the same and keep up the good work, learning a lot from you.

  • @wearevacationuncoverers
    @wearevacationuncoverers วันที่ผ่านมา

    Thanks a lot MIK. Mai 3rd approach k lie aya tha. Goat ho aap.

  • @AryanVats603
    @AryanVats603 17 ชั่วโมงที่ผ่านมา +1

    Esy one today also❤ thanks sir for your teachings

  • @Hpktube
    @Hpktube วันที่ผ่านมา +2

    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

  • @ayush-of1fe
    @ayush-of1fe วันที่ผ่านมา +1

    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

  • @ishangujarathi10
    @ishangujarathi10 17 ชั่วโมงที่ผ่านมา +1

    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!!

  • @m_fi8926
    @m_fi8926 18 ชั่วโมงที่ผ่านมา

    yay! 80k soon🥳🤯🥳

  • @annagarg2567
    @annagarg2567 14 ชั่วโมงที่ผ่านมา +1

    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.

  • @Coder_Buzz07
    @Coder_Buzz07 23 ชั่วโมงที่ผ่านมา +1

    Happy Makar Sankranti mik bhaiya❤❤❤

    • @codestorywithMIK
      @codestorywithMIK  23 ชั่วโมงที่ผ่านมา +1

      Happy Makar Sakranti 😇🙏❤️🎉🎉

  • @eprithvikushwah4754
    @eprithvikushwah4754 วันที่ผ่านมา +2

    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;
    }
    };

  • @aryansinha1818
    @aryansinha1818 13 ชั่วโมงที่ผ่านมา

    Thank you for the motivation

  • @vishwashsoni610
    @vishwashsoni610 20 ชั่วโมงที่ผ่านมา +2

    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

  • @salmaniproductions1104
    @salmaniproductions1104 22 ชั่วโมงที่ผ่านมา +1

    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.

    • @codestorywithMIK
      @codestorywithMIK  21 ชั่วโมงที่ผ่านมา +2

      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 ❤️❤️

  • @janaSdj
    @janaSdj 22 ชั่วโมงที่ผ่านมา +1

    Thank you...

  • @mohammadaftabansari6882
    @mohammadaftabansari6882 20 ชั่วโมงที่ผ่านมา +1

    Thanks bhaiya

  • @iqrathanedar915
    @iqrathanedar915 19 ชั่วโมงที่ผ่านมา +1

    Today I solved this problem on my own in an optimized way without watching any tutorial

  • @gui-codes
    @gui-codes วันที่ผ่านมา +1

    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

  • @adarshpatel58
    @adarshpatel58 19 ชั่วโมงที่ผ่านมา +4

    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

    • @codestorywithMIK
      @codestorywithMIK  18 ชั่วโมงที่ผ่านมา +2

      th-cam.com/video/B5aIqLq50_8/w-d-xo.htmlsi=PTJMTu2Q8RwrmbP8
      I will definitely try to squeeze more time to upload more ❤️🙏

  • @thefinancialdiet4458
    @thefinancialdiet4458 วันที่ผ่านมา +2

    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 🫡

    • @aws_handles
      @aws_handles วันที่ผ่านมา +2

      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.

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +1

      Soon ❤️ will update on this

    • @thefinancialdiet4458
      @thefinancialdiet4458 วันที่ผ่านมา +1

      @@codestorywithMIK Thanks alottt sir😭😭❤️❤️❤️❤️

    • @chinmaygupta2277
      @chinmaygupta2277 วันที่ผ่านมา +2

      If sir starts leetcode contests soolutions toh usme main consistency aa jayegi

    • @AkOp-bf9vm
      @AkOp-bf9vm 18 ชั่วโมงที่ผ่านมา

      @@codestorywithMIK also cpp oops playlist 🤗🤗

  • @vikadjaat1392
    @vikadjaat1392 19 ชั่วโมงที่ผ่านมา +1

    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

  • @aizad786iqbal
    @aizad786iqbal 23 ชั่วโมงที่ผ่านมา +1

    awesome...

  • @dharmeshlunawat5196
    @dharmeshlunawat5196 23 ชั่วโมงที่ผ่านมา +1

    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

  • @AradhyaVerma-p3m
    @AradhyaVerma-p3m 17 ชั่วโมงที่ผ่านมา +1

    /*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

  • @vinaymaurya4895
    @vinaymaurya4895 19 ชั่วโมงที่ผ่านมา +1

    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;
    }
    };

  • @team-fanfeet
    @team-fanfeet วันที่ผ่านมา +1

    Present sir ❤

  • @atishayjain7220
    @atishayjain7220 11 ชั่วโมงที่ผ่านมา +1

    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

  • @ckshenoy
    @ckshenoy วันที่ผ่านมา +1

    This can be done using bit manipulation, can we have a video on that. This would be a good problem for bit manipulation practice

  • @adityazende6710
    @adityazende6710 14 ชั่วโมงที่ผ่านมา +1

    For O(n) solution the most important word is "permutation".

  • @eprithvikushwah4754
    @eprithvikushwah4754 วันที่ผ่านมา +1

    1:25 " Comparison se bhiya bs bdti h baicheni" -- Dino james

  • @Ankitkumar-fz3kc
    @Ankitkumar-fz3kc 16 ชั่วโมงที่ผ่านมา

    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?

  • @jayeshverma7049
    @jayeshverma7049 วันที่ผ่านมา +1

    thanks bhai

  • @EB-ot8uu
    @EB-ot8uu วันที่ผ่านมา

    Itna effort kaise dete ho sir aap har din. ek baar life me aapse zaroor milna hai mujhe

  • @utkarsh-k6p
    @utkarsh-k6p 22 ชั่วโมงที่ผ่านมา +1

    Bhaiya Please solve -->
    3291. Minimum Number of Valid Strings to Form Target I
    3292. Minimum Number of Valid Strings to Form Target II

  • @24deeshankbatra38
    @24deeshankbatra38 21 ชั่วโมงที่ผ่านมา +2

    My Brute Force is Optimal 😅....xd :)

  • @crazygamerrohan9899
    @crazygamerrohan9899 วันที่ผ่านมา

    Kindly Discuss BIt Manipulation Solution

  • @arnabsarkar5245
    @arnabsarkar5245 19 ชั่วโมงที่ผ่านมา

    Bhaiya please make a playlist on combinatorics concept and questions. Bahhto jarurat hai iski bhaiya

  • @jitDhank04
    @jitDhank04 15 ชั่วโมงที่ผ่านมา +1

    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

  • @surajbodke5173
    @surajbodke5173 14 ชั่วโมงที่ผ่านมา +1

    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;
    }
    };

  • @TauheedMohammed-d2e
    @TauheedMohammed-d2e วันที่ผ่านมา +1

    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

  • @faizanmohammed7687
    @faizanmohammed7687 18 ชั่วโมงที่ผ่านมา +1

    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.

    • @faizanmohammed7687
      @faizanmohammed7687 18 ชั่วโมงที่ผ่านมา

      meri question understanding hi gandi hai bahut.

    • @codestorywithMIK
      @codestorywithMIK  18 ชั่วโมงที่ผ่านมา +1

      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.

    • @faizanmohammed7687
      @faizanmohammed7687 18 ชั่วโมงที่ผ่านมา

      @@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

  • @worldofgaming748
    @worldofgaming748 22 ชั่วโมงที่ผ่านมา +1

    Tomorrow's quotes from my side:""Motivation may fade, but discipline and obsession are the fuel that never run dry.""

  • @VaishnavI-me8bz
    @VaishnavI-me8bz 17 ชั่วโมงที่ผ่านมา

    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??🫠

  • @bhupendrakalal1727
    @bhupendrakalal1727 21 ชั่วโมงที่ผ่านมา +1

    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

    • @VaishnavI-me8bz
      @VaishnavI-me8bz 16 ชั่วโมงที่ผ่านมา

      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)

  • @25-cse-csmohitkumarmandal59
    @25-cse-csmohitkumarmandal59 23 ชั่วโมงที่ผ่านมา

    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

  • @aditya12881
    @aditya12881 วันที่ผ่านมา +1

    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 ..

    • @EB-ot8uu
      @EB-ot8uu วันที่ผ่านมา

      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

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +1

      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

    • @imPriyansh77
      @imPriyansh77 วันที่ผ่านมา

      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.

    • @aditya12881
      @aditya12881 14 ชั่วโมงที่ผ่านมา +1

      Thanks to everyone and especially mik bhaiya

  • @sauravchandra10
    @sauravchandra10 20 ชั่วโมงที่ผ่านมา +1

    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

    • @codestorywithMIK
      @codestorywithMIK  19 ชั่วโมงที่ผ่านมา

      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 ❤️🙏

    • @sauravchandra10
      @sauravchandra10 18 ชั่วโมงที่ผ่านมา +1

      @@codestorywithMIK Yes, I suppose we can use bit masking. Will try on my own and let you know.

    • @sauravchandra10
      @sauravchandra10 18 ชั่วโมงที่ผ่านมา +1

      @@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

    • @codestorywithMIK
      @codestorywithMIK  18 ชั่วโมงที่ผ่านมา

      So glad to you solved it. Well done and keep up this curiosity always ❤️

    • @sauravchandra10
      @sauravchandra10 17 ชั่วโมงที่ผ่านมา +1

      @@codestorywithMIK Thanks bhaiya, means a lot!

  • @aizad786iqbal
    @aizad786iqbal 22 ชั่วโมงที่ผ่านมา +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;
    }
    }

    • @codestorywithMIK
      @codestorywithMIK  21 ชั่วโมงที่ผ่านมา +1

      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 ❤️😇

  • @ishikagarg5261
    @ishikagarg5261 12 ชั่วโมงที่ผ่านมา

    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

  • @zaffarzeshan1308
    @zaffarzeshan1308 16 ชั่วโมงที่ผ่านมา

    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

  • @AkOp-bf9vm
    @AkOp-bf9vm 18 ชั่วโมงที่ผ่านมา

    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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 วันที่ผ่านมา +1

    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

  • @ago7506
    @ago7506 17 ชั่วโมงที่ผ่านมา

    लॉर्ड 🐘

  • @RamakantS07
    @RamakantS07 17 ชั่วโมงที่ผ่านมา

    bhai isme bhi logic nhi bn rahan hain kya karoon anyone???

    • @gui-codes
      @gui-codes 17 ชั่วโมงที่ผ่านมา

      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

  • @pammijain9477
    @pammijain9477 18 ชั่วโมงที่ผ่านมา

    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

  • @RandomGuy34-j1u
    @RandomGuy34-j1u วันที่ผ่านมา +1

    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 !!!

    • @gui-codes
      @gui-codes วันที่ผ่านมา

      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

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +4

      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!

    • @imPriyansh77
      @imPriyansh77 วันที่ผ่านมา

      @@gui-codes Great bro

  • @yuvrajsoni6865
    @yuvrajsoni6865 วันที่ผ่านมา +1

    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;
    }
    };