Array - 16: Check if there exists a pair which matches given Sum
Vložit
- čas přidán 7. 09. 2024
- Code: thecodingsimpl...
Solution:
Brute Force:
- In this, each element will add to other element in array & check if target sum matches
- Take two loop & add each element with every other & check the target sum.
- Complexity: O(n^2)
Sorting:
- Sort the array
- Take two variable, one from start & other from end.
- If a[start] + a[end] = target_sum, it means there exists a sum
- If target_sum is greater than a[start] + a[end], then decrease end, else increase start.
- Complexity: O(nlog(n))
Hashing:
- Take one Set & iterate each element of array
- If target_sum - arr[i] is in set, we got the solution, else keep adding element in array.
Please check video for more info:
This problem is similar to:
array check there is a pair for given sum,
array check if pair exists for sum,
array if sum adds ti two values,
pair,
sum,
array,
java tutorial,
coding simplified,
java
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
I started my CZcams channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.
★☆★ SUBSCRIBE TO ME ON CZcams: ★☆★
www.youtube.co...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
You r the only one who explained it in the👍💯 best way
Thanks for your nice feedback. Keep Watching.
Im doing this playlist by using c++
Now im going to learn java coz you playlists are very good🔥💓💓
Thanks for your nice feedback. Yeah, only syntax is different otherwise logic would be same.
should i learn java ...i know cpp and i entered 3rd yr now
Sir iam a huge fan of your content ..plz make videos regularly
Thanks. Sure, will upload more videos now.
amazing explaination
while(start < end) not while(start
Hi as you were saying we will increase the start or decrease the end, However I observed that the elements are getting increased or decreased not array element.
example
end = 12
end -- = 11 -- not in the array
can you please answer this, is this the desired outcome?
End is the index , not the value of the index
Sir , for sorting approach there should be ->
While (start < end )
Not
While (start
it will depend on the size of the array if it is even then 1 one if it is odd than 2 one
what if there are multiple numbers how to find pair sum in sorting method
plz make correction when u r using the concept of sorting the while condition should be like dis while(start
Nice Catch Sunny. For this case, Sorting case will give true. Updated the condition to while(start < end) rather than while(start
Plzz post videos on Amazon interview questions
Sure, will focus more on it.
@@CodingSimplified thank you so much for this type of content. Its really good
Thanks
Can we get the index of those two elements using hashset
No you can get it by element only
Hi Neel, though it's late reply, but answering if it helps you. Using current Hashing solution, we can't get index. But if you want to get index. While insertion, store index as well. For this, you can take another structure like Node & store in hashSet.
- Hope it helps you. Thanks.
well explained than.
sir ekdum mast
Thanks for your nice feedback. Keep Watching.
OP ,♥️
Does this also work for duplicate elements?
If you've duplicate i.e 2, 3, 2, 3 & given sum.is 5 then it'll work. For case 2, 3, 2 & given sum 5. It'll given answer 2 using hashing. Now it all depends on requirements. If you're ok with 2 set (2, 3) & (2, 3) then it'll work. So as per your question, you need to change little bit in code. Thanks.
We can do using sorting we duplicate contain also
Bestttttt
two pointer algorithm
If array contains duplicate elements then your code fails
For duplicate, we need little bit modify the code. Try to do it by yourself. If you need any help, let me know with your input.