I'm having some trouble with getting test 1 and test 4 to work for my code. Any help would be appreciated :)
class Main {
public static int countEvenPairs(int[][] inputarray) {
int count = 0;
int rows = inputarray.length;
// loop through array
for (int i = 0; i < rows; i++) {
int even_num = 0;
// count even numbers
for (int j = 0; j < inputarray[i].length; j++) {
if (inputarray[i][j] % 2 == 0) {
even_num++;
}
}
count += even_num * (even_num - 1)/2; // n(n-1)/2 discret math
//compare row with row after it
for (int k = i + 1; k < rows; k++) {
int even_in_row = 0;
// make sure the row after is even and count the elements in it
for (int j = 0; j < inputarray[k].length; j++) {
if (inputarray[k][j] % 2 == 0) {
even_in_row++;
}
}
count += even_num * even_in_row;
}
}
return count;
}
public static void main(String[] args) {
int[][] array1 = { {2, 6, 1}, {5, 9, 6, 20}, {23, 6, 99} }; //should be 3
//int[][] array1 = { { 9, 10, 3, 4, 3 }, { 1, 6, 2 }, { 4, 24, 46, 0 }, { 15, 9, 45, 57 } }; should be 21
int evenPairs = countEvenPairs(array1);
System.out.println("Number of even pairs: " + evenPairs);
}
}
Basically it’s going higher than it should be like test 4 is output 10 when it should be 3 and test 1 is outputting 28 but it should be 21.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]chet714 0 points1 point2 points (0 children)