Solution #1: Here is the recursive code,
#define SIZE 9 int hasZero(int* A, int len, int prevSum) { if(len == 1) return((A[0]+prevSum) == 0); int sum = prevSum + A[0]; if(sum == 0) return 1; return hasZero(A+1, len-1, sum) | hasZero(A+1, len-1, 0); } int main() { int A[SIZE] = {-2, 1, 4, -3, 6, 3, 8, -4, 7}; printf("hasSum: %d\n", hasZero(A, SIZE, 0)); return 0; }
No comments:
Post a Comment