Ask Question
20 July, 20:43

Given an unsorted array of distinct positive integers A[1 ... n] in the range between 1 and 10000 and an integer i in the same 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).

+4
Answers (1)
  1. 21 July, 00:32
    0
    Arbitrary means That no restrictions where placed on the number rather still each number is finite and has finite length. For the answer to the question--

    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Given an unsorted array of distinct positive integers A[1 ... n] in the range between 1 and 10000 and an integer i in the same range. Here ...” in 📘 Engineering if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers