Skip to main content

Daphne array

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

Popular posts from this blog

wave array

A   wave   array   is defined to an array which does   not   contain   two even numbers   or two odd numbers in adjacent locations. So {7, 2, 9, 10, 5}, {4, 11, 12, 1, 6},   {1, 0, 5} and {2} are all   wave arrays . But { 2, 6 , 3, 4} is not a   wave   array because the even numbers 2 and 6 are adjacent to each other.   Also   {3, 4, 9, 11, 8}   is not a wave array because the   odd   numbers   9   and   11   are adjacent to each other.   You can assume array has at least one element.            Write a function   named   isWave   that returns 1 if its array argument is a   Wave   array, otherwise it returns 0. If you are programming in Java or C#, the function signature is    int isWave   (int [ ] a) If you are programming in C or C++, the function signature is    int is...

minDistance

Write a function named   minDistance   that returns the smallest distance between two   non-trivial   factors of   a number. For example, consider   63. Its   non-trivialfactors are 3,   7, 9   and 21. Thus   minDistance (63) would return 2 because the   smallest distance between any two   non-trivial factors is 2 (9 - 7   = 2).   As another example,   minDistance   (25) would return 0   because   25 has only   one   non-trivial   factor:   5. Thus   the smallest distance   between any two   non-trivial   factors is 0   (5   -   5 = 0).   Note that   minDistance (11)   would return -1 since 11 has no non-trivial factors. The function signature is    int   minDistance (int n)  Solution :        public static int minDistance ( int n) { int minDist = n, co...