Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

T(nlgn) algorithm

$
0
0
The question:
Suppose you are given a set P of integers and another integer x. We wish to use a Θ(nlgn) algorithm to decide whether there are 2 integers in P and the product (multiplication) of these two integers equals to x. Show your algorithm. (You can use pseudo code or by illustration only)

My answer:
Var x;
Function equate(list m)
If(length.m >=1)
Var l.list, r.list;
Var int middle = length.m /2;
For(int i = 0; i < middle; i++)
For(int j = 1;j < middle; j++)
l.list(i)*l.list(j)
if(l.list(i)*l.list(j) =x)
print l.list(i), l.list(j)
For(int i = middle; i < m; i++)
For(int j = middle; j < m; j++)
r.list(i)*r.list(j)
if(r.list(i)*r.list(j) = x)
print r.list(i),r.list(j)

I want to make sure if this is correct! If I have to change something please show me on my code what I have to change, and explain! Thanks!

Viewing all articles
Browse latest Browse all 2703

Trending Articles