Given a pyramid, formed using unit area cubes, of given height H. The pyramid is then placed on the ground and painted from outside. The task is to find the count of cubes that are left uncoloured.
Examples:
Input: H = 3
Output: 1
Explanation:

Input: H = 1
Output: 0
Naive Approach: The number of cubes that will remain uncoloured is the number of cubes that are not present in the boundary of the pyramid. So, for every layer of cubes at height h, the number of uncoloured cubes is
where h>1. So, add the number of uncoloured cubes at every level to get the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int uncolouredCubes( int H)
{
int res = 0;
for ( int h = 2; h <= H; h++) {
res += (h - 2) * (h - 2);
}
return res;
}
int main()
{
int H = 3;
cout << uncolouredCubes(H);
}
|
Java
class GFG{
static int uncolouredCubes( int H)
{
int res = 0 ;
for ( int h = 2 ; h <= H; h++) {
res += (h - 2 ) * (h - 2 );
}
return res;
}
public static void main(String [] args)
{
int H = 3 ;
System.out.println(uncolouredCubes(H));
}
}
|
Python3
def uncolouredCubes(H):
res = 0
for h in range ( 2 , H + 1 ):
res = res + (h - 2 ) * (h - 2 )
return res
H = 3
print (uncolouredCubes(H))
|
C#
using System;
class GFG{
static int uncolouredCubes( int H)
{
int res = 0;
for ( int h = 2; h <= H; h++) {
res += (h - 2) * (h - 2);
}
return res;
}
public static void Main()
{
int H = 3;
Console.WriteLine(uncolouredCubes(H));
}
}
|
Javascript
<script>
function uncolouredCubes(H){
var res = 0
for ( var h = 2 ; h < H + 1; h++)
res = res + (h - 2) * (h - 2)
return res
}
var H = 3
document.write(uncolouredCubes(H))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: As the number of uncoloured cubes in each layer at height h is
, so this problem can be reduced to find the sum of the squares of the first (H-2) natural numbers. So, the answer is (n * (n + 1) * (2n + 1)) / 6 where n=H-2.
C++
#include <bits/stdc++.h>
using namespace std;
int uncolouredCubes( int H)
{
if (H < 2) {
return 0;
}
int n = H - 2;
return (n * (n + 1) * (2 * n + 1)) / 6;
}
int main()
{
int H = 3;
cout << uncolouredCubes(H);
}
|
Java
import java.util.*;
public class GFG
{
static int uncolouredCubes( int H)
{
if (H < 2 ) {
return 0 ;
}
int n = H - 2 ;
return (n * (n + 1 ) * ( 2 * n + 1 )) / 6 ;
}
public static void main(String args[])
{
int H = 3 ;
System.out.println(uncolouredCubes(H));
}
}
|
Python3
def uncolouredCubes(H):
if (H < 2 ):
return 0 ;
n = H - 2 ;
return (n * (n + 1 ) * ( 2 * n + 1 )) / 6 ;
H = 3 ;
print (( int )(uncolouredCubes(H)));
|
C#
using System;
class GFG
{
static int uncolouredCubes( int H)
{
if (H < 2) {
return 0;
}
int n = H - 2;
return (n * (n + 1) * (2 * n + 1)) / 6;
}
public static void Main()
{
int H = 3;
Console.Write(uncolouredCubes(H));
}
}
|
Javascript
function uncolouredCubes(H)
{
if (H < 2) {
return 0;
}
let n = H - 2;
return (n * (n + 1) * (2 * n + 1)) / 6;
}
let H = 3;
document.write(uncolouredCubes(H));
|
Time Complexity: O(1)
Auxiliary Space: O(1)