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

N/2-Forest

Easy
0/40
Average time to solve is 5m
profile
Contributed by
72 upvotes

Problem statement

Sam is making a forest visualizer. An N-dimensional forest is represented by the pattern of size NxN filled with ‘*’.

An N/2-dimensional forest is represented by the lower triangle of the pattern filled with ‘*’.

For every value of ‘N’, help sam to print the corresponding N/2-dimensional forest.

Example:
Input:  ‘N’ = 3

Output: 
* 
* *
* * *
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line contains an integer, ‘N’.
Output format:
Print the pattern as specified.
Constraints :
1  <= N <= 25
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
* 
* *
* * *
Explanation Of Sample Input 1 :
For N = 3, fill all the rows and columns in the lower triangle of 3x3 matrix with ‘*’.
Sample Input 2 :
1
Sample Output 2 :
* 
Hint

Iterate to all the cells.

Approaches (1)
Brute Force

Approach: 

The solution to the problem lies in just iterating to all the NxN cells of the pattern and print every cell in the lower triangle with ‘*’.

The steps are as follows :

Function void nForest(int ‘N’)

  1. For loop ‘row’ in range 0 to N-1.
    • For loop ‘col’ in range 0 to ‘row’.
      •   print ‘*’.
    • Print the next line.
Time Complexity

O(N*N) 

There are two nested loops, so time complexity would be the order of N*N.

Hence the time complexity is O(N*N). 

Space Complexity

O(1)

Since we are using constant extra space. 

Hence the space complexity is O(1).

Code Solution
(100% EXP penalty)
N/2-Forest
All tags
Sort by
Search icon

Interview problems

Java

public class Solution {

    public static void nForest(int n) {

        // Write your code here

 

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

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

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}

17 views
0 replies
0 upvotes

Interview problems

Print Pattern with cpp

void nForest(int n) {

    // Write your code here.

 

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

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

            cout<<"* ";

        }

        cout<<endl;

    }

 

}

13 views
0 replies
0 upvotes

Interview problems

cpp solution

void nForest(int n) {
	// Write your code here.
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=i;j++)
		{
			cout<<"* ";
		}
		cout<<endl;
	}

}

 

27 views
0 replies
0 upvotes

Interview problems

c++ code

void nForest(int n) {
	for(int i=1;i<=n;i++){
		for(int j=1;j<i+1;j++){
			cout<<"* ";
		}
		cout<<endl;
	}
}
18 views
0 replies
0 upvotes

Interview problems

Pattern

void nForest(int n) {

    // Write your code here.'

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

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

             cout<<"* ";

         }

         cout<<endl;

     }

}

 

12 views
0 replies
0 upvotes

Interview problems

solution in cpp

void nForest(int n) {

    // Write your code here.

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

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

            cout<<"*"<<" ";

        }

        cout<<endl;

    }

}

15 views
0 replies
0 upvotes

Interview problems

N/2-Forest

def nForest(n:int) ->None:

    # Write your solution here.

    for i in range(n):

        for j in range(i+1):

            print("*",end=" ")

        print()

    pass

29 views
0 replies
0 upvotes

Interview problems

N/2-Forest Solution in Java

public class Solution {
    public static void nForest(int n) {
        // Write your code here
        for(int i=1; i<=n; i++){
            for(int j=1; j<=i; j++){
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

java

beginners

programming

Coding Ninjas

+1 more
46 views
0 replies
0 upvotes

Interview problems

n/2 cpp solution

void nForest(int n) {

    // Write your code here.

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

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

            cout<<"* ";

        }

        cout<<endl;

    }

}

 

40 views
0 replies
0 upvotes

Interview problems

try to find the best relation between i and j

void nForest(int n) {

  // Write your code here.

    

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

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

            if(i>=j){

                cout<<"* ";

            }

            else {

                cout<<" ";

            }

        }

        cout<<endl;

    }

 

    }

24 views
0 replies
0 upvotes
Full screen
Console