Given an array of ints, return true if the number of 1's is greater than the number of 4's

Let's understand the problem by following examples.

Ex1: [1, 4, 1] → true
Ex2: [1, 4, 1, 4] → false
Ex3: [1, 1] → true
Ex4: [1, 4, 4, 4] → false
Ex51: [1, 1, 1, 4] → true

Solution.

public class arrayProblem {

public static void main(String[] args) {

int [] a = {1,1,1,4,4};

int countof_1=0, countof_4=0;

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

if (a[i]==1){

countof_1++;
}

else if (a[i]==4){

countof_4++;
}
}

if (countof_1>countof_4){

System.out.println(true);
}

else {

System.out.println(false);
}
}
}


0 comments:

Post a Comment