# Count of 0s in an N-level hexagon

Given an integer **N**, the task is to find the count of **0s** in an **N-level** hexagon.

**Examples:**

Input:N = 2

Output:7

Input:N = 3

Output:19

**Approach:** For the values of **N = 1, 2, 3, …** it can be observed that a series will be formed as **1, 7, 19, 37, 61, 91, 127, 169, …**. It’s a difference series where differences are in AP as **6, 12, 18, …**.

Therefore the **N ^{th}** term of will be 1 + {6 + 12 + 18 +…..(n – 1) terms}

= 1 + (n – 1) * (2 * 6 + (n – 1 – 1) * 6) / 2

= 1 + (n – 1) * (12 + (n – 2) * 6) / 2

= 1 + (n – 1) * (12 + 6n – 12) / 2

= 1 + (n – 1) * (6n) / 2

**= 1 + (n – 1) * (3n)**

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 ` `// 0s in an n-level hexagon ` `int` `count(` `int` `n) ` `{ ` ` ` `return` `3 * n * (n - 1) + 1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << count(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of ` ` ` `// 0s in an n-level hexagon ` ` ` `static` `int` `count(` `int` `n) ` ` ` `{ ` ` ` `return` `3` `* n * (n - ` `1` `) + ` `1` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(count(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# 0s in an n-level hexagon ` `def` `count(n): ` ` ` `return` `3` `*` `n ` `*` `(n ` `-` `1` `) ` `+` `1` ` ` `# Driver code ` `n ` `=` `3` ` ` `print` `(count(n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// 0s in an n-level hexagon ` `static` `int` `count(` `int` `n) ` `{ ` ` ` `return` `3 * n * (n - 1) + 1; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `Console.Write(count(n)); ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

**Output:**

19

## Recommended Posts:

- Diagonal of a Regular Hexagon
- Largest hexagon that can be inscribed within a square
- Number of coloured 0's in an N-level hexagon
- Area of hexagon with given diagonal length
- Find length of Diagonal of Hexagon
- Largest Square that can be inscribed within a hexagon
- Area of a circle inscribed in a regular hexagon
- Area of the Largest Triangle inscribed in a Hexagon
- Largest hexagon that can be inscribed within an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of all possible values of X such that A % X = B

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.