Sub-array Division HackerRank Solution in C, C++, Java, Python

Given a chocolate bar, two children, Lily and Ron, are determining how to share it. 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.

You must determine how many ways she can divide the chocolate.

Consider the chocolate bar as an array of squares,s=[2,2,1,3,2] . She wants to find segments summing to Ron’s birth day,d=4  with a length equalling his birth month,m=2 . In this case, there are two segments meeting her criteria:[2,2]  and [1,3] .

Function Description

Complete the birthday function in the editor below. It should return an integer denoting the number of ways Lily can divide the chocolate bar.

birthday has the following parameter(s):

  • s: an array of integers, the numbers on each of the squares of chocolate
  • d: an integer, Ron’s birth day
  • m: an integer, Ron’s birth month

Input Format

The first line contains an integer n, the number of squares in the chocolate bar.

The second line contains n space-separated integers s[i], the numbers on the chocolate squares where 0<=i<n.

The third line contains two space-separated integers,d  and m, Ron’s birth day and his birth month.

Constraints

  • 1<=n<=100
  • 1<=s[i]<=5, where (0<=i<n)
  • 1<=d<=31
  • 1<=m<=12

Output Format

Print an integer denoting the total number of ways that Lily can portion her chocolate bar to share with Ron.

Sample Input 0

5

1 2 1 3 2

3 2

 

Sample Output 0

2

 

Sub-array Division HackerRank 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 main(){
int n;
scanf("%d",&n);
int *squares = malloc(sizeof(int) * n);
for(int squares_i = 0; squares_i < n; squares_i++){
scanf("%d",&squares[squares_i]);
}
int d;
int m;
scanf("%d %d",&d,&m);
int count = 0;
for (int i = 0; i <= n-m; ++i){
int sum = 0;
for (int j = i; j < i+m; ++j){
sum += squares[j];
}
if (sum == d) ++count;
}
printf("%d", count);
return 0;
}

 

Sub-array Division HackerRank Solution in C++

#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
using namespace std;
int n,d,m,a[105],s[105],ans;
int main(){

cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
s[i]=s[i-1]+a[i];
}
cin>>d>>m;

for(int i=m;i<=n;i++)
if(s[i]-s[i-m]==d)
ans++;
cout<<ans<<endl;

return 0;
}

 

Sub-array Division HackerRank Solution in Java

import java.io.*;
import java.util.*;
public final class womens_a
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static FastScanner sc=new FastScanner(br);
static PrintWriter out=new PrintWriter(System.out);
static Random rnd=new Random();

public static void main(String args[]) throws Exception
{
int n=sc.nextInt();int[] a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int d=sc.nextInt(),m=sc.nextInt(),res=0;
for(int i=0;i+m-1<n;i++)
{
int sum=0;
for(int j=i;j<i+m;j++)
{
sum+=a[j];
}
if(sum==d)
{
res++;
}
}
out.println(res);out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;

public FastScanner(BufferedReader in) {
this.in = in;
}

public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}

public String next() throws Exception {
return nextToken().toString();
}

public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}

 

Sub-array Division HackerRank Solution in Python

#!/bin/python

import sys


n = int(raw_input().strip())
squares = map(int, raw_input().strip().split(' '))
d,m = raw_input().strip().split(' ')
d,m = [int(d),int(m)]
# your code goes here

num_ways = 0
for i in range(len(squares)-m+1):
if sum(squares[i:i+m]) == d:
num_ways += 1


print num_ways

 

Sub-array Division HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
var t = Console.ReadLine().Trim().Split();
int[] squares = Array.ConvertAll(t,int.Parse);
string[] tokens_d = Console.ReadLine().Split();
int d = Convert.ToInt32(tokens_d[0]);
int m = Convert.ToInt32(tokens_d[1]);

int sum = 0;
int count= 0;
for (int i=0; i<n; i++)
{
sum += squares[i];
if (i>=m) sum -= squares[i-m];
if (sum==d && i>=m-1)
count++;
}

Console.WriteLine(count);
}
}

 

Attempt – Sub-array Division HackerRank Challenge

Link – https://www.hackerrank.com/challenges/the-birthday-bar/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/divisible-sum-pairs-hackerrank-solution/

 

Leave a Comment