Hey guys,
another one that I got stuck on(Although I passed 4000 points, soon to be on the leader board!:D) ... so here is the idea
I have an input array for example {1,2,3,4,5,6,7} and an int number for example 10; so what I have to do is return all combinations for which the sum of 2 numbers will be 10. such as {{3,7},{4,6}}.
The problems that I am facing is that I have to return a 2d array and I can't know its size until everything is checked. so I tried something simple just to get all into 1d array and then make sets out of that array. But problem again is the size of the 1d array. Here is what I have
public static int[] Puzzle(int[] numbers, int x) {
int position=0;
int[] a = new int[numbers.length];
for (int i = 0; i<numbers.length/2; i++){
for (int j=0;j<numbers.length;j++){
if((numbers[j]+numbers[i])== x) {
a[position] = i;
position++;
a[position] = j;
position++;
}
}
}
return a;
}
if my input is {512,1,3,5,3} and 4 , my 1d array has 1,2,1,4,0 then again the size is a bit too much and has to be redifined. I think a solution would be to make at this point new array of size position and move all from a to the new one and then copy it once again into 2d? Seems inefficient somehow