here I only did with one loop, but you will need 2 loops. To compare each element of array1 to each element of array2
2) If difference if less then minimum then that will be the minimum.
3) every time you get a minimum store your array1[j] and array2[j].
eg:
say array3[length].
if you get the minimum then do length = length +2 since you have used 2 spaces.
hope this helps :)
Yes, there is! billy is a 1D array. In line 6, you attempt to store three ints in a memory location that can hold only one...
I'm assuming what biplav17 meant was, after you find a pair (that gives you minimum difference), you store the ints in adjacent blocks of the result array. For example, suppose you find that the ith element of array1 matches with jth element of array two, then:
1 2 3
array3[length]=array1[i];
array3[length+1]=array2[j];
length+=2; //Equivalent to length=length+2;
You really can't remove an element of an array that easily. You have basically 2 options:
1) create a new array not containing the removed element (not a really efficient one but it's straight forward)
2) place the elements you don't need a t the end of your array and don't use them (if your array has N elements afterward it will use only the first N-1 elements this way)
3) As a variation of this 2nd method you can keep a track of the "removed" elements and skip them when you encounter them without moving them to a different point.
Also I did not get some point of your desired algorithm:
How are you going to choose which element to pair if more than one match the same element. From you example you have:
array a1: [34,25,27,86]
array a2: [75,87,96,98]
Apparently a1[0] should match a2[3] but also do a1[1] and a1[2] and besides the difference for those from a2[3] is bigger then that of abs(a1[0]-a2[0]). In this case which criterion are you going to use? Also if you intend to remove the elements them obviously different pairs will evolve.
eypros, in your example, why would a1[0] match with a2[3]? By what he's told us, and what I've understood, he wants to pair up numbers which have the least difference between them. I know this would result in different answers, depending on choice of your first array, and your first element. So we're taking the first array as the comparison factor, and going through it element by element, comparing with the other array.