My problem is the following:
A certain type of bomb requires a detonator to activate. These bombs are made in such a way that only their detonator can activate them. This is done by making them have a different detonator and plug for each bomb.
You have in your possession 20 bombs with their respective 20 detonators. However, you don't know which detonator corresponds to which bomb and you need to know. You can't compare any detonator to any bomb, as for this you would need to plug them in and activate the bomb. You can only compare the detonators with each other, and the bombs with each other.
Take into account that in the locations from 1
to 20
are the sizes of the plugs of the bombs and from 21
to to are the 40
sizes of the detonators.
I have already solved it as follows (recursively):
public class DetonadorAndBomb {
public static void bomb(int []a,int m,int n){
if(a[m]==a[n+1]){
System.out.println(a[n+1]);
if(!(m==(n+1))){
n=1;
bomb(a,m+1,n);
}
} else{
bomb(a,m,n+1);
}
}
public static void main(String[] args) {
int num[] = {1,3,3,1};
bomb(num,0,1);
}
}
And in the console it results:
1
3
3
It is not supposed to print the last value.
Let's see if I understood:
Departure:
Possible recursive solution:
Code available with debugging messages to visually see how the recursive function works.