Average sequence : HackerEarth Problem Solution

Today Oz is playing with two lists A and B. List A contains N elements. List B is formed by replacing every element of List A by average value of all elements before current element including current one. 


For example :
If list A is like : a,b,c then
list B will be like : a1,a+b2,a+b+c3
Now you are given list B and you have to find list A. So help Oz with this task.

Input :
First line contain an integer N - size of list B
Second line contains N space separated integers - elements of list B

Output :

Output N space separated integers - elements of list A

Test data is such that list A elements are always integers.

Constraints :

1N100
1valueofeachelement10 9 

Implementation:

#include <stdio.h>
int main()
{
 int *a,*b;
 int n;
 int i,average,current = 1, j=0,sum=0,res;
 scanf("%d",&n);
 a=(int*)malloc(sizeof(int)*n);
 b=(int*)malloc(sizeof(int)*n);
 for(i=0;i<n;i++)
 {
  scanf("%d",(b+i));
  res=(*(b+i)*current)-sum;
  sum += res;
  current++;
  *(a+i) = res;
 }
 for(i=0;i<n;i++)
  printf("%d ",*(a+i));
 free(a);
 free(b);
 return 0;