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

Seeding

Easy
0/40
Average time to solve is 10m
profile
Contributed by
86 upvotes

Problem statement

Sam is planting trees on the upper half region (separated by the left diagonal) of the square shared field.

For every value of ‘N’, print the field if the trees are represented by ‘*’.

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  <= T <= 10
1  <= N <= 25
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
* * *
* *
*
Sample Input 2 :
1
Sample Output 2 :
*
Sample Input 3 :
4
Sample Output 3 :
* * * *
* * *
* *
*
Hint

 Iterate to all the cells.

Approaches (1)
Brute Force

Approach: 

The solution to the problem lies in just iterating to all the N*N cells of the pattern and printing every cell.

The steps are as follows :

Function void seeding(int ‘N’)

  1. For loop ‘row’ in range 0 to N-1.
    • For loop ‘col’ in range 0 to N-1.
      •   If ‘col’ < N-’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.

Code Solution
(100% EXP penalty)
Seeding
All tags
Sort by
Search icon

Interview problems

CPP Solution

void seeding(int n) {

    // Write your code here.

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

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

            cout<<"*"<<" ";

        }

        cout<<endl;

    }

}

This code is up and running but is the logic correct

 

5 views
0 replies
0 upvotes

Interview problems

CPP Solution

void seeding(int n) 

{

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

    {

        for(int j=n-i;j>0;j--)

        {

            cout<<"* ";

        }

        cout<<endl;

    }

}

14 views
0 replies
0 upvotes

Interview problems

cpp solution

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

Interview problems

Codz with JAva

public class Solution {

    public static void seeding(int n) {

       for ( int i = n; i >= 1; i--){

           for ( int j = i; j >= 1; j--){

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

           }

           System.out.println();

           

         }

    }

}

29 views
0 replies
0 upvotes

Interview problems

c++ code

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

Interview problems

solution

void seeding(int n) {

    // Write your code here.

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

        for(int j=n;j>i;j--){

            cout<<"* ";

        }

        cout<<endl;

    }

}

7 views
0 replies
1 upvote

Interview problems

Pattern

void seeding(int n) {

    // Write your code here.

    for(int i=n;i>0;i--){

        for(int j=i;j>0;j--){

            cout<<"* ";

        }

        cout<<endl;

    }

}

14 views
0 replies
0 upvotes

Interview problems

sol in cpp

void seeding(int n) {

    // Write your code here.

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

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

            cout<<"*"<<" ";

        }

        cout<<endl;

    }

}

10 views
0 replies
0 upvotes

Interview problems

easiest cpp solution 2 approach

Approach 1

    

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

    {

        for(int j=i ; j<n ;j++)

        {

            cout<<"* ";

        }

        cout<<endl;

    }

 

 

Approach 2

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

    {

        for(int j=n ; j>i ;j--)

        {

            cout<<"* ";

        }

        cout<<endl;

    }

13 views
0 replies
0 upvotes

Interview problems

Seeding

def seeding(n: int) -> None:

    # Write your solution here.

    for i in range(n,0,-1):

        for j in range(i):

            print("*",end=" ")

        print()

    pass

28 views
0 replies
0 upvotes
Full screen
Console