Question: Input: Given an array A of integers, and a target integer x . Process: Write an algorithm two _ sum ( A , x )
Input: Given an array A of integers, and a target integer x
Process: Write an algorithm two
sum
A
x
which traverses A to find the indices i and j of
two numbers st
i
j
A
i
A
j
x
Meaning that you need to return the indices of two unique elements in A that add up to x
Remember, these two elements have to be unique in the sense that they have different indices,
yet they can be the same numbers; for ex:
two
sum
two
sum
two
sum
Output: A tuple of the form
i
j
Note: You may assume that there will always be exactly two numbers in A that will add up to
x
Your algorithm can take any number of operations to compute this, i
e
any time complexity will be accepted. Derive an O
n log n
solution for this question which includes the algorithms, appropriate proofs of correctness, time
space
complexity analysis. code the whole soluting in sml coding language.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
