Given an integer N, the task is to print the modified binary tree pattern.
In Modified Binary Triangle Pattern, the first and last element of a Nth row are 1 and the middle (N – 2) elements are 0.
Examples:
Input: N = 6
Output:
1
11
101
1001
10001
100001
Input: N = 3
Output:
1
11
101
Approach: From the above examples, it can be observed that, for each row N:
- 1st and Nth element is a 1
- and elements 2 to (N-1) are 0
Therefore an algorithm can be developed to print such pattern as:
- Run a loop from 1 to N using a loop variable i, which denotes the row number of the triangle pattern.
- For each row i, run a loop from 1 to i, using a loop variable j, which denotes the number in each row.
- In each iteration of j, check if j is 1 or i. If either of it true, print 1.
- If none of the case is true for j, then print 0
- Increment the value of j by 1 after each iteration
- When the j loop has completed successfully, we have printed a row of the pattern. Therefore change the output to the next line by printing a next line.
- Increment the value of i and repeat the whole process till N rows has been printed successfully.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void modifiedBinaryPattern( int n)
{
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++) {
if (j == 1 || j == i)
cout << 1;
else
cout << 0;
}
cout << endl;
}
}
int main()
{
int n = 7;
modifiedBinaryPattern(n);
}
|
Java
import java.io.*;
class GFG{
static void modifiedBinaryPattern( int n)
{
for ( int i = 1 ; i <= n; i++)
{
for ( int j = 1 ; j <= i; j++)
{
if (j == 1 || j == i)
System.out.print( 1 );
else
System.out.print( 0 );
}
System.out.println();
}
}
public static void main (String[] args)
{
int n = 7 ;
modifiedBinaryPattern(n);
}
}
|
Python 3
def modifiedBinaryPattern(n):
for i in range ( 1 , n + 1 , 1 ):
for j in range ( 1 , i + 1 , 1 ):
if (j = = 1 or j = = i):
print ( 1 , end = "")
else :
print ( 0 , end = "")
print ( '\n' , end = "")
if __name__ = = '__main__' :
n = 7
modifiedBinaryPattern(n)
|
C#
using System;
class GFG{
static void modifiedBinaryPattern( int n)
{
for ( int i = 1; i <= n; i++)
{
for ( int j = 1; j <= i; j++)
{
if (j == 1 || j == i)
Console.Write(1);
else
Console.Write(0);
}
Console.WriteLine();
}
}
public static void Main()
{
int n = 7;
modifiedBinaryPattern(n);
}
}
|
Javascript
<script>
function modifiedBinaryPattern(n)
{
for (let i = 1; i <= n; i++)
{
for (let j = 1; j <= i; j++)
{
if (j == 1 || j == i)
document.write(1);
else
document.write(0);
}
document.write( "<br/>" );
}
}
let n = 7;
modifiedBinaryPattern(n);
</script>
|
Output:
1
11
101
1001
10001
100001
1000001
Time Complexity: O(n2)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Nov, 2021
Like Article
Save Article