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.
This is a Java program
Solution
public class Sum
{
public static void main(String[] args)
{
int a[]={2, 13, 8, 38, 63, 9, 11, 4, 1, 3};
int[] b = new int[10];
int k=0,c=0;
int flag=0;
int n = a.length;
int n1 = b.length;
int targetsum=17;
for (int i=0;i<n-1 ;i++ ){
for (int j=1;j<n ;j++ ){
if (a[i]+a[j]==17){
for (k=0; k<n1; k++){
if (b[k]==a[i])
flag=1;
}
if (flag==0){
b[c]=a[i];
c++;
b[c]=a[j];
c++;
}
}
}
}
for (int i=0;b[i]!=0 ;i++ ){
System.out.println(b[i] +\" and \" +b[i+1]);
i=i+1;
}
}
}
