Sunday, 24 December 2017

A Meera array is defined to be an array such that for all values n in the array, the value 2*n is not in the array. So {3, 5, -2} is a Meera array because 3*2, 5*2 and -2*2 are not in the array. But {8, 3, 4} is not a Meera array because for n=4,2*n=8 is in the array.

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

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

If you are programming in C or C++, the function signature is
   int isMeera(int a[ ], int len) where len is the number of elements in the array.

 Solution :
public static int isMeera(int[] a)
        {
            int isMeera = 1;

            for (int i = 0; i < a.Length && isMeera == 1; i++)
            {
                int isValid = 0;
                for (int j = 0; j < a.Length; j++)
                {
                    if (a[i] == 2 * a[j])
                    {
                        isValid = 1;
                        break;
                    }
                }

                if (isValid == 1)
                    isMeera = 0;
            }

            return isMeera ;
        }

2 comments:

  1. can you please clarify the code
    it returns 1 for {} array
    and it returns 0 for {0}

    shouldn't it be the other way arround

    ReplyDelete
    Replies
    1. # this better code
      public static int isMeera(int[] a){
      for (int i=0; i < a.length; i++){
      for (int j = 0; j < a.length; j++){
      if(a[i] * 2 == a[j] && i != j){
      return 0;
      }
      }
      }
      return 1;
      }

      Delete

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