Tuesday, February 22, 2011

Given a list of numbers and a number x, find two numbers in the list such that product of those 2 numbers is equal to x

import java.util.*;

class twonummulti_equalto_x
{

public static void main(String[] args)
{

int x = 21;
int[] arr = {4, 7, 3, 2, 4, 7, 6, 5, 2};

Set set = new HashSet ();
for (int i=0; i{
set.add(arr[i]);
}

Iterator itr = set.iterator();

while (itr.hasNext())
{
int i = itr.next();
int div = x/i;
int mod = x%i;
if (mod==0 && set.contains(div))
{
System.out.println(i+"x"+div+"="+x);
}

}

}


}

No comments :