Skip to content

Additive sequence of an array

venuprasad naik edited this page Aug 14, 2017 · 1 revision

Q6. Additive sequence of an array

The additive sequence of an array x is an array y of the same length as x, where each element y[i] = x[0] + x[1] + … + x[i]. (For example, [1,3,8,12,15] is the additive sequence of [1,2,5,4,3].)

Complete the following function so that it returns 1 if y is the additive sequence of x (where x and y are both integer arrays with the same length n >= 0), and returns 0 otherwise.

#include <stdio.h>

int checkAddSeq(int *x, int *y, int n) { for(int i = 0; i < n; i++) { // Write your code is this space if(i==0) { if(x[i]==y[i]) { continue; } else { return 0; } }

if(y[i]-x[i]==(y[i-1])) { continue; }

`else return 0;`

} return 1; } int main() { int n, i; int x[10], y[10];

`scanf("%d", &n);`
`for(i=0; i<n; i++)`
    `scanf("%d", &x[i]);`
`for(i=0; i<n; i++)`
    `scanf("%d", &y[i]);`

`printf("%d\n", checkAddSeq(x, y, n));`
`return 0;`

}

Clone this wiki locally