The output completely matches conditions.
package com.codegym.task.task20.task2026;
/*
Rectangle algorithms
*/
import java.util.HashMap;
import java.util.HashSet;
public class Solution {
public static void main(String[] args) {
byte[][] a1 = new byte[][]{
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 1}
};
byte[][] a2 = new byte[][]{
{1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 1}
};
int count1 = getRectangleCount(a1);
System.out.println("count = " + count1 + ". Must be 2");
int count2 = getRectangleCount(a2);
System.out.println("count = " + count2 + ". Must be 4");
}
public static int getRectangleCount(byte[][] a) {
int result = 0;
for ( int i=0; i < a.length; i++ ){
for ( int j=0; j < a[i].length; j++ ) {
if ( a[i][j]==1 ) {
if ( i==0 && j==0 ) result++;
else if ( i==0 && a[i][j-1]==0 ) result++;
else if ( j==0 && a[i-1][j]==0 ) result++;
else if (i>0 && j>0 && a[i-1][j-1]==0) result++;
}
}
}
return result;
}
}