Created
May 4, 2014 03:59
-
-
Save jitsceait/3439f0a91f0028a5b1fa to your computer and use it in GitHub Desktop.
This post provides implementation for zero sum sub array problem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<stdlib.h> | |
| #include <math.h> | |
| void zero_subarray(int a[], int n){ | |
| int i, j; | |
| int T[n]; | |
| T[0] = a[0]; | |
| for(i= 1; i<n; i++){ | |
| T[i] = T[i-1] +a[i]; | |
| if(T[i] == 0){ | |
| printf("\n Sub array from %d to %d adds upto zero" , 0,i); | |
| } | |
| for(j=0; j<i; j++){ | |
| if(T[i] == T[j]){ | |
| printf("\n Sub array from %d to %d adds upto zero" , j+1,i); | |
| } | |
| } | |
| } | |
| } | |
| int main(){ | |
| int a[] = {4, 6, 3, -9, -5, 1, 3, 0, 2}; | |
| int n = sizeof(a)/sizeof(a[0]); | |
| zero_subarray(a,n); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment