Sunday, 24 December 2017

. Question :

An array is defined to be a Bean array if it meets the following conditions
   a. If it contains a 9 then it also contains a 13.
   b. If it contains a 7 then it does not contain a 16.

{1, 2, 3, 9, 6, 13}   // 9, 13 present. 7 not present. So 16 may or may not appear. Bean array.
{3, 4, 6, 7, 13, 15}  // 9 not present. So 13 may or may not appear. 7 present, 16 not present. Bean array.
{1, 2, 3, 4, 10, 11, 12}  // 9 not present. So 13 may or may not appear. 7 not present. So 16 may or may not appear. Bean array.
{3, 6, 5, 16, 13, 6, 17}  // 9 not present. So 13 may or may not appear. 7 not present. So 16 may or may not appear. Bean array.

{ 9, 6, 18}  // contains a 9 but no 13. Not a Bean array.
{4, 7, 16}  // contains both a 7 and a 16. Not a Bean array.

Write a function named isBean that returns 1 if its array argument is a Bean array, otherwise it returns 0.

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

If you are programming in C or C++, the function signature is

   int isBean (int a[ ], int len) where len is the number of elements in the array.

Solution:
public static int isBean(int [] a)
        {
            int result = 1;
            for(int i=0;i<a.Length;i++)
            {
                if(a[i]==9)
                {
                   for(int j=0;j<a.Length;j++)
                   {
                       if(a[j]==13)
                       {
                           result = 1;
                           break;
                       }
                       else
                       {
                           result = 0;
                       }
                   }
                }
                if(a[i]==7)
                {
                    for(int j=0;j<a.Length;j++)
                    {
                        if(a[j]==16)
                        {
                            result = 0;
                            break;
                        }
                    }
                }
            }
            return result;
        }        

0 comments:

Post a Comment

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