-4

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.

Here is my solution please can you provide me most efficient solution of that and ,tell me how efficient my logic is here? no build-in functions and sorting etc. are allowed ,just core logic on Java , its ok in c or c++ . Solution

static int isFilter(int[] a){
    int i,len=a.length,j,result=1,found9=0,found11=0,found7=0,found13=0;
    for(i=0;i<len;i++){
        if(a[i]==9 ){
            found9=1;
            for(j=0;j<len; j++){
                if(a[j]==11){
                    found11=1;
                }
            }
        }

        if(a[i]==7){
            found7=1;
            for(j=0;j<len;j++){
                if(a[j]==13){
                    found13=1;
                }
            }
        }
    }

    if(found9==1 && found11!=1){
        result=0;
    }

    if(found7==1 && found13!=0){
        result=0;
    }

    return result;
}
FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • If you want a codereview, check out https://codereview.stackexchange.com/, *but read their guidelines first*. – Baum mit Augen May 14 '16 at 21:44
  • Possible duplicate of [What is the best way to filter a Java Collection?](http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection) – ivan_pozdeev May 15 '16 at 00:35

1 Answers1

2

mb its more efficient

static int isFilter(int[] a){
        boolean found9 = false, found11 = false, found7 = false, found13 = false;
        for (int i = 0; i < a.length; i++) {
            switch (a[i]) {
            case 7:
                found7 = true;
                break;
            case 9:
                found9 = true;
                break;
            case 11:
                found11 = true;
                break;
            case 13:
                found13 = true;
                break;
            }
        }
        if(found9 && found11) return 1;
        if(found7 && !found13) return 1;
        return 0;
    }
IT_Nike
  • 91
  • 6