Thursday, May 2, 2019

Pattern

  • Problem Description

    Print the following pattern :

    If N = 1

    1

    If N = 2

    2 2 2
    2 1 2
    2 2 2

    If N = 3

    3 3 3 3 3
    3 2 2 2 3
    3 2 1 2 3
    3 2 2 2 3
    3 3 3 3 3

    and so on.
  • CODING ARENA
  • #include <stdio.h>

    int main(){
        int i, j, n;
        int number_to_print;


        scanf("%d", &n);
        number_to_print = n;

        for(i = 0; i < n; i++){
            //--next_number_to_repeat;
            number_to_print = n;

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

                printf("%d ", number_to_print);
                if( j < i   )   
                    --number_to_print;
                else if( j >  ( (n*2)- i -3  ) ) 
                    ++number_to_print;

            }   
            printf("\n");
        }   
    #if 1
        for(i = n-2; i >= 0; i--){
            number_to_print = n;

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

                printf("%d ", number_to_print);
                if( j < i   )   
                    --number_to_print;
                else if( j >  ( (n*2)- i -3  ) ) 
                    ++number_to_print;

            }   
            printf("\n");
        }   
            
    #endif
        return 0;

    }
  • Test Case 1

    Input (stdin)
    3

    Expected Output
    3 3 3 3 3 

    3 2 2 2 3

    3 2 1 2 3

    3 2 2 2 3

    3 3 3 3 3
  • Test Case 2

    Input (stdin)
    2

    Expected Output
    2 2 2 

    2 1 2

    2 2 2

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...