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
You are our Unknown Guru, and delivering lots of knowledge
Thank you very much!
JAI HIND
Thanks for your nice feedback. Keep Watching. Jai Hind.
array index out of bound
thank you bro
Nicely explained, keep going 😀
Thanks for your nice feedback. Keep Watching.
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;
}
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?
sir we can that question also from hashset
Thanks bro.... ♥️👍👍
Thanks. Keep Watching.
What if the array is unsorted ?
Well explained bro!
Thanks for your nice feedback. Keep watching.
Good works sir
Thanks for your nice feedback. Keep Watching.
Good work. Please create a git repo and upload the programs and share the link in description. Thanks.
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.
WHAT THE HELL ! THANKS A MILLION BROTHER !
Thanks for your nice feedback. Keep Watching.
Problem solving to interview 😁
Right, that's the final goal of everyone :)
sir can we use hashset or tree ??
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.
which is the best time complexity for this?
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.
@@CodingSimplified what will be the space complexity??as u taking finalList here...space complete will Incr or not??