I came up with 2 different solutions in Java.
First one is:
public static void main(String[] args) {
int[] array = new int[]{10,3,4,6,1,9,10,0};
int sum=0;
for (int i=0;i<array.length;i++)
{
for (int j=i ; j<array.length;j++){
sum = array[i] + array[j];
if (sum==10){
System.out.println(array[i] + " " + array[j]);
}
}
}
I believe this one works O(N^2)in \$O(N^2)\$.
My second solution is:
public static void main(String[] args) {
int[] nums = new int[] {10, 3, 4, 6, 1, 9, 10, 0};
Hashtable<Integer, Integer> reqNoList = new Hashtable<Integer, Integer>();
int sum = 10;
int i = 0, count = 0;
while (i < nums.length && i < sum) {
int key = sum - nums[i];
if (reqNoList.containsKey(nums[i])) {
Integer temp = reqNoList.get(nums[i]);
System.out.println(key + " " + nums[i]);
count++;
} else {
reqNoList.put(key, i);
}
i++;
}
}
}
I tried to do it O(N)in \$O(N)\$ but I have no idea.