Given a **N x N** Chessboard. The task is to count distinct rectangles from the chessboard. For example, if the input is 8 then the output should be 36.

**Examples:**

Input:N = 4Output:10Input:N = 6Output:21

**Approach:**

Suppose N = 8 i.e. 8 x 8 chessboard is given, So different rectangles that can be formed are:

1 x 1, 1 x 2, 1 x 3, 1 x 4, 1 x 5, 1 x 6, 1 x 7, 1 x 8 = 8 2 x 2, 2 x 3, 2 x 4, 2 x 5, 2 x 6, 2 x 7, 2 x 8 = 7 3 x 3, 3 x 4, 3 x 5, 3 x 6, 2 x 7, 3 x 8 = 6 4 x 4, 4 x 5, 4 x 6, 4 x 7, 4 x 8 = 5 5 x 5, 5 x 6, 5 x 7, 5 x 8 = 4 6 x 6, 6 x 7, 6 x 8 = 3 7 x 7, 7 x 8 = 2 8 x 8 = 1

So total unique rectangles formed = 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 36 which is the sum of the first 8 natural numbers. So in general, distinct rectangles that can be formed in N x N chessboard is:

Sum of the first N natural numbers = N*(N+1)/2 = 8*(8+1)/2 = 36

Below is the implementation of the above approach:

## C++

`// C++ code to count distinct rectangle in a chessboard ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of distinct rectangles ` `int` `count(` `int` `N) ` `{ ` ` ` `int` `a = 0; ` ` ` `a = (N * (N + 1)) / 2; ` ` ` `return` `a; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `cout<<count(N); ` `} ` ` ` `// This code is contributed by nidhi16bcs2007 ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count unique rectangles in a chessboard ` `class` `Rectangle { ` ` ` ` ` `// Function to count distinct rectangles ` ` ` `static` `int` `count(` `int` `N) ` ` ` `{ ` ` ` `int` `a = ` `0` `; ` ` ` ` ` `a = (N * (N + ` `1` `)) / ` `2` `; ` ` ` ` ` `return` `a; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.print(count(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python code to count distinct rectangle in a chessboard ` ` ` `# Function to return the count ` `# of distinct rectangles ` `def` `count(N): ` ` ` `a ` `=` `0` `; ` ` ` `a ` `=` `(N ` `*` `(N ` `+` `1` `)) ` `/` `2` `; ` ` ` `return` `int` `(a); ` ` ` ` ` `# Driver Code ` `N ` `=` `4` `; ` `print` `(count(N)); ` ` ` `# This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count unique rectangles in a chessboard ` `using` `System; ` ` ` `class` `Rectangle ` `{ ` ` ` ` ` `// Function to count distinct rectangles ` ` ` `static` `int` `count(` `int` `N) ` ` ` `{ ` ` ` `int` `a = 0; ` ` ` ` ` `a = (N * (N + 1)) / 2; ` ` ` ` ` `return` `a; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `Console.Write(count(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

10

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of distinct rectangles inscribed in an equilateral triangle
- Count squares with odd side length in Chessboard
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Count of Rectangles with area K made up of only 1s from given Binary Arrays
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Puzzle | Program to find number of squares in a chessboard
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Find if two rectangles overlap
- Number of unique rectangles formed using N unit squares
- Number of rectangles in N*M grid
- Sum of Areas of Rectangles possible for an array
- Smallest square formed with given rectangles
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Maximum given sized rectangles that can be cut out of a sheet of paper

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.