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
([3
,
2
,
1
,
5]
,
7)
=
(1
,
3)
two
_
sum
([3
,
2
,
8
,
3]
,
6)
=
(0
,
3)
two
_
sum
([3
,
2
,
8]
,
6)
=
(0
,
0)
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!