Given a number N which represents the last term of the Triangle Pattern. The task is to print the Triangle Pattern from 1 to N, such that each row is complete.
Triangle Pattern is given as:
1
2 3
4 5 6
7 8 9 10
.
.
Examples:
Input: N = 3
Output:
1
2 3
Input: N = 7
Output:
1
2 3
4 5 6
7 will not be printed as
it would result in an incomplete row
Approach:
- Find the number of complete rows from the given last term N.
- As:
For Max Height = 1, the last term would be 1
For Max Height = 2, the last term would be 3
For Max Height = 3, the last term would be 6 - So the last term forms a pattern: 1, 3, 6, 10, 15,…
- Therefore, the n-th term of series 1, 3, 6, 10, 15,…
A(n) = 1 + 2 + 3 + 4… + (n – 1) + n
= n(n + 1) / 2
i.e A(n) is the sum of First n natural numbers. - So in
A(n) = n(n + 1) / 2
A(n) represents the last term (as per our problem),
and n represents the max height of the Triangle
- Hence this can be seen as:
Last term = height (height + 1) / 2
height = (-1 + sqrt(1 + 8*lastTerm)) / 2
- After finding the max height, the triangle pattern can be easily printed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void triangle( int n)
{
int k = 2 * n - 2;
int ch = 1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < k; j++)
cout << " " ;
k = k - 1;
for ( int j = 0; j <= i; j++) {
cout << ch++ << " " ;
}
cout << endl;
}
}
int maxHeight( int n)
{
return ((( int ) sqrt (1 + 8.0 * n)) - 1) / 2;
}
int main()
{
int N = 9;
triangle(maxHeight(N));
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void triangle( int n)
{
int k = 2 * n - 2 ;
int ch = 1 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < k; j++)
System.out.print( " " );
k = k - 1 ;
for ( int j = 0 ; j <= i; j++)
{
System.out.print(ch++ + " " );
}
System.out.println();
}
}
static int maxHeight( int n)
{
return ((( int )Math.sqrt( 1 + 8.0 * n)) - 1 ) / 2 ;
}
public static void main(String[] args)
{
int N = 9 ;
triangle(maxHeight(N));
}
}
|
Python3
from math import sqrt
def triangle(n) :
k = 2 * n - 2 ;
ch = 1 ;
for i in range (n) :
for j in range (k) :
print ( " " , end = "");
k = k - 1 ;
for j in range (i + 1 ) :
print (ch, end = " " );
ch + = 1 ;
print ()
def maxHeight(n) :
ans = (sqrt( 1 + 8.0 * n) - 1 ) / / 2 ;
return int (ans);
if __name__ = = "__main__" :
N = 9 ;
triangle(maxHeight(N));
|
C#
using System;
class GFG
{
static void triangle( int n)
{
int k = 2 * n - 2;
int ch = 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < k; j++)
Console.Write( " " );
k = k - 1;
for ( int j = 0; j <= i; j++)
{
Console.Write(ch++ + " " );
}
Console.WriteLine();
}
}
static int maxHeight( int n)
{
return ((( int )Math.Sqrt(1 + 8.0 * n)) - 1) / 2;
}
public static void Main(String[] args)
{
int N = 9;
triangle(maxHeight(N));
}
}
|
Javascript
<script>
function triangle(n)
{
var k = 2 * n - 2;
var ch = 1;
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < k; j++)
document.write( " " );
k = k - 1;
for ( var j = 0; j <= i; j++) {
document.write(ch++ + " " );
}
document.write( "<br>" );
}
}
function maxHeight(n)
{
return parseInt(((parseInt(Math.sqrt(1 + 8.0 * n))) - 1) / 2);
}
var N = 9;
triangle(maxHeight(N));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)