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
Problem of the day
For every value of ‘N’, print the field if the trees are represented by ‘*’.
Example:Input: ‘N’ = 3
Output:
* * *
* *
*
The first and only line contains an integer, ‘N’.
Output format:
Print the pattern as specified.
1 <= T <= 10
1 <= N <= 25
Time Limit: 1 sec
3
* * *
* *
*
1
*
4
* * * *
* * *
* *
*
Iterate to all the cells.
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’)
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).
O(1)
Since we are using constant extra space.
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
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;
}
}
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;
}
}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();
}
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
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