Given two integers **N** and **K**, the task is to find the number of squares of size **K** that is inscribed in a square of size **N**.

**Examples:**

Input:N = 4, K = 2Output:9Explanation:

There are 9 squares of size 2 inscribed in a square of size 4.

Input:N = 5, K = 3Output:9Explanation:

There are 9 squares of size 3 inscribed in a square of size 5.

**Approach: **The key observation to solve the problem is that the total number of squares in a square of size **N** is **(N * (N + 1)* (2*N + 1)) / 6**. Therefore, the total number of squares of size **K **possible from a square of size **N** are:

Below is the implementation of the above approach:

## C++

`// C++ implementation of the` `// above approach` `#include <iostream>` `using` `namespace` `std;` `// Function to calculate the number` `// of squares of size K in a square` `// of size N` `int` `No_of_squares(` `int` `N, ` `int` `K)` `{` ` ` `// Stores the number of squares` ` ` `int` `no_of_squares = 0;` ` ` `no_of_squares` ` ` `= (N - K + 1) * (N - K + 1);` ` ` `return` `no_of_squares;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Size of the` ` ` `// bigger square` ` ` `int` `N = 5;` ` ` `// Size of` ` ` `// smaller square` ` ` `int` `K = 3;` ` ` `cout << No_of_squares(N, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the` `// above approach` `import` `java.util.*;` `class` `GFG{` `// Function to calculate the ` `// number of squares of size ` `// K in a square of size N` `static` `int` `No_of_squares(` `int` `N, ` ` ` `int` `K)` `{` ` ` `// Stores the number ` ` ` `// of squares` ` ` `int` `no_of_squares = ` `0` `;` ` ` `no_of_squares = (N - K + ` `1` `) * ` ` ` `(N - K + ` `1` `);` ` ` `return` `no_of_squares;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Size of the` ` ` `// bigger square` ` ` `int` `N = ` `5` `;` ` ` `// Size of` ` ` `// smaller square` ` ` `int` `K = ` `3` `;` ` ` `System.out.print(No_of_squares(N, K));` `}` `}` `// This code is contributed by Princi Singh` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the` `# above approach` `# Function to calculate the` `# number of squares of size` `# K in a square of size N` `def` `No_of_squares(N, K):` ` ` ` ` `# Stores the number` ` ` `# of squares` ` ` `no_of_squares ` `=` `0` `;` ` ` `no_of_squares ` `=` `(N ` `-` `K ` `+` `1` `) ` `*` `(N ` `-` `K ` `+` `1` `);` ` ` `return` `no_of_squares;` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Size of the` ` ` `# bigger square` ` ` `N ` `=` `5` `;` ` ` `# Size of` ` ` `# smaller square` ` ` `K ` `=` `3` `;` ` ` `print` `(No_of_squares(N, K));` `# This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the` `// above approach` `using` `System;` `class` `GFG{` `// Function to calculate the ` `// number of squares of size ` `// K in a square of size N` `static` `int` `No_of_squares(` `int` `N, ` `int` `K)` `{` ` ` ` ` `// Stores the number ` ` ` `// of squares` ` ` `int` `no_of_squares = 0;` ` ` ` ` `no_of_squares = (N - K + 1) * ` ` ` `(N - K + 1);` ` ` ` ` `return` `no_of_squares;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` ` ` `// Size of the` ` ` `// bigger square` ` ` `int` `N = 5;` ` ` ` ` `// Size of` ` ` `// smaller square` ` ` `int` `K = 3;` ` ` ` ` `Console.Write(No_of_squares(N, K));` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity: **O(1) **Auxiliary Space: **O(1)

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:

- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Count cubes of size K inscribed in a cube of size N
- Biggest Square that can be inscribed within an Equilateral triangle
- Program to calculate area of an Circle inscribed in a Square
- Largest square that can be inscribed in a semicircle
- Area of the Largest square that can be inscribed in an ellipse
- Program to find the side of the Octagon inscribed within the square

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.