Open In App

Number of cards needed build a House of Cards of a given level N

Last Updated : 21 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the number of cards needed to make a House of Cards of N levels.
 


Examples: 
 

Input: N = 3 
Output: 15 
From the above image, it is clear that for the House of Cards for 3 levels 15 cards are needed
Input: N = 2 
Output:
 


 


Approach: 
 

  1. If we observe carefully, then a series will be formed as shown below in which i-th item denotes the number of triangular cards needed to make a pyramid of i levels: 
     

2, 7, 15, 26, 40, 57, 77, 100, 126, 155………and so on.


  1.  
  2. The above series is a method of difference series where differences are in AP as 5, 8, 11, 14……. and so on. 
     
  3. Therefore nth term of the series will be: 
     
nth term = 2 + {5 + 8 + 11 +14 +.....(n-1) terms}
         = 2 + (n-1)*(2*5+(n-1-1)*3)/2
         = 2 + (n-1)*(10+(n-2)*3)/2
         = 2 + (n-1)*(10+3n-6)/2
         = 2 + (n-1)*(3n+4)/2
         = n*(3*n+1)/2;

  1.  
  2. Therefore the number of cards needed for building a House of Cards of N levels will be: 
     
n*(3*n+1)/2

  1.  


Below is the implementation of the above approach:
 

CPP

// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of cards needed
int noOfCards(int n)
{
    return n * (3 * n + 1) / 2;
}
 
// Driver Code
int main()
{
    int n = 3;
    cout << noOfCards(n) << ", ";
    return 0;
}

                    

Java

// Java implementation of the above approach
import java.lang.*;
 
class GFG
{
    // Function to find number of cards needed
    public static int noOfCards(int n)
    {
        return n * (3 * n + 1) / 2;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 3;
        System.out.print(noOfCards(n));
    }
}
 
// This code is contributed by shubhamsingh10

                    

Python3

# Python3 implementation of the above approach
 
# Function to find number of cards needed
def noOfCards(n):
    return n * (3 * n + 1) // 2
 
# Driver Code
n = 3
print(noOfCards(n))
 
# This code is contributed by mohit kumar 29

                    

C#

// C# implementation of the above approach
using System;
 
class GFG
{
    // Function to find number of cards needed
    public static int noOfCards(int n)
    {
        return n * (3 * n + 1) / 2;
    }
      
    // Driver Code
    public static void Main(String []args)
    {
        int n = 3;
        Console.Write(noOfCards(n));
    }
}
 
// This code is contributed by 29AjayKumar

                    

Javascript

<script>
 
// Javascript implementation of the above approach
 
// Function to find number of cards needed
function noOfCards(n)
{
    return parseInt(n * (3 * n + 1) / 2);
}
 
// Driver Code
var n = 3;
document.write(noOfCards(n));
 
</script>

                    

Output: 
15

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 



Similar Reads

Count triangles required to form a House of Cards of height N
Given a number N, the task is to count the number of triangles required to form a House of Cards of N levels. Examples: Input: N = 3 Output: 13 Explanation: From the above image, the following observations can be made:Count of triangles of unit 1 = 9 (6 non-inverted triangles and 3 inverted triangles)Count of triangles of unit 2 = 3Count of triangl
3 min read
Minimum number of towers required such that every house is in the range of at least one tower
Given a map of the city and the network range, the task is to determine the minimum number of the tower so that every house is within range of at least one tower. Each tower must be installed on top of an existing house. Examples: Input: range : 1 house : 1 2 3 4 5 Output: 2 Input: range : 2 house : 7 2 4 6 5 9 12 11 Output: 3 All cities can be cov
7 min read
Modify a Binary Tree by adding a level of nodes with given value at a specified level
Given a Binary Tree consisting of N nodes and two integers K and L, the task is to add one row of nodes of value K at the Lth level, such that the orientation of the original tree remains unchanged. Examples: Input: K = 1, L = 2 Output:11 12 34 5 6 Explanation:Below is the tree after inserting node with value 1 in the K(= 2) th level. Input: K = 1,
32 min read
Difference between odd level and even level leaf sum in given Binary Tree
Given a Binary Tree, the task is to find the difference of the sum of leaf nodes at the odd level and even level of the given tree. Examples: Input: Output: -12Explanation: Following are the operations performed to get the result.odd_level_sum = 0, even_level_sum = 0Level 1: No leaf node, so odd_level_sum = 0Level 2: No leaf node, so even_level_sum
13 min read
Shortest path for a thief to reach the Nth house avoiding policemen
Given an unweighted graph and a boolean array A[ ], where if the ith index of array A[ ] denotes if that node can be visited (0) or not (1). The task is to find the shortest path to reach (N - 1)th node from the 0th node. If it is not possible to reach, print -1. Examples : Input : N = 5, A[] = {0, 1, 0, 0, 0}, Edges = {{0, 1}, {0, 2}, {1, 4}, {2,
11 min read
Minimum distance to collect coins and place them in House
Given an m*n matrix that contains character 'B' to represent the boy's initial position, 'H' represents the house's position, 'C' represents the position of coins, and '.' represents empty cells, the task is to find the minimum distance (i.e. minimum moves) for the boy to collect all the coins and place them inside the house one by one. The boy can
8 min read
Rabbit House | Google Kickstart 2021 Round A
Barbara got really good grades in school last year, so her parents decided to gift her with a pet rabbit. She was so excited that she built a house for the rabbit, which can be seen as a 2D grid with RR rows and CC columns. Rabbits love to jump, so Barbara stacked several boxes on several cells of the grid. Each box is a cube with equal dimensions,
20 min read
Find maximum possible stolen value from houses (House Robber)
There are N houses built in a line, each of which contains some value in it. A thief is going to steal the maximum value of these houses, but he can’t steal in two adjacent houses because the owner of the stolen houses will tell his two neighbors left and right sides. The task is to find what is the maximum stolen value. Examples: Input: hval[] = {
27 min read
Build Binary Tree from BST such that it's level order traversal prints sorted data
Construct a Binary Tree from the given Binary Search Tree such that it's level order traversal outputs sorted data.Examples: Input: Output: 1 2 3 Input: Output: 1 2 3 4 5 Approach: Perform the inorder traversal of the given Binary Search Tree.Add each node in level order to the Binary Tree.Finally, print the level order traversal of the created Bin
12 min read
Minimum value to be added at each level in Binary Tree to make all level sum equal
Given a binary tree, the task is to find all minimum values greater than or equal to zero, that should be added at each level to make the sum at each level equal. Examples: Input: 1 / \ 2 3 / \ 4 5 Output: 8 4 0Explanation: Initial sum at each level is {1, 5, 9}. To make all levels sum equal minimum values to be added are {8, 4, 0}. So the final su
9 min read
Article Tags :
Practice Tags :