# Count of squares that can be drawn without lifting the pencil

Given an integer **N**, the task is to find the count of squares of side **1** that can be drawn without lifting the pencil, starting at one corner of an **N * N** grid and never visiting an edge twice.

Input:N = 2

Output:2

Input:N = 3

Output:5

**Approach:** It can be observed that for the values of **N = 1, 2, 3, …**, a series will be formed as **1, 2, 5, 10, 17, 26, 37, 50, …** whose **N ^{th}** term is

**(N**

^{2}– (2 * N) + 2)Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// squares that can be formed ` `int` `countSquares(` `int` `n) ` `{ ` ` ` `return` `(` `pow` `(n, 2) - (2 * n) + 2); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2; ` ` ` ` ` `cout << countSquares(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// squares that can be formed ` `static` `int` `countSquares(` `int` `n) ` `{ ` ` ` `return` `(` `int` `) (Math.pow(n, ` `2` `) - (` `2` `* n) + ` `2` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` `System.out.println(countSquares(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# squares that can be formed ` `def` `countSquares(n) : ` ` ` ` ` `return` `(` `pow` `(n, ` `2` `) ` `-` `(` `2` `*` `n) ` `+` `2` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `2` `; ` ` ` ` ` `print` `(countSquares(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of ` ` ` `// squares that can be formed ` ` ` `static` `int` `countSquares(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `) (Math.Pow(n, 2) - (2 * n) + 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `n = 2; ` ` ` `Console.WriteLine(countSquares(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(1)

## Recommended Posts:

- Count digits in a factorial | Set 2
- Count of Binary Digit numbers smaller than N
- Count numbers that don't contain 3
- Count the number of possible triangles
- Count numbers with same first and last digits
- Count digits in given number N which divide N
- Count all possible paths from top left to bottom right of a mXn matrix
- Count trailing zeroes in factorial of a number
- Count Possible Decodings of a given Digit Sequence
- Count ways to reach the n'th stair
- Print squares of first n natural numbers without using *, / and -
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Count inversions in an array | Set 3 (Using BIT)
- Count numbers from 1 to n that have 4 as a digit
- Count factorial numbers in a given range

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.