Sunday, 24 December 2017

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

So {1, 2, 3, 9, 6, 11} and {3, 4, 6, 7, 14, 16}, {1, 2, 3, 4, 10, 11, 13} and {3, 6, 5, 5, 13, 6, 13} are Filter arrays. The following arrays are not Filter arrays: {9, 6, 18} (contains 9 but no 11), {4, 7, 13} (contains both 7 and 13)

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

If you are programming in Java or C#, the function signature is
   int isFilter(int[ ] a)
If you are programming in C or C++, the function signature is
   int isFilter(int a[ ], int len) where len is the number of elements in the array.
public static int isFilter(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]==11)
                        {
                            result = 1;
                            break;
                        }
                        else
                        {
                            result = 0;
                        }
                    }
                }
                if(a[i]==7)
                {
                    for(int j=0;j<a.Length;j++)
                    {
                        if(a[j]==13)
                        {
                            result = 0;
                            break;
                        }
                    }
                }
            }
            return result;

        }

3 comments:

  1. // O(N) Time | O(1) Space
    static int isFilter(int[] arr) {
    boolean has9 = false;
    boolean has11 = false;

    boolean has7 = false;
    boolean has13 = false;

    for (int i = 0; i < arr.length; i++) {
    if (arr[i] == 9)
    has9 = true;
    if (arr[i] == 11)
    has11 = true;

    if (arr[i] == 7)
    has7 = true;
    if (arr[i] == 13)
    has13 = true;
    }

    if (has9 == true) {
    if (has11 == true)
    return 1;
    else
    return 0;
    }

    if (has7 == true) {
    if (has13 == false)
    return 1;
    else
    return 0;
    }

    return 1;
    }

    ReplyDelete
    Replies
    1. the solution has a problem
      if the array contains 9, 11 7 and 13 at the same time it returns 1;

      Delete
    2. I corrected your function

      static int isFilter(int[] arr) {

      boolean has9 = false, has11 = false, has7 = false, has13 = false ;

      for (int i = 0; i < arr.length; i++) {

      if (arr[i] == 9)
      has9 = true;
      if (arr[i] == 11)
      has11 = true;

      if (arr[i] == 7)
      has7 = true;
      if (arr[i] == 13)
      has13 = true;
      }

      if (has9 )
      if (! has11 )
      return 0 ;

      if (has7)
      if (has13)
      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