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

N-Forest

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

Problem statement

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

For every value of ‘N’, help sam to print the corresponding N-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 3x3 matrix with ‘*’.
Sample Input 2 :
1
Sample Output 2 :
*
Sample Input 3 :
4
Sample Output 3 :
* * * *
* * * *
* * * *
* * * *
Hint

Think about how to use nested loops to iterate through each row and column of the ‘N x N’ -dimensional matrix.

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 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 N-1.
      •   Print ‘*’.
    • Print the next line
Time Complexity

O(NxN)

There are two nested loops and both are running exactly N times, so time complexity would be the order of NxN.

Hence the time complexity is O(NxN). 

Space Complexity

O(1). 

Since we are using constant extra space.

Hence the space complexity is O(1).

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

Interview problems

cpp solution

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

Interview problems

c++ code

void nForest(int n) {
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            cout<<"* ";
        }
        cout<<endl;
    }
}
68 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<n;j++){

            cout<< "* ";

        }

        cout<< endl;

    }

}

 

54 views
0 replies
0 upvotes

Interview problems

Solution in java

public class Solution {

    public static void nForest(int n) {

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

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

                System.out.print("* ");

            }

            System.out.println("\n");

        }

    }

}

113 views
0 replies
0 upvotes

Interview problems

solution in cpp

void nForest(int n) {

    // Write your code here.

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

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

            cout<<"*"<<" ";

        }

        cout<<endl;

    }

}

40 views
0 replies
0 upvotes

Interview problems

N-Forest

def nForest(n:int) ->None:

    # Write your solution here.

    for i in range(n):

        for j in range(n):

            print("*",end=" ")

        print()

    pass

66 views
0 replies
0 upvotes

Interview problems

add no

how to add two no

3 views
0 replies
0 upvotes

Interview problems

N-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<=n; j++){
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

java

beginners

programming

Coding Ninjas

+1 more
114 views
0 replies
0 upvotes

Interview problems

N FOREST

void nForest(int n) {

    // Write your code here.

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

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

            cout<<"*";

        }

        cout<<endl;

    }

 

};

 

75 views
0 replies
0 upvotes

Interview problems

n- forest simple cpp

void nForest(int n) {

    // Write your code here.

 

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

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

            cout<< "* ";

        }

        cout<<"\n";

    }

}

 

79 views
0 replies
0 upvotes
Full screen
Console