Tuesday, 15 August 2017

Birthday Chocolate hacker rank solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int solve(int n, int s_size, int* s, int d, int m){
    int i,j,sum,c=0;
    for(i=0;i<=n-m;i++)
                {
                                sum=0;
                                for(j=i;j<m+i;j++)
                                {
                                                sum=sum+*(s+j);
                                }
                                if(sum == d)
                                                c++;
                }
    return c;
}

int main() {
    int n;
    scanf("%d", &n);
    int *s = malloc(sizeof(int) * n);
    for(int s_i = 0; s_i < n; s_i++){
       scanf("%d",&s[s_i]);
    }
    int d;
    int m;
    scanf("%d %d", &d, &m);
    int result = solve(n, n, s, d, m);
    printf("%d\n", result);
    return 0;
}

No comments:

Post a Comment