Amazon | Phone Screen | Count Number of Squares in a grid
Anonymous User
3842

Find a total number of overlapping squares in a given gird.

**
**

Above matrix should return 5 as it is having 5 total squares in it (each star forms one square and all four stars form one square).

Code->

private int getSquaresCount(int n, int m) {
    int maxSize = Math.min(n,m);
    int count = 0;
    for (int i = 1; i < maxSize; i++) {
        count += (m - i + 1) * (n - i + 1); 
    }    
    return count;
}
Comments (9)