Saturday, August 18, 2018

MEMORY AND CROW

  • Problem Description

    There are n integers b1,b2,,bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:

    The crow sets ai initially 0.
    The crow then adds bi to ai, subtracts bi+1, adds the bi+2 number, and so on until the n'th number. Thus, ai=bi-bi+1+bi+2-bi+3.... 
    Memory gives you the values a1,a2,,an, and he now wants you to find the initial numbers b1,b2,,bn written in the row? Can you do it?

    Input

    The first line of the input contains a single integer n (2<=n<=100000) the number of integers written in the row.

    The next line contains n, the ith of which is ai (-109<=ai<=109) the value of the ith number.

    Output

    Print n integers corresponding to the sequence b1,b2,,bn. Its guaranteed that the answer is unique and fits in 32-bit integer type.
  • CODING ARENA::

  •  #include<stdio.h>
    int main()
    { int n,p,q,i;
    scanf("%d%d",&n,&p);
    for(i=1;i<n;i++)
    { scanf("%d",&q);
    printf("%d ",p+q);
    p=q;
    }
    printf("%d\n",p);
     return 0;
    }
  • Test Case 1

    Input (stdin)
    5
    
    6 -4 8 -2 3
    
    
    Expected Output
    2 4 6 1 3
  • Test Case 2

    Input (stdin)
    5
    
    3 -2 -1 5 6
    
    
    Expected Output
    1 -3 4 11 6

No comments:

Post a Comment