# 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 = 2Output:2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

DSA Self Paced Courseat a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please referComplete Interview Preparation Course.In case you wish to attend

live classeswith experts, please referDSA Live Classes for Working ProfessionalsandCompetitive Programming Live for Students.

Input:N = 3Output: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;` `}` |

## 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` |

## 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` |

## 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` |

## Javascript

`<script>` `// Javascript implementation of the approach` `// Function to return the count of` `// squares that can be formed` `function` `countSquares(n)` `{` ` ` `return` `(Math.pow(n, 2) - (2 * n) + 2);` `}` `// Driver code` `var` `n = 2;` `document.write(countSquares(n));` `</script>` |

**Output:**

2

**Time Complexity:** O(1)