Created
August 3, 2016 04:57
-
-
Save ympons/411392741c5fc62f542712095ae73040 to your computer and use it in GitHub Desktop.
Find Max SubArray with given sum
This file contains 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> | |
int maxLength(int a_size, int* a, int k) { | |
int sum = a[0], i, ini=0, sol=0, s; | |
for (i=1;i<a_size;i++) { | |
sum += a[i]; | |
while(sum > k && ini < i) { | |
sum -= a[ini]; | |
ini++; | |
} | |
if (sum <= k) { | |
s = i - ini + 1; | |
if (s > sol) sol = s; | |
} | |
} | |
return sol; | |
} | |
int main(void) { | |
int n,a[501],i,k; | |
scanf("%d", &n); | |
for (i=0;i<n;i++) | |
scanf("%d", &a[i]); | |
scanf("%d", &k); | |
printf("%d", maxLength(n, a, k)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment