Saturday, September 22, 2018

Happy Insertion

  • Problem Description

    A little boy chintu received a gift from his Nephew. He found numbers written in a building blocks arranged in array.His friend Tom wish to insert an element in a specified position in the building block. Your task is to write a code to insert an element into the building blocks in a specified position.
    Input:
    Input should contain the maximum number of building blocks n
    And input the integers up to the limit n.Input the element to be inserted and mention the position.
    Output:
    It should print the arranged elements in the blocks after insertion of that element in the specified position.
  • CODING ARENA
  • #include <stdio.h>
    int main()
     {
       int arr[30], element, num, i, location;
     
       
       scanf("%d",&num);
     
       for (i=0;i<num;i++)
       {
          scanf("%d",&arr[i]);
       }
     
      
       scanf("%d",&element);
       scanf("%d",&location);
     
      
       for (i=num;i>=location;i--) 
       {
          arr[i] = arr[i-1];
       }
     
       num++;
       arr[location - 1] = element;
     
       
       for (i=0;i<num;i++)
          printf("%d ",arr[i]);
     return 0;
    }

  • Test Case 1

    Input (stdin)
    4
    
    1 2 3 4
    
    7
    
    2
    
    
    Expected Output
    1 7 2 3 4
  • Test Case 2

    Input (stdin)
    3
    
    4 5 7
    
    8
    
    1
    
    
    Expected Output
    8 4 5 7

No comments:

Post a Comment