-6

I want to check if an array Is235. Is235 is an array that has an integer divisible by 2, another integer divisible by 3 and a third integer divisible by 5. The other integers in the array that aren’t divisible by either of 2, 3, or 5 when added to the integers divisible by 2, 3 and 5 should be equal to the total number of elements in the array. Return 1 if the array Is235, else return 0. Please note that array cannot contain negative integers or zero. I want to approach this in a brute force manner only, thanks for your help in advance. My wrong attempt -

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int n = a.length;
        int countOne = 0;
        int countTwo = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] / 2 == 0 || a[i] / 3 == 0 || a[i] / 5 == 0) {
                countOne++;
            }
        }
        for (int j = 0; j < a.length; j++) {
            if (a[j] / 2 != 0 || a[j] / 3 != 0 || a[j] / 5 != 0) {
                countTwo++;
            }
        }
        if (countOne + countTwo != n) {
            return 0;
        }
        return 1;
    }
}

My countOne and countTwo variables couldn't count the integers as i taught it would.

Tunde
  • 31
  • 7
  • 6
    I'm voting to close this question as off-topic because this is not a "do my homework for me" site – Scary Wombat Aug 28 '19 at 05:11
  • I was having issues adding my code, it there now. Thanks. – Tunde Aug 28 '19 at 05:16
  • The opposite of `(a == 0 || b == 0)` is `(a != 0 && b != 0)`. The `if` statement in the second loop uses `||` when it should use `&&`. – Andreas Aug 28 '19 at 05:19
  • "Count all that is, and count all that isn't. The sum of those counts should equal the total count." --- In what world wouldn't they equal? You just counted all the values, so how can that not equal to total count? That test makes no sense whatsoever. – Andreas Aug 28 '19 at 05:21
  • 1
    `a / 2 == 0` is not the test for whether `a` is *divisible* by 2. You need to use `%` operator, not `/` operator, to test that. – Andreas Aug 28 '19 at 05:23
  • 1
    @ScaryWombat If `a[j]` is 1, the result is false. – Andreas Aug 28 '19 at 05:24
  • @Andeas, thanks a lot, it worked. But for this input int[] a = {3, 3, 5, 7, 11} It's returning 1 instead of 0. – Tunde Aug 28 '19 at 05:24
  • @Hulk because the no integer is divisible by 2. – Tunde Aug 28 '19 at 05:29
  • You need to add a check that *each* of the numbers you are looking for is present. Currently you are only checking the sum. You can do that by maintaining 3 separate counts instead of `countOne`. – Hulk Aug 28 '19 at 05:29
  • @Hulk thanks, trying to wrap my head around it. – Tunde Aug 28 '19 at 05:39
  • Basically what you are trying to do is `if divisible by 2, 3 or 5 -> countOne++, else -> countTwo++`. So since all elements are counted for in one of the counters your total sum will always equal array length. As Andreas said earlier this check makes no sense currently. Are you expecting to `return 1` if and only if `countOne == array.length` or if atleast one number divisible by 2, 3 AND 5 is present? – Cray Aug 28 '19 at 06:10
  • @Tunde did my answer work? – Daniel_Kamel Aug 30 '19 at 13:43
  • @Daniel K, there has to be a number divisible by 2, another one divisible by 3 and a third number which would be divisible by 5, this is the first thing to check ,if this first check fails, it should return 0. The second thing is just to count all other numbers that are not divisible by either 2, 3 or 5, then it should return 1 because obviously the length of the array would be equal to the total number of the array elements. Thanks a l – Tunde Sep 01 '19 at 14:32
  • @Tunde did my answer work?? – Daniel_Kamel Sep 01 '19 at 14:37
  • @Daniel K, no it didn't. This input int[] arr = {2, 3, 5, 7, 11} it's returning 0 instead of 1. Thanks. – Tunde Sep 02 '19 at 00:06

3 Answers3

0

When you want to compare if an integer is divisible by a number then you should use the remainder operator

So here's the code:

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int n = a.length;
        int countOne = 0;
        int countTwo = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 == 0 || a[i] % 3 == 0 || a[i] / 5 == 0) {
                countOne++;
            }else{countTwo++;}
        }

        if (countOne + countTwo != n) {
            return 0;
        }else{return 1;}

    }
}

Also notice that there is no need to write that 2nd for loop as it isn't necessary at all and a bad practice if you can do the task using a single for loop.

Also as mentioned is the answer of the question, a for-each loop is better [performance wise] than the regular for loop so using a for-each loop would turn in into:

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int countOne = 0;
        int countTwo = 0;

        for (int i : a) {
            if (i % 2 == 0 || i % 3 == 0 || i / 5 == 0) {
                countOne++;
            }else{countTwo++;}
        }

        if (countOne + countTwo != a.length) {
            return 0;
        }else{return 1;}

    }
}
Daniel_Kamel
  • 610
  • 8
  • 29
0

Try this, I tested it and it works, you should use the remainder operator %:

public class Array {

    public static void main(String[] args) {

        int[] arr = {2, 3, 5, 7, 11};

        System.out.println(is235Array(arr));
    }

    public static int is235Array(int[] a) {
        int countOne = 0;
        int countTwo = 0;

        for (int i : a) {
            if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
                countOne++;
            }else{countTwo++;}
        }

        if (countOne + countTwo != a.length) {
            return 0;
        }else{return 1;}

    }
}
Daniel_Kamel
  • 610
  • 8
  • 29
0

Check if an array is 235. is235Array.

public class Array{

static int is235Array(int[] a){

    int countNonMultiples = 0;
    int countMultiplesOfTwo = 0;
    int countMultiplesOfThree = 0;
    int countMultiplesOfFive = 0;

    for (int i : a){
        if(i % 2 == 0 ){
            countMultiplesOfTwo++;
        }
        if(i % 3 == 0){
            countMultiplesOfThree++;
        }
        if(i % 5 == 0){
            countMultiplesOfFive++;
        }
        if(i % 2 != 0 && i % 3 != 0 && i % 5 != 0 ){
            countNonMultiples++;
        }
    }

    if(countMultiplesOfTwo + countMultiplesOfThree + countMultiplesOfFive + countNonMultiples != a.length){
        return 0;
    }
    return 1;
}

public static void main(String[] args) {
    int[] arr = {7,2,7,2,7,2,7,2,3,7,7};

    System.out.println(is235Array(arr));
}

}