2Sum problem Given an unsorted array of integers A 2 13 8 3

2-Sum problem:

Given an unsorted array of integers A = {2, 13, 8, 38, 63, 9, 11, 4, 1, 3} and a target sum Sum = 17

Goal:Using a hash implementation, determineif there exists two integers x, y in A such that x + y = Sum

Assume that there is only one solution to the sum. & Please use hash implementation for this. Thank you.

Solution

public class Test

{

public static void main (String args[])

{

HashTable h=new HashTable();

int A[]={2,13,8,38,63,9,11,4,1,3};

for(int i=0;I<A.length();i++)

{

h.put(new String(new Integer(i)),new Integer(A[i]);

}

String x,y;

for(String s:h.keys())

{

for(String t:h.keys())

{

if(s!=t)

{

if(h.get(s).IntValue()+h.get(t).IntValue()==17)

{

x=s;

y=t;

}

}

}

}

System.out.println(\"elements\"+x+\" and \"+y+\" give sum 17 \");

System.out.println(\"values are \"+h.get(x)+\" and \"+h.get(y));

}

}

2-Sum problem: Given an unsorted array of integers A = {2, 13, 8, 38, 63, 9, 11, 4, 1, 3} and a target sum Sum = 17 Goal:Using a hash implementation, determinei
2-Sum problem: Given an unsorted array of integers A = {2, 13, 8, 38, 63, 9, 11, 4, 1, 3} and a target sum Sum = 17 Goal:Using a hash implementation, determinei

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site