Union and Intersection of two sorted arrays | GeeksforGeeks

Поділитися
Вставка
  • Опубліковано 31 бер 2016
  • Explanation for the article: www.geeksforgeeks.org/union-an...
    Read More: www.geeksforgeeks.org/union-a...
    This video is contributed by Harshit Jain.

КОМЕНТАРІ • 94

  • @pranavkorhale5089
    @pranavkorhale5089 2 роки тому +7

    what about the last remaining array, which may contain duplicate elements.

  • @lakhdeepsingh5883
    @lakhdeepsingh5883 4 роки тому +2

    Beautifully explained. Thanks GFG

  • @abhishekchauhan6056
    @abhishekchauhan6056 Рік тому +4

    I figured out a important exception i.e a test case that do not follows it, if there is a repetition of elements in 2nd array which is greater than maximum element of 1st array, that elements will get repeated in array

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

      Yeah that's what I am thinking, it would be good if you provide any source of rectified code.

  • @akulshrestha88
    @akulshrestha88 4 роки тому +7

    What if the individual arrays have the repeated elements, then if i am filtering it with list.contains() method then it is exceeding time limit

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

    For union code, shouldn't O(max(m,n)) be a better u.b.?

  • @vishwajeetchoudhary270
    @vishwajeetchoudhary270 2 роки тому +5

    what if elemnts in array are repeating ...
    what should we do in that case..?

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

      hey bro u asked this question 8 months back so can u share the answer here? :)

  • @shirishpatil9861
    @shirishpatil9861 7 років тому

    Thanks for sharing your knowledge , Really appropriate your work , Please keep adding ,

  • @karthikeyy
    @karthikeyy 7 років тому +10

    Its is very good idea of making videos on Algos... Thanks for the great explanation...

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

    Wow!! great explanation

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

    how to take union of two dynamically allocated arrays in class in c++

  • @shubhagupta8489
    @shubhagupta8489 6 років тому +7

    Perfect explanation.

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

    if there is duplicate elements in array we also have to check that currect number is occurs past time or not

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

    What if there are duplicates in any of the array. How to solve then without repeating the number in the output.

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

    great explanation .

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

    Superb !!!

  • @kumarrohit5898
    @kumarrohit5898 2 роки тому +1

    I didn't understand the part arr1[ i++ ] , we had to print a[ i ] know?

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

    in the union code there will be wrong answer if have repeating elements
    like {1,2,2,3,4,5} and {1,2,2,3,5}
    in union array 2 will be twice

  • @yogitashewale2670
    @yogitashewale2670 5 років тому +1

    Why we taking size of???

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

    is this intersection work for unsorted array?

  • @RohitKumar-rz3qj
    @RohitKumar-rz3qj 7 років тому

    Great work .... ...... Keep it up.... ..

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

    isn't going to print duplicate numbers if remains in larger array ?

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

    is might in union one set have one or more same elements

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

    I would like to start DSA now. Shall I start now with this channel because it is made 6 years before. What I would like to ask is can I follow this or is this outdated

  • @074vinaysuyal3
    @074vinaysuyal3 3 роки тому

    Best on yt 😄😄

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

    Thanku so much

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

    Thank you so much

  • @sahej97
    @sahej97 6 років тому +1

    Thanks for this video, How is the time complexity O(m+n) in the case of intersection??

    • @amitsharma5996
      @amitsharma5996 4 роки тому +2

      Sahejeet Singh arr1={1,3,5}, arr2={2,4,6}

    • @PrinceKumar-el7ob
      @PrinceKumar-el7ob 4 роки тому +2

      Time complexity in worst cases !!
      Not in everyone !!

  • @jawadadil8604
    @jawadadil8604 7 років тому +1

    what about unsorted arrays? should we have to sort them first?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому +2

      Thanks for asking the question.
      You can find the related answer here: www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/

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

    Why in second case time complexity is O(m+n)

  • @adnannasim6349
    @adnannasim6349 4 місяці тому

    There’s a bug in this code if we have values like
    arr1={1,2,2,4,5,6}
    arr2={2,2,2,3,5,7}
    It will print the value 2 twice which is wrongi believe. Please correct it.
    Optimal solution use unordered set.

  • @vikastiwari2910
    @vikastiwari2910 6 років тому

    Time complexity of intersection will be O(mn) where m is the length of the array1 and n is the length of the array2 is it right please clarify me Thank You!

  • @suhanisharma2277
    @suhanisharma2277 2 роки тому +1

    Thanks

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

    Thanku so much😀😀

  • @rahulbaburajan
    @rahulbaburajan 7 років тому +1

    You said time complexity given for union of 2 sorted arrays is the worst case time complexity. But isn't the time complexity in this case is always O(m+n) ?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому

      Yes, Rahul, you are right. In the first solution the time complexity is always O(m+n).

    • @sahej97
      @sahej97 6 років тому

      GeeksforGeeks how?
      If suppose both the arrays are {1,2,3} , then it would only run 3 times not six times...... isn't it?

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

    I have the code as it is shown in the video but it doesn't worked.

  • @rajeshreddyz
    @rajeshreddyz 6 років тому

    Will this algo works i fwe have two arrays like a1=[9,20,30] a2=[40,50,60]

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

      If the both arrays are sorted and there is no repeated element in the individual array. Then this algo definitely works

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

    what if one number is in starting of one array and the sum number is at last of another array. like this a{5,3,2,4,1} b{1,2,7,3,5}. How to solve this?

  • @DhananjayKumar-vn5tc
    @DhananjayKumar-vn5tc 3 роки тому +5

    i think the code has some problem (if not then correct me)
    in while part of code if the remaining elements of an array have duplicates (which you already printed) can create problem...
    help me and correct me if i am wrong.

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

      I think it won't be like that, .. because both arrays are in sorted, so the repeated element will not come . Until one of the array printed fully and then the remaining element in the higher length array, will print the greater element than the element present in the smaller length array
      . I think you will understand now

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

      Plz try it and correct , If I'm wrong

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

      @@BioMedicalD you are right

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

      💯💯

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

    For union can't we just destructuring both? Like [ ...arr1, ...arr2 ]?

  • @systemskoncept2143
    @systemskoncept2143 6 років тому

    but, the array union = { } was not created.
    thanks

  • @shaishav3207
    @shaishav3207 2 роки тому +1

    void niceExplanation(Union and Intersection of two sorted arrays)
    {
    cout

  • @sidk5919
    @sidk5919 7 років тому

    Thanks!

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

    how to handle duplicates while writing program?

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

    I didn't understood the code of printing the remaining elements

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

      As one of the array gets exhausted the other array still contain remaining numbers which need to be pushed in final union array so for pushing the remaining numbers we need to put all the numbers left in second array.

  • @mohitkumar-ml9sr
    @mohitkumar-ml9sr 5 років тому +1

    if arr1[1,3,4,5,3,4] and arr2[4,5.1,6,4] then this code is not work

  • @muditrastogi5755
    @muditrastogi5755 Рік тому +1

    C++ code if either of the array contains duplicate elements:
    class Solution {
    public:

    bool isUnique(vector& temp, int element)
    {
    for(int i = 0;i

  • @reyou7
    @reyou7 6 років тому

    very very nice exercise. Typical phone-screen question.

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

    What if u have duplicate array elements !?!?

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

      first take the union with duplicates and then delete duplicates in second chance..

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

    for arrays
    13 17 18 19 20 22 22 27 36 39 46 48 50
    4 12 45
    it shows 4 12 13 17 18 19 20 22 22 27 36 39 45 46 48 50
    which is wrong

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

      Because elements are repeating in individual array

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

    It will not work if one array contains a value multiple times

  • @momomiasec1078
    @momomiasec1078 7 років тому

    int arr1[]={1,2,3,4,4};
    int arr2[]={4,5,6,8,9};
    its not correctly working for these input
    expected output is= 1 2 3 4 5 6 8 9
    actual output= 1 2 3 4 4 5 6 8 9

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому +1

      Thanks for pointing this out. In this video, we assume that there are no duplicates in the arrays.

    • @momomiasec1078
      @momomiasec1078 7 років тому

      GeeksforGeeks I want a java program that print Union and intersection having unsorted integer array.... can you help?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому +1

      Sure,
      You can visit this article: www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/ on GeeksforGeeks for that.

    • @sahej97
      @sahej97 6 років тому

      GeeksforGeeks but a video would be more helpful, that article is a little difficult to understand 😕

    • @basudevyadav8326
      @basudevyadav8326 6 років тому

      First of all a set doesn't contain duplicate elements. The definition itself says that collection of unique well defined objects

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

    Not a optimized solution for union of array

  • @gbkumar4
    @gbkumar4 6 років тому

    Not a good explanation for second/optimized way solving this problem

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

    Your program is in input favourable case it is not by asked by user what's the size of a set and what element do you inserted.
    Make a general program

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

      Yes, how to write by taking input from user?? Kindly reply

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

    it is not true for any array having repeated elements

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

    Well this fails when there are same elements in any of the arrays

  • @piyushsharma1638
    @piyushsharma1638 8 років тому +1

    Thanks