I'm trying to write a method that takes as input 2 integer arrays and compares them based on the binary & between each element and takes into consideration if one array is derived from the other. Also the size of the arrays is known and equal.
So basically if the method takes as input A = [2, 0, 0, 0] and B = [6, 0, 0, 3] it should return true because (2 & 6) != 0 (careful with the brackets here hehe) but if it takes the opposite A = [6, 0, 0, 3] and B = [2, 0, 0, 0] it should return false because A is derived from B (or B is contained in A).
An array is contained in another if by applying the & operator between two elements located at the same position you get true (basically the case where the method should return true).
So far I've got the following code which obviously doesn't work because it doesn't take into consideration if one array is derived from the other. All the operations I can think of using are commutative and it doesn't help.
private boolean checkContainment(int[] firstArr, int[] secondArr)
{
List<Integer> posList = new ArrayList<Integer>();
for(int i = 0;i < firstArr.length;i++)
{
if((firstArr[i] & secondArr[i]) != 0)
{
posList.add(i);
}
}
for(int j = 0;j < firstArr.length;j++)
{
if(posList.size() > 0)
{
for(int k : posList)
{
if((j != k) && (firstArr[j] != secondArr[j]))
{
return true;
}
}
}
}
return false;
}
I'm pretty sure it's quite simple to tweak the following code in order to get it right but I can't get my head around it.