Array - 20: Union of Two Sorted Arrays

Поділитися
Вставка
  • Опубліковано 30 вер 2024
  • Code: thecodingsimpl...
    Solution:
    - One by one we'll compare both array values from starting
    - Whichever array value is smaller, put that value in new array or list & increase the index by 1
    - If both array value is same, then, put any array value in new array or list & inclrease both indexes(i, j)
    - At last, if you've any item left in any array, put all values of that array in new array
    - Time Complexity: O(n + m) for iterating the both array, where m - first array length, m - second array length
    Please check video for more info:
    This problem is similar to:
    how to do union of two sorted array,
    union of two sorted arrays,
    print union of two sorted array,
    union,
    two sorted array,
    array,
    java tutorial,
    coding simplified,
    java
    CHECK OUT CODING SIMPLIFIED
    / codingsimplified
    I started my UA-cam channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 400+ videos. My account is Partner Verified.
    ★☆★ VIEW THE BLOG POST: ★☆★
    thecodingsimpli...
    ★☆★ SUBSCRIBE TO ME ON UA-cam: ★☆★
    www.youtube.co...
    ★☆★ SEND EMAIL At: ★☆★
    Email: thecodingsimplified@gmail.com

КОМЕНТАРІ • 31

  • @ravitejapoosala9064
    @ravitejapoosala9064 3 роки тому +3

    You are our Unknown Guru, and delivering lots of knowledge
    Thank you very much!
    JAI HIND

    • @CodingSimplified
      @CodingSimplified  3 роки тому +1

      Thanks for your nice feedback. Keep Watching. Jai Hind.

  • @gauravmisal4610
    @gauravmisal4610 Рік тому

    array index out of bound

  • @tamilmukbang3789
    @tamilmukbang3789 6 місяців тому

    thank you bro

  • @jayantsharma2669
    @jayantsharma2669 3 роки тому +2

    Nicely explained, keep going 😀

  • @sherazdotnet
    @sherazdotnet 2 роки тому

    Line 8 and 12 in code is code smell. It works but anyone reading it would have to think what that code is doing. Instead, if you have a method that that says something like IsDuplicate and encapsulate that logic in there would make it much easier to understand. Something like this [small sample]
    if(p1Value < p2Value) {
    if(! IsDuplicate(p1Value,result,resultIndex)) {
    result[resultIndex] = p1Value;
    resultIndex++;
    }
    p1++;
    }
    And here is the IsDuplicate function
    private bool IsDuplicate(int value, int[] result, int index) {
    if(index ==0) {
    return false;
    }

    return result[index-1] == value;
    }

  • @roshanbhandari3999
    @roshanbhandari3999 3 роки тому

    What will be the space complexity? will it be also m+n as the array size will be dependent upon the incoming two input arrays?

  • @shivankgautam5196
    @shivankgautam5196 Рік тому

    sir we can that question also from hashset

  • @adityanayak6816
    @adityanayak6816 4 роки тому +1

    Thanks bro.... ♥️👍👍

  • @snehasisbarat2457
    @snehasisbarat2457 3 роки тому

    What if the array is unsorted ?

  • @reshmah1497
    @reshmah1497 3 роки тому +1

    Well explained bro!

  • @SOURAVKUMAR-di4xg
    @SOURAVKUMAR-di4xg 3 роки тому +1

    Good works sir

  • @_jitendrakumarr
    @_jitendrakumarr 5 років тому

    Good work. Please create a git repo and upload the programs and share the link in description. Thanks.

    • @CodingSimplified
      @CodingSimplified  5 років тому +2

      Thanks for your suggestion. After your feedback, I thought about it and created website www.thecodingsimplified.com It'll have all source code about problems. Will add problem link in description of videos.

  • @juraijbinjamal5313
    @juraijbinjamal5313 3 роки тому +4

    WHAT THE HELL ! THANKS A MILLION BROTHER !

  • @dharmaputra7394
    @dharmaputra7394 5 років тому

    Problem solving to interview 😁

  • @ujjwaljain9780
    @ujjwaljain9780 4 роки тому

    sir can we use hashset or tree ??

    • @CodingSimplified
      @CodingSimplified  4 роки тому +3

      Yes you can use it for both Sorted & Unsorted array. But here we're given sorted array so we're doing it without Hashset. HashSet is costly space complexity wise. Thanks. Let me know if you have any questions.

  • @nithishreddy752
    @nithishreddy752 4 роки тому

    which is the best time complexity for this?

    • @CodingSimplified
      @CodingSimplified  4 роки тому +1

      For this, Best & Avg time complexity will be O(n + m), where n = length of 1st array, m = length of 2nd array
      As you need to traverse all elements of array to get final list.

    • @anjalisingh-sx5ct
      @anjalisingh-sx5ct 4 роки тому

      @@CodingSimplified what will be the space complexity??as u taking finalList here...space complete will Incr or not??