Best Way to Build House of Cards With Least Amount

Number of cards needed build a House of Cards of a given level Due north

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

Examples:

Input: Due north = iii
Output: 15
From the above prototype, it is clear that for the Business firm of Cards for 3 levels xv cards are needed
Input: N = two
Output: 7

Approach:

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

two, seven, 15, 26, 40, 57, 77, 100, 126, 155………and then on.

  1. The above serial is a method of deviation serial where differences are in AP as 5, eight, 11, fourteen……. and and so on.
  2. Therefore nth term of the series volition be:
nth term = 2 + {5 + 8 + 11 +14 +.....(n-1) terms}          = two + (north-1)*(2*5+(n-1-1)*3)/2          = two + (due north-ane)*(10+(n-2)*3)/ii          = ii + (due north-i)*(10+3n-6)/ii          = ii + (northward-1)*(3n+4)/2          = due north*(3*northward+1)/2;
  1. Therefore the number of cards needed for building a House of Cards of Northward levels will be:
            n*(3*n+1)/2          

Below is the implementation of the above approach:

CPP

#include <bits/stdc++.h>

using namespace std;

int noOfCards( int due north)

{

render north * (3 * n + 1) / 2;

}

int main()

{

int due north = 3;

cout << noOfCards(n) << ", " ;

return 0;

}

Coffee

import java.lang.*;

class GFG

{

public static int noOfCards( int due north)

{

return n * ( three * n + 1 ) / ii ;

}

public static void main(Cord args[])

{

int n = 3 ;

Organization.out.impress(noOfCards(n));

}

}

Python3

def noOfCards(north):

return n * ( 3 * northward + 1 ) / / 2

n = iii

print (noOfCards(north))

C#

using System;

class GFG

{

public static int noOfCards( int n)

{

render n * (iii * n + ane) / 2;

}

public static void Main(String []args)

{

int n = 3;

Console.Write(noOfCards(n));

}

}

Javascript

<script>

function noOfCards(northward)

{

return parseInt(n * (3 * n + ane) / two);

}

var n = three;

document.write(noOfCards(north));

</script>

Fourth dimension Complexity: O(1)

Auxiliary Space: O(1)


freesmor1995.blogspot.com

Source: https://www.geeksforgeeks.org/number-of-cards-needed-build-a-house-of-cards-of-a-given-level-n/

0 Response to "Best Way to Build House of Cards With Least Amount"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel