Question :
A Daphne array is an array that contains either all odd numbers or all even numbers. For example, {2, 4, 2} (only even numbers) and {1, 3, 17, -5} (only odd numbers) are Daphne arrays but {3, 2, 5} is not because it contains both odd and even numbers. Write a function named isDaphne that returns 1 if its array argument is a Daphne array. Otherwise it returns 0.
If you are programming in Java or C#, the function prototype is
int isDaphne (int[ ] a);
If you are programming in C or C++, the function prototype is
int isDaphne (int a[ ], int len) where len is the number of elements in the array.
Solution :
public int isDaphne(int [] a) { bool isEven = false, isOdd = false; for(int i=0;i<a.Length;i++) { if(a[i]%2==0) { isEven = true; } else { isOdd = true; } } if(isEven && isOdd) { return 0; } else { return 1; } }
Comments
Post a Comment