Question

always explain their correctness and analyze their complexity. The complexity should be as small as possible...

always explain their correctness and analyze their complexity. The complexity should be as small as possible

Consider the following question. Given a SORTED array and a number z, find if there are two number i != j, so that A[i] + A[j] = z. Give the best algorithm you can for this problem. Explain very precisely your answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This method works in O(n) time if range of numbers is known.
Let sum be the given sum and A[] be the array in which we need to find pair.

1) Initialize Binary Hash Map M[] = {0, 0, …}
2) Do following for each element A[i] in A[]
   (a)  If M[x  - A[i]] is set then print the pair (A[i], x – A[i])
   (b)  Set M[A[i]]
Add a comment
Know the answer?
Add Answer to:
always explain their correctness and analyze their complexity. The complexity should be as small as possible...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT