Sunday, 24 December 2017

Question :
A twinoid is defined to be an array that has exactly two even values that are adjacent to one another. For example {3, 3, 2, 6, 7} is a twinoid array because it has exactly two even values (2 and 6) and they are adjacent to one another. The following arrays are not twinoid arrays.
{3, 3, 2, 6, 6, 7} because it has three even values.
{3, 3, 2, 7, 6, 7} because the even values are not adjacent to one another
{3, 8, 5, 7, 3} because it has only one even value.
Write a function named isTwinoid that returns 1 if its array argument is a twinoid array. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isTwinoid (int [ ] a);
If you are programming in C or C++, the function signature is

int isTwinoid(int a[ ], int len) where len is the number of elements in the array.
Solution :
public static int isTwinoid(int[] a)
        {

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] % 2 == 0)
                {
                    i++;
                    if (a[i] % 2 == 0)
                    {
                        i++;
                        for (; i < a.Length; i++)
                        {
                            if (a[i] % 2 == 0)
                            {
                                return 0;
                            }
                        }
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            return 0;
        }

3 comments:

  1. ---------this code in java ----in other methode----
    ---------------------------------------------------
    static int twinoid(int []a){
    int len=a.length;
    int count=0,isTwinOin=0;
    for(int i=0;i1){
    isTwinOin=0;
    break;
    }else
    if(count==1){
    isTwinOin=1;
    break;
    }}
    }else
    isTwinOin=0;
    }
    return isTwinOin;
    }

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. The soution above is nor right, the test array {2, 3,7,8,10} returns 0 yet it should return 1

    ReplyDelete

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page