You are on page 1of 5

A-1.

7 Suppose you are given an integer c and an array, A, indexed from 1 to n, of n integers in the range from 1 to 5n (possibly
with duplicates). Describe an efficient algorithm for determining if there are two integers, A[i] and A[j], in A that sum to c. that is,
such that c = A[i]+ A[j], for 1 <= i < j <= n. What is the running time of your algorithm?

Algorithn comtcn วะ

Inpvt An A
storing integers

n ≥ 1
array

Display the
Outpt ะ
integer

COU ท -

for i ←
o
to n -

า do
tor to do
j
← im
j < n

if c Aci ] *
Acj ] > Aci ] +
Acj ]
covn
- count +1

return count

i.
The
running
time of the algorithm is Ocn )

F-
#

Input :{ 5,0 , 3,1 ,


2 }

Outpt ะ 3

63160187 ขวัญจิรา คุ้มปากพิง


ซ๋
ฝ่
A-1.8 Given an array, A, describe an efficient algorithm for reversing A. For example,if A = [3. 4, 1, 5], then its reversal is A = [5.
1, 4, 3]. You can only use O(1) memory in addition to that used by A itself. What is the running time of your algorithm?

Algorithm array
Reverse (A) ะ

Input ะ
An n
-
element array Aof
integers
of A
Output ะ
Reverse Array

start ←
o

end ←
nn

while start < end do

temp ← Acstarts

A [ starts ← A [ end ]

A [ ends ←
temp

start ← start + า

end ← end -

return start

i.
The
running
time of the algorithm is Ocn )

F- .

:{ 10,2 0,3 0,4 0,50 }


Inpvt
Output :{50,40,3 0,4 0,1 ง }

63160187 ขวัญจิรา คุ้มปากพิง


A-1.9 Given a string, S, of n digits in the range from 0 to 9, describe an efficient algorithm for converting S into the integer it
represents. What is the running time of your algorithm?

Convertcs )
AI gorithm ะ

Inputi An n
-
element array S of
string
Output ะ

Display the integer


num o

for i ← ◦ to nn do

← * + C 5 [ I ] -0 )
num nvm 1o

return num
'

i.
The
running
time of the algorithm is Ocn )

F-
¥
Input
" "
:
{ 123 }

Output ะ
123

63160187 ขวัญจิรา คุ้มปากพิง


A-1.10 Given an array, A, of n integers, find the longest subarray of A such that all the numbers in that subarray are in sorted
order. What is the running time of your method?

Algorithm Subarray (A) ะ

Inputi An element A ot integers


array
-

Output ะ
the longest sorted subarrays of A

max ←
1

len ←
1

for i ←
า to m do

if arr Li ] > arr [ i -


า ] then

len ← len +1

else

it max < len then

max ← len

Ien ←
1

if max < len then


max

len

return max

i.
The
running
time of the algorithm is Ocn )

E-
±

Input [] { 5,6

3,5 7,8 9 1,2 }
=
arr
, , , ,

Subarray ะ { 3,5 , 7,8 ,


9 }

Output ะ
5

63160187 ขวัญจิรา คุ้มปากพิง


A-1.11 Given an array, A, of n positive integers, each of which appears in A exactly twice, except for one integer, a, describe an
O(n)-time method for finding a using only a single variable besides A.

Algorithm Outlier (A) ะ

Inpvt ะ
An n
-

element array
A of
positive integers
the outlier valuex
Output ะ

value ←
o

for i ← ◦ to n do

valve ←
value ^ Aci ]

return value

i.
The
running
time of the algorithm is 0 (1)

5
¥ -

Input ะ
A [] =

{ 7,3 5. 4,5 ,
,
3,4 ]

Output ะ 7

res =
7 n (3 ^ 3) n (4 ^ 4) ^ (5^5)

=
7 n 0 ^ 0 ^ 0

63160187 ขวัญจิรา คุ้มปากพิง

You might also like