Sunday 24 December 2017

Meera array is defined to be an array that contains at least one odd number and  begins and ends with the same number of even numbers.

So {4, 8, 6, 3, 2, 9, 8,11, 8, 13, 12, 12, 6} is a Meera array because it begins with three even numbers and ends with three even numbers and it contains at least one odd number

The array {2, 4, 6, 8, 6} is not a Meera array because it does not contain an odd number.

The array {2, 8, 7, 10, -4, 6} is not a Meera array because it begins with two even numbers but ends with three even numbers.

Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise, it returns 0.

If you are writing in Java or C#, the function signature is
   int isMeera (int[ ] a)

If you are writing in C or C++, the function signature is
   int isMeera (int a[ ], int len) where len is the number of elements in the array.
 public static int isMeera(int[] a)
        {
            bool isOdd = false, firstCountEnd = false, lastCountEnd = false;
            int firstCount = 0, lastCount = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] % 2 != 0)
                {
                    isOdd = true;
                    break;
                }
            }
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] % 2 != 0)
                    firstCountEnd = true;
                if (!firstCountEnd && a[i] % 2 == 0)
                    firstCount++;
                if (a[(a.Length - 1) - i] % 2 != 0)
                    lastCountEnd = true;
                if (!lastCountEnd && a[(a.Length - 1) - i] % 2 == 0)
                    lastCount++;
            }
            if (isOdd && firstCount == lastCount)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

3 comments:

  1. # this is better code

    public static int meeraArray(int[] a){

    int evenCount, oddNum, evenNum ;

    evenCount = oddNum = evenNum = 0;
    for (int i = 0; i < a.length; i++){
    if(a[i] % 2 == 0 && oddNum == 0){
    evenCount += 1;
    evenNum = evenCount;

    }
    else if(a[i] % 2 != 0 && oddNum == 0){
    oddNum += 1;

    }

    else if (a[i] % 2 == 0 && oddNum != 0){
    if( i +1 < a.length && a[i+1] % 2 != 0){
    evenCount = evenNum;

    }
    else{ evenCount -= 1;}

    }
    }
    if (evenCount != 0 || oddNum ==0){ return 0;}

    return 1;
    }

    ReplyDelete
  2. This is an O(n) solution, let me know your thoughts. Thanks

    public static int isMeera(int [] a){
    boolean foundOdd=false;
    boolean countingEvens=true; //count evens and later odds
    int evensCount=0;
    for(int i=0;i0) return 1;
    else return 0;
    }

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

    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