A Meera array is defined to be an array such that for all values n in
the array, the value 2*n is
not in the array. So {3, 5, -2} is a Meera array because 3*2, 5*2 and -2*2 are
not in the array. But {8, 3, 4} is not a Meera array because for n=4,2*n=8 is in the array.
Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isMeera(int [ ] a)
If you are programming in C or C++, the function signature is
int isMeera(int a[ ], int len) where len is the number of elements in the array.
{
int isMeera = 1;
for (int i = 0; i < a.Length && isMeera == 1; i++)
{
int isValid = 0;
for (int j = 0; j < a.Length; j++)
{
if (a[i] == 2 * a[j])
{
isValid = 1;
break;
}
}
if (isValid == 1)
isMeera = 0;
}
return isMeera ;
}
Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isMeera(int [ ] a)
If you are programming in C or C++, the function signature is
int isMeera(int a[ ], int len) where len is the number of elements in the array.
Solution :
public static int isMeera(int[] a){
int isMeera = 1;
for (int i = 0; i < a.Length && isMeera == 1; i++)
{
int isValid = 0;
for (int j = 0; j < a.Length; j++)
{
if (a[i] == 2 * a[j])
{
isValid = 1;
break;
}
}
if (isValid == 1)
isMeera = 0;
}
return isMeera ;
}
can you please clarify the code
ReplyDeleteit returns 1 for {} array
and it returns 0 for {0}
shouldn't it be the other way arround
# this better code
Deletepublic static int isMeera(int[] a){
for (int i=0; i < a.length; i++){
for (int j = 0; j < a.length; j++){
if(a[i] * 2 == a[j] && i != j){
return 0;
}
}
}
return 1;
}