You could write a utility method as follows (assumes the array is sorted before hand please see the update if it's not the case):
public static boolean checkIfOnFire(int[] arr, int... nums) {
for(int n : nums) {
if(Arrays.binarySearch(arr, n) < 0) {
return false;
}
}
return true;
}
Test with:
int[] myarray = new int[] {1,2,3,4,5};
System.out.println(checkIfOnFire(myarray, 1, 2, 3));
System.out.println(checkIfOnFire(myarray, 1, 2, 7));
Prints:
true
false
Update:
Since I am using the Arrays.binarySearch
. The input array to the checkIfOnFire
method has to be sorted as it is in your example.
But if your array might not be sorted then checkIfOnFire
has to be modified as follows:
public static boolean checkIfOnFire(int[] arr, int... nums) {
//make a copy of the original array
int[] copyArr = new int[arr.length];
System.arraycopy(arr, 0, copyArr, 0, arr.length);
//sort the copy
Arrays.sort(copyArr);
for(int n : nums) {
if(Arrays.binarySearch(copyArr, n) < 0) {
return false;
}
}
return true;
}