Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active October 18, 2017 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/5b2140f14b567c6c7621d9fd4d07a664 to your computer and use it in GitHub Desktop.
Save thmain/5b2140f14b567c6c7621d9fd4d07a664 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class NutsAndBoltsBruteForce {
public static void match(char [] nuts,char [] bolts){
for (int i = 0; i <nuts.length ; i++) {
char nut = nuts[i];
for (int j = 0; j <bolts.length ; j++) {
if(nut==bolts[j]){
swap(bolts,i,j);
break;
}
}
}
System.out.println("Matched nuts and bolts are: ");
System.out.println(Arrays.toString(nuts));
System.out.println(Arrays.toString(bolts));
}
public static void swap(char [] bolts, int i, int j){
char temp = bolts[i];
bolts[i] = bolts[j];
bolts[j] = temp;
}
public static void main(String[] args) {
char [] nuts = {'$', '%', '&', 'x', '@'};
char [] bolts = {'%', '@', 'x', '$', '&'};
match(nuts,bolts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment