An array is defined to be a Filter array if it meets the following conditions
a. If it contains 9 then it also contains 11.
b. If it contains 7 then it does not contain 13
So {1, 2, 3, 9, 6, 11} and {3, 4, 6, 7, 14, 16}, {1, 2, 3, 4, 10, 11, 13} and {3, 6, 5, 5, 13, 6,
13} are Filter arrays. The following arrays are not Filter arrays: {9, 6, 18} (contains 9 but
no 11), {4, 7, 13} (contains both 7 and 13)
Write a function named isFilter() that returns 1 if its array argument is a Filter array,
otherwise it returns 0. (java)
Answers
Answer:
public static int isFilter(int [] a)
{
int result = 1;
for(int i=0;i<a.Length;i++)
{
if(a[i]==9)
{
for(int j=0;j<a.Length;j++)
{
if(a[j]==11)
{
result = 1;
break;
}
else
{
result = 0;
}
}
}
if(a[i]==7)
{
for(int j=0;j<a.Length;j++)
{
if(a[j]==13)
{
result = 0;
break;
}
}
}
}
return result;
}
Email This
BlogThis!
Share to Twitter
Share to Facebook
Explanation:
Hello