Given two integers **N** and **K**, the task is to find the number of cubes of size **K** that can be contained in a cube of size N.

**Examples:**

Input:N = 2, K = 1Output:8Explanation:

There are 8 cubes of size 1 that can be drawn inside the bigger cube of size 2.

Input:N = 5, K = 2Output:64Explanation:

There are 64 cubes of size 2 can be drawn inside the bigger cube of size 5.

**Approach: **The key observation to solve the problem is that the number of cubes inside the cube of size **N** is **(N ^{2} * (N+1)^{2})/4**. Therefore, the cubes of size

**K**inside the cube of size N is:

Below is the implementation of the above approach:

## C++

`// C++ implementation of the` `// above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the number` `// of the cubes of the size K` `int` `No_of_cubes(` `int` `N, ` `int` `K)` `{` ` ` `int` `No = 0;` ` ` `// Stores the number of cubes` ` ` `No = (N - K + 1);` ` ` `// Stores the number of cubes` ` ` `// of size k` ` ` `No = ` `pow` `(No, 3);` ` ` `return` `No;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Size of the bigger cube` ` ` `int` `N = 5;` ` ` `// Size of the smaller cube` ` ` `int` `K = 2;` ` ` `cout << No_of_cubes(N, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the` `// above approach` `class` `GFG{` `// Function to find the number` `// of the cubes of the size K` `static` `int` `No_of_cubes(` `int` `N, ` ` ` `int` `K)` `{` ` ` `int` `No = ` `0` `;` ` ` `// Stores the number of cubes` ` ` `No = (N - K + ` `1` `);` ` ` `// Stores the number of cubes` ` ` `// of size k` ` ` `No = (` `int` `) Math.pow(No, ` `3` `);` ` ` `return` `No;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Size of the bigger cube` ` ` `int` `N = ` `5` `;` ` ` `// Size of the smaller cube` ` ` `int` `K = ` `2` `;` ` ` `System.out.print(No_of_cubes(N, K));` `}` `}` `// This code is contributed by Princi Singh` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the` `# above approach` ` ` `# Function to find the number` `# of the cubes of the size K` `def` `No_of_cubes(N, K):` ` ` ` ` `No ` `=` `0` ` ` ` ` `# Stores the number of cubes` ` ` `No ` `=` `(N ` `-` `K ` `+` `1` `)` ` ` ` ` `# Stores the number of cubes` ` ` `# of size k` ` ` `No ` `=` `pow` `(No, ` `3` `)` ` ` `return` `No` `# Driver Code` `# Size of the bigger cube` `N ` `=` `5` ` ` `# Size of the smaller cube` `K ` `=` `2` ` ` `print` `(No_of_cubes(N, K))` `# This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the` `// above approach` `using` `System;` ` ` `class` `GFG{` ` ` `// Function to find the number` `// of the cubes of the size K` `static` `int` `No_of_cubes(` `int` `N, ` `int` `K)` `{` ` ` `int` `No = 0;` ` ` ` ` `// Stores the number of cubes` ` ` `No = (N - K + 1);` ` ` ` ` `// Stores the number of cubes` ` ` `// of size k` ` ` `No = (` `int` `)Math.Pow(No, 3);` ` ` `return` `No;` `}` ` ` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` ` ` `// Size of the bigger cube` ` ` `int` `N = 5;` ` ` ` ` `// Size of the smaller cube` ` ` `int` `K = 2;` ` ` ` ` `Console.Write(No_of_cubes(N, K));` `}` `}` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

**Output:**

64

**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:

- 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 within a cube which is in turn inscribed within a right circular cone
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Largest cube that can be inscribed within the sphere
- Largest sphere that can be inscribed inside a cube
- Largest cone that can be inscribed within a cube
- Largest cube that can be inscribed within a right circular cone
- Largest cube that can be inscribed within a right circular cylinder
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- 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
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle

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.