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
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
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.
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!
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) ?
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.
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
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.
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
what about the last remaining array, which may contain duplicate elements.
Beautifully explained. Thanks GFG
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
Yeah that's what I am thinking, it would be good if you provide any source of rectified code.
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
For union code, shouldn't O(max(m,n)) be a better u.b.?
what if elemnts in array are repeating ...
what should we do in that case..?
hey bro u asked this question 8 months back so can u share the answer here? :)
Thanks for sharing your knowledge , Really appropriate your work , Please keep adding ,
You're welcome, Shirish!
Its is very good idea of making videos on Algos... Thanks for the great explanation...
You're welcome, Karthik!
Wow!! great explanation
how to take union of two dynamically allocated arrays in class in c++
Perfect explanation.
Thanks :)
if there is duplicate elements in array we also have to check that currect number is occurs past time or not
What if there are duplicates in any of the array. How to solve then without repeating the number in the output.
great explanation .
Superb !!!
I didn't understand the part arr1[ i++ ] , we had to print a[ i ] know?
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
i think it should be done by heap..
Why we taking size of???
is this intersection work for unsorted array?
Great work .... ...... Keep it up.... ..
Thank you, Rohit!
isn't going to print duplicate numbers if remains in larger array ?
is might in union one set have one or more same elements
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
Best on yt 😄😄
Thanku so much
Thank you so much
Thanks for this video, How is the time complexity O(m+n) in the case of intersection??
Sahejeet Singh arr1={1,3,5}, arr2={2,4,6}
Time complexity in worst cases !!
Not in everyone !!
what about unsorted arrays? should we have to sort them first?
Thanks for asking the question.
You can find the related answer here: www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/
Why in second case time complexity is O(m+n)
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.
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!
yes
Thanks
Thanku so much😀😀
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) ?
Yes, Rahul, you are right. In the first solution the time complexity is always O(m+n).
GeeksforGeeks how?
If suppose both the arrays are {1,2,3} , then it would only run 3 times not six times...... isn't it?
I have the code as it is shown in the video but it doesn't worked.
Will this algo works i fwe have two arrays like a1=[9,20,30] a2=[40,50,60]
If the both arrays are sorted and there is no repeated element in the individual array. Then this algo definitely works
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?
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.
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
Plz try it and correct , If I'm wrong
@@BioMedicalD you are right
💯💯
For union can't we just destructuring both? Like [ ...arr1, ...arr2 ]?
but, the array union = { } was not created.
thanks
void niceExplanation(Union and Intersection of two sorted arrays)
{
cout
Thanks!
You're welcome, Siddharth! :)
how to handle duplicates while writing program?
Good question
I didn't understood the code of printing the remaining elements
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.
if arr1[1,3,4,5,3,4] and arr2[4,5.1,6,4] then this code is not work
must in ascending and duplicates r not in sets
C++ code if either of the array contains duplicate elements:
class Solution {
public:
bool isUnique(vector& temp, int element)
{
for(int i = 0;i
very very nice exercise. Typical phone-screen question.
What if u have duplicate array elements !?!?
first take the union with duplicates and then delete duplicates in second chance..
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
Because elements are repeating in individual array
It will not work if one array contains a value multiple times
Yes and unsort case also
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
Thanks for pointing this out. In this video, we assume that there are no duplicates in the arrays.
GeeksforGeeks I want a java program that print Union and intersection having unsorted integer array.... can you help?
Sure,
You can visit this article: www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/ on GeeksforGeeks for that.
GeeksforGeeks but a video would be more helpful, that article is a little difficult to understand 😕
First of all a set doesn't contain duplicate elements. The definition itself says that collection of unique well defined objects
Not a optimized solution for union of array
Not a good explanation for second/optimized way solving this problem
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
Yes, how to write by taking input from user?? Kindly reply
it is not true for any array having repeated elements
Well this fails when there are same elements in any of the arrays
Thanks
your welcome :)