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

Symmetry

Easy
0/40
Average time to solve is 10m
profile
Contributed by
50 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-1)*(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-2.
    • For loop ‘col’ in range 0 to N-1.
      •   If(row is less than n/2 and (col less than equal to row or col greater equal to (n-row-1))).
        • Print ‘*’.
      •  Else If(row is greater than equal to n/2 and (col less than (n-row-1) or col greater than (row))).
        • Print ‘*’.
      • Else :
        • Print ‘ ’.
    • End the current 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).

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

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

Interview problems

c++ code

void print1(int n){

    int k=2*n+2;

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

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

            cout<<"*"<<" ";

        }

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

            cout<<" ";

 

        }

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

            cout<<"*"<<" ";

        }

        k-=4;

        cout<<endl;

    }

}

 

void print2(int n){

    int k=4;

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

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

            cout<<"*"<<" ";

        }

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

            cout<<" ";

 

        }

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

            cout<<"*"<<" ";

        }

        k+=4;

        cout<<endl;

    }

}

 

void symmetry(int n) {

    // Write your code here.

    print1( n);

    print2( n);

}

6 views
0 replies
1 upvote

Interview problems

Butterfly pattern

public class Solution {

    public static void symmetry(int n) {

        // 1st half

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

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

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

            }

            //space-->2*(n-i)

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

                System.out.print(" ");

            }

            //Stars

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

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

            }

            System.out.println();

        }

        // 2nd half

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

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

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

            }

            //space-->2*(n-i)

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

                System.out.print(" ");

            }

            //Stars

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

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

            }

            System.out.println();

        }

    }

}

13 views
0 replies
0 upvotes

Interview problems

c++ code

void symmetry(int n) {
    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;
    }
    for(int i=1;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;
    }
}
32 views
0 replies
0 upvotes

Interview problems

Symmetry Easy || 2 Loops || Pattern

def symmetry(n: int):
    for i in range(1,n+1):
        print("* "*(i),end="")
        print(" "*4*(n-i),end="")
        print("* "*(i),end="")
        print()
    for i in range(n-1,0,-1):
        print("* "*(i),end="")
        print(" "*4*(n-i),end="")
        print("* "*(i),end="")
        print()
    pass
14 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=0;j<i;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;j++){

            cout<<"* ";

        }

        cout<<endl;

    }

    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;

    }

    

 

}

7 views
0 replies
2 upvotes

Interview problems

C++ solution(beats 100%)

void symmetry(int n) {

    // Write your code here.

 

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

    {

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

        {

            cout<<"* ";

        }

 

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

        {

            cout<<"  ";

        }

 

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

        {

            cout<<"* ";

        }

 

        cout<<"\n";

    }

 

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

    {

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

        {

            cout<<"* ";

        }

 

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

        {

            cout<<" ";

        }

 

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

        {

            cout<<"* ";

        }

 

        cout<<"\n";

    }

}

19 views
0 replies
0 upvotes

Interview problems

Easy JAVA solution

        for(int row=0; row<n; row++){
            for(int star=0; star<row+1; star++){
                System.out.print("* ");
            }
            for(int space=1; space<=2*(n-1-row); space++){
                System.out.print("  ");
            }
            for(int star=0; star<row+1; star++){
                System.out.print("* ");
            }
            System.out.println();
        }
        for(int row=0; row<n-1; row++){
            for(int star=0; star<n-1-row; star++){
                System.out.print("* ");
            }
            for(int space=1; space<=2*(1+row); space++){
                System.out.print("  ");
            }
            for(int star=0; star<n-1-row; star++){
                System.out.print("* ");
            }
            System.out.println();
        }
    }

java

38 views
0 replies
0 upvotes

Interview problems

C++ Solution

void symmetry(int n) {

    // Write your code here.

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

        // Left stars

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

            cout << "* ";

        }

        // Spaces

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

            cout << " ";

        }

        // Right stars

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

            cout << "* ";

        }

        cout << endl;

    }

 

    // Lower half of the pattern

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

        // Left stars

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

            cout << "* ";

        }

        // Spaces

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

            cout << " ";

        }

        // Right stars

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

            cout << "* ";

        }

        cout << endl;

    }

}

28 views
0 replies
1 upvote

Interview problems

Butterfly Pattern solved using functions in cpp

void lowerPattern(int &n,int &i){

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

            cout<<"* ";

        }

}

void upperPattern(int &n,int &i){

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

            cout<<"* ";

        }

}




void symmetry(int n) {

    // Write your code here.

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

        upperPattern(n,i);

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

            cout<<" ";

        }

        upperPattern(n,i);

        cout<<endl;

    }

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

        lowerPattern(n,i);

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

            cout<<" ";

        }

        lowerPattern(n,i);

        cout<<endl;

    }

}



beginners

programming

csharp

11 views
0 replies
0 upvotes

Interview problems

Symmetry

void symmetry(int n) {

    int space = 2*n-2;

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

        int star = i;

        if(i>n) star = 2*n-i;

 

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

            cout<<"*"<<" ";

        }

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

            cout<<" ";

        }

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

            cout<<"*"<<" ";

        }

        cout<<endl;

        if(i<n) space = space - 2;

        else space = space + 2;

    }

}

40 views
0 replies
0 upvotes
Full screen
Console