Algorithms

Suppose you are given a set P of integers and another integer x. We wish to use a T(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)

This is what I have:
Var x;
Function equate( list m)
// if list size is 0 empty or 1 print computation can not be done
If(length m >=1)
Divide the lists into two parts
Var list left ,right ;
Var int middle = length (m) /2;
For each x in before middle
Add x to the left // left is an array
Loop : For (int i=0;i< middle;i++ )
For (int j= 1;j<middle;j++)
multiply left (i)xleft(j)
if (left (i)xleft(j) =x )
print left (i) , left(j)
For each x in after middle before length(m) // right is an array
Add x to the right
Loop : For (int i=middle ; i< m ;i++ )
For (int j= middle ;j<m ;j++)
multiply right (i)x right (j)
if (right (i)xright (j) =x )
print right (i), right j

Is this correct. Please, do not make it complicated. I want to keep it simple. If it is correct or I have to change something please tell me the line, and what it should look like, write the line out! Thank you!

Comments

  • chipkahuchipkahu VietNam
    edited June 2014

    your algorithm output the integer pair overlap when it must be true. I think so. do not know right? if the list has the same number, the result will be identical.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion