I recently got this problem to find out the ksum from a pair of numbers and i got the solution but just looking to have a more optimized solution:
public class KSum {
public static void kSum(int[] array, int k){
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(k==(array[i]+array[j])){
System.out.println("pair=("+array[i]+","+array[j]+")");
}
}
}
}
public static void main(String[] args) {
int[] array={1,2,3,4,5,6,7};
kSum(array, 7);
}
}