Question :
An
array is defined to be odd-valent if it meets the following two
conditions:
a. It contains a value that occurs more than
once
b. It contains an odd number
For
example {9, 3, 4, 9, 1} is odd-valent because 9 appears more than once and 3 is
odd. Other odd-valent arrays are {3, 3, 3, 3} and {8, 8, 8, 4, 4, 7, 2}
The
following arrays are not odd-valent:
{1,
2, 3, 4, 5} - no value appears more than once.
{2,
2, 2, 4, 4} - there are duplicate values but there is no odd value.
Write
a function name isOddValent that returns 1 if its array argument is
odd-valent, otherwise it returns 0.
If
you are programming in Java or C#, the function prototype is
int isOddValent (int[ ] a);
If
you are programming in C or C++, the function prototype is
int isOddValent (int a[ ], int len) where
len is the number of elements in the array.
Solution :
public int isOddValent(int [] a)
{
int rtnVal
= 0;
bool isFirst = false,isSecond=false;
for(int i=0;i<a.Length-1;i++)
{
int count =
1;
for(int j=i+1;j<a.Length;j++)
{
if(a[i]==a[j])
{
count++;
if(count>1)
{
isFirst = true;
}
}
}
if(a[i]%2!=0)
{
isSecond = true;
}
}
if(isFirst
&& isSecond)
{
rtnVal = 1;
}
else
{
rtnVal = 0;
}
return rtnVal;
}
0 comments:
Post a Comment