Question

Given an unsorted array of distinct positive integers A [ 1......n ] in the range between...

Given an unsorted array of distinct positive integers A [ 1......n ] in the range between 1 and 10000 and an integer i in the sane range. Here n can be arbitrary large You want to find out whether there are 2 elements of the array that add up to i. Give an algorithm that runs in time (O(n).

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

******************************************************************************************
Please Upvote the answer as it matters to me a lot :)
*****************************************************************************************
As per HomeworkLib expert answering guidelines,Experts are supposed to answer only certain number of questions/sub-parts in a post.Please raise the remaining as a new question as per HomeworkLib guidelines.
******************************************************************************************

find(A,n,i)
for j =0 to 10000 do
frequency[j]=0
for j=1 to n do
frequency[A[j]]= frequency[A[j]]+1
for j =1 to n do
if i>=A[j] then
if (i-A[j])!=A[j] and frequency[i-A[j]]>0 then
return true
else if (i-A[j])==A[j] and frequency[j-A[j]]>1 then
return true
else
if (A[j]-i)!=A[j] and frequency[A[j]-i]>0 then
return true
else if (A[j]-i)==A[j] and frequency[A[j]-i]>1 then
return true
return false
  
  

Add a comment
Know the answer?
Add Answer to:
Given an unsorted array of distinct positive integers A [ 1......n ] in the range between...
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