HackerRank: Subarray Division 2 C Program

Subarray Division 2 HackerRank solution in C:

Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it.

Lily decides to share a contiguous segment of the bar selected such that:

  • The length of the segment matches Ron’s birth month, and,
  • The sum of the integers on the squares is equal to his birth day.
  • Determine how many ways she can divide the chocolate.
int birthday(int s_count, int* s, int d, int m) {
  int sum = 0;
  int ans = 0;
  for (int i = 0; i < s_count - m + 1; i++) {
    sum=0;
    for (int j = i; j < i+m; j++) {
      sum += s[j];
    }
    if (sum == d) {
      ans++;
    }
  }
    return ans;
}

Leave a Comment