Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Binary Number Triangle

Easy
0/40
profile
Contributed by
140 upvotes

Problem statement

Aryan and his friends are very fond of the pattern. For a given integer ‘N’, they want to make the N-Binary Number Triangle.

You are required to print the pattern as shown in the examples below.

Example:
Input: ‘N’ = 3

Output: 

1
0 1
1 0 1
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer ‘N’.
Output format:
Print the pattern as specified in the problem statement.
Constraints :
1  <= N <= 20
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
1
0 1
1 0 1
Sample Input 2 :
4
Sample Output 2 :
1
0 1
1 0 1
0 1 0 1
Sample Input 3 :
6
Sample Output 3 :
1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 
Hint

Iterate to all the cells and fill the cells in an alternate manner.

Approaches (1)
Brute Force

Approach: 

The solution is iterating on the pattern and for the cell in the ‘i’th row and the ‘j’ column, the value will be 1 if ‘i’ + ‘j’ is even else the value will be 0.

The steps are as follows :

Function void nBinaryTriangle(int ‘N’)

 

  1. Int ‘num’ = 1
  2. For ‘i’ from 0 to ‘N’-1:
    • For j from 0 to ‘num’-1:
      • If ‘i’+’j’ % 2 == 0:
        • Print ‘1’
      • Else  :
        • Print ‘0’
    • ‘Num’++
Time Complexity

O(N^2), Where N is the given input integer. 

 

Two nested loops are running N * N times so that time complexity would be the order of N * N.

Hence the time complexity is O(N^2). 

Space Complexity

O(1)

Since we are using constant extra space.

 

Code Solution
(100% EXP penalty)
Binary Number Triangle
All tags
Sort by
Search icon

Interview problems

Java easy solution

public class Solution {

    public static void nBinaryTriangle(int n) {

        // Write your code here.

        for(int i =1;i<=n;i++){

            int start = 0;

            if(i%2!=0){

                start =1;

            }

            for(int j =0;j<i;j++){

                System.out.print(start+" ");

                start =1-start;

            }

            System.out.println();

        }

    }

}

9 views
0 replies
0 upvotes

Interview problems

C++ Solution

void nBinaryTriangle(int n) {

    for(int i=0;i<n;i++){

        for(int j=0; j<=i; j++){

            if((i+j)%2==0){

                cout<<"1 ";

            }

            else{

                cout<<"0 ";

            }

        }

        cout<<endl;

    }

}

41 views
0 replies
1 upvote

Interview problems

cpp easyyy

void nBinaryTriangle(int n) {

    // Write your code here.

     int start = 1;

    for(int i=1; i<=n; i++){

       

        if(i%2 == 0){

            start = 0;

        }else{

            start = 1;

        }

        for(int j=1; j<=i; j++){

            cout<<start<<" ";

            start = 1-start;

 

        }

        cout<<endl;

    }

 

}

40 views
0 replies
0 upvotes

Interview problems

C++ Simple Bruteforce Solution

void nBinaryTriangle(int n) {

    // Write your code here.

    for(int i=0;i<n;i++){

        for(int j=0;j<=i;j++){

            if((i%2) == 0){

                if((j%2) != 0){

                    cout<<"0 ";

                }

                else{

                    cout<<"1 ";

                }

            }

            else{

                

                if((j%2) != 0){

                    cout<<"1 ";

                }

                else{

                    cout<<"0 ";

                }

            }

        }

        cout<<endl;

    }

}

13 views
0 replies
0 upvotes

Interview problems

c++ code

void nBinaryTriangle(int n) {

   for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            if((i+j)%2==0)
            {
                cout<<1<<" ";
            }
            else{
                cout<<0<<" ";
            }
        }
        cout<<endl;
    }
}
17 views
0 replies
0 upvotes

Interview problems

c++ code

void nBinaryTriangle(int n) {
    int start=1;
    for(int i=0;i<n;i++){
        if(i%2==0){
            start=1;
        }
        else{
            start=0;
        }
        for(int j=0;j<=i;j++){
            cout<<start<<" ";
            start=1-start;
        }
        cout<<endl;
    }
}
10 views
0 replies
0 upvotes

Interview problems

Using bit manipulation for flipping number

def nBinaryTriangle(n: int) -> None:
    for i in range(1,n+1):
        digit = i % 2
        for _ in range(i):
            print(digit, end=" ")
            digit ^= 1
        print()
13 views
0 replies
0 upvotes

Interview problems

simple python solution

def nBinaryTriangle(n: int) -> None:
    # Write your solution here.
    for i in range(1, n+1):
        x = 1
        if i%2 == 0:
            x = 0
        for j in range(i):
            print(x, end=' ')
            x=0 if x==1 else 1
        print()

beginners

programming

python

36 views
0 replies
0 upvotes

Interview problems

Beginner Friendly Solution || C++

void nBinaryTriangle(int n) {

    // Write your code here.

    for(int i = 0;i<n;i++){

        for(int j = 0;j<=i;j++){

            if((i+j)%2 == 0){

                cout<<"1 ";

 

            }

            else{ 

                cout<<"0 ";

            }

        }

        cout<<endl;

    }

}

34 views
0 replies
0 upvotes

Interview problems

Binary Triangle Java Solution

public class Solution {

    public static void nBinaryTriangle(int n) {

        // Write your code here.

 

         int start =0;

 

        for (int i=0;i<n;i++){

        if(i%2==0) 

        start=1;

        else start=0;

        

        for (int j=0;j<=i;j++){

 

        System.out.print(start +" ");

        start=1-start;

        

        }

    

        System.out.println();

 

        }

    

    }

        

    }

 

48 views
0 replies
0 upvotes
Full screen
Console