I am looking to create a function in Java that will take in two arrays or lists and be able to tell if the first array (source) ‘fits into’ the second (target). The target array has values that can not be exceeded in the source array.
For example:
[ 16, 16, 16 ] will not fit into [ 13, 13, 22 ]
[ 12, 12 ] will fit into [ 16, 16, 12 ]
[ 12, 18, 14 ] will not fit into [ 10, 18, 14 ]
[ 12, 24 ] will fit into [ 10, 12, 24 ]
[ 10, 10, 10 ] will not fit into [ 10, 10 ]
My current attempt (IANA CS Major!) is ok for 3-element arrays, and that’s all I need to worry about for the short term, but I’m missing some logic in the inner loop that will prevent false negatives.
Gist: https://gist.github.com/1208514
private Boolean designFits(int[] max, int[] design) {
Boolean designFits = true;
Arrays.sort(max);
Arrays.sort(design);
int passCount = 0;
if(design.length <= max.length) {
for(int i = 0; i < max.length; i++) {
for(int j = 0; j < design.length; j++) {
if(max[i] <= design[j]) {
passCount++;
}
}
}
if(passCount == 0 || passCount > max.length) {
designFits = false;
}
} else {
designFits = false;
}
return designFits;
}
I think this achieves what you’re looking for: