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

Symmetric Void

Easy
0/40
Average time to solve is 15m
profile
Contributed by
99 upvotes

Problem statement

Sam is curious about symmetric patterns, so he decided to create one.

For every value of ‘N’, return the symmetry as shown in the example.

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:
* * * * * * 
* *     * * 
*         * 
*         * 
* *     * * 
* * * * * * 
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 (2*N)*(2*N) cells of the pattern and printing every cell.

The steps are as follows :

Function void symmetry(int ‘N’)

  1. Update N with 2*N.
  2. For loop ‘row’ in range 0 to N-1.
    • For loop ‘col’ in range 0 to N-1.
      •  If  row less than n/2 and (col less than (n/2 - row) or col greater than equal to (n/2 + row)).
        • Assign pattern(row, col) with ‘*’.
      •  If row greater than equal to n/2 and (col less than equal to (row-n/2) or col greater than equal to (n-row+n/2-1)).
        • Assign pattern(row, col) with ‘*’.
  3. Print the pattern.
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)


No extra space is used as we are just printing the pattern.

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

Interview problems

Beginner Friendly | Symmetric Void | Python

def symmetry(n: int):

    for i in range(n):

        # print *

        for y in range(n-i):

            print("*",end=' ')

        # print gap 

        for o in range(2*i):

            print(" ",end=' ')

        # reverse *

        for j in range(n-i):

            print("*",end=' ')

        # newline

        print()

    for s in range(n):

        # print *

        for y in range(s+1):

            print("*",end=' ')

        # print gap 

        gap = 2*(n-1)

        for o in range(gap-(2*s)):

            print(" ",end=' ')

        # reverse *

        for j in range(s+1):

            print("*",end=' ')

        print() 

11 views
0 replies
0 upvotes

Interview problems

python

def symmetry(n: int):

    # Write your solution from here.

    for i in range(n):

        print("* "*(n-i),end="")

        print(" "*2*i,end="")

        print("* "*(n-i))

    for i in range(1,n+1):

        print("* "*i,end="")

        print(" "*2*(n-i),end="")

        print("* "*i)

    pass

 

python

13 views
0 replies
0 upvotes

Interview problems

easiest cpp sol which works

void symmetry(int n) {

    // Write your code here.

 

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

        //stars

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

            cout<<"*"<<" ";

        }

 

        //spaces

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

            cout<<"  ";

        }

 

        //stars

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

            cout<<"*"<<" ";

        }

 

        cout<<endl;

    }

    

 

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

        //stars

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

            cout<<"*"<<" ";

        }

 

        //spaces

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

            cout<<"  ";

        }

 

        //stars

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

            cout<<"*"<<" ";

        }

        cout<<endl;

    }

}

50 views
0 replies
0 upvotes

Interview problems

c++ code

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

Interview problems

2Loops || Python || Symmetry

2 loops = [stars,space,stars]

 

def symmetry(n: int):
    for i in range(1,n+1):
        for j in range(1,(n+1)-i+1):
            print("*",end=" ")
        print(" "*2*(2*((n+1)-(((n+1)-i)))-2),end="")
        for j in range(1,(n+1)-i+1):
            print("*",end=" ")
        print()
    for i in range(n,0,-1):
        for j in range(1,(n+1)-i+1):
            print("*",end=" ")
        print(" "*2*(2*((n+1)-(((n+1)-i)))-2),end="")
        for j in range(1,(n+1)-i+1):
            print("*",end=" ")
        print()
    pass
21 views
0 replies
0 upvotes

Interview problems

solution

void symmetry(int n) {

    // Write your code here.

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

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

            cout<<"* ";

        }

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

            cout<<" " ;

        }

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

            cout<<" " ;

        }

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

            cout<<"* ";

        }

        cout<<endl;

    }

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

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

            cout<<"* ";

        }

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

            cout<<" " ;

        }

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

            cout<<" " ;

        }

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

            cout<<"* ";

        }

        cout<<endl;

    }

}

24 views
0 replies
1 upvote

Interview problems

c++

void symmetry(int n) {

    // Write your code here.

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

    {

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

        {

            cout<<"* ";

        }

 

        for(int j=0;j<i*2;j++)

        {

            cout<<"  ";

        }

 

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

        {

            cout<<"* ";

        }

        cout<<"\n";

    }

 

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

    {

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

        {

            cout<<"* ";

        }

 

        for(int j=0;j<n-i-1*2;j++)

        {

            cout<<"  ";

        }

 

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

        {

            cout<<"* ";

        }

 

        cout<<"\n";

    }

}

19 views
0 replies
0 upvotes

Interview problems

C++

void symmetry(int n) {

    // Write your code here.

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

    {

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

        {

            cout<<"* ";

        }

 

        for(int j=0;j<i*2;j++)

        {

            cout<<"  ";

        }

 

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

        {

            cout<<"* ";

        }

        cout<<"\n";

    }

 

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

    {

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

        {

            cout<<"* ";

        }

 

        for(int j=0;j<n-i-1*2;j++)

        {

            cout<<"  ";

        }

 

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

        {

            cout<<"* ";

        }

 

        cout<<"\n";

    }

}

10 views
0 replies
0 upvotes

Interview problems

java code

public class Solution {

    public static void symmetry(int n) {

        // Write your code here

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

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

                System.out.print("* ");

            }

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

                System.out.print("   ");

            }

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

                System.out.print("* ");

            }

            System.out.println();

        }

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

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

                System.out.print("* ");

            }

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

                System.out.print("   ");

            }

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

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}

64 views
0 replies
0 upvotes

Interview problems

Easy C++ solution

void symmetry(int n) {

    // Write your code here.

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

    {

        for(int j = 1; j <= n*2; j++)

        {

            if(n - (i - 1) >= j)

                cout << "* ";

            else if((n + 1) + (i - 1) <= j)

                cout << "* ";

            else

                cout << " ";

        }

        cout << endl;

    }

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

    {

        for(int j = 1; j <= n*2; j++)

        {

            if(j <= i)

                cout << "* ";

            else if((n*2) - (i-1) <= j)

                cout << "* ";

            else

                cout << " ";

        }

        cout << endl;

    }

}

38 views
0 replies
0 upvotes
Full screen
Console