Header Ad

HackerRank Subarray Division 2 problem solution

In this HackerRank Subarray Division 2 problem-solution 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 birthday.

Determine how many ways she can divide the chocolate.

HackerRank Subarray Division 2 problem solution


Problem solution in Python.

class so:
    def __init__(self):
        self.n = int(input().strip())

        self.s = list(map(int, input().rstrip().split()))

        first_multiple_input = input().rstrip().split()

        self.d = int(first_multiple_input[0])

        self.m = int(first_multiple_input[1])

        self.calc()

    def calc(self):
        kq = 0
        for i in range(self.n + 1 - self.m):
            sum = 0
            for j in range(i, self.m + i):
                sum += self.s[j]
            if sum == self.d:
                kq += 1

        print(kq)


item = so()



Problem solution in Java.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'birthday' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY s
     *  2. INTEGER d
     *  3. INTEGER m
     */

    public static int birthday(List<Integer> s, int d, int m) {
    // Write your code here
        int sum;
        int count = 0;
        for (int i=0; i<=s.size()-m; i++) {
            sum = s.subList(i, i+m).stream().reduce((a,b)->a+b).get();
            if (sum == d) {
                count++;
            }
        }
        return count;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> s = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int d = Integer.parseInt(firstMultipleInput[0]);

        int m = Integer.parseInt(firstMultipleInput[1]);

        int result = Result.birthday(s, d, m);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}


Problem solution in C++.

#include <iostream>
using namespace std;

int main()
{
    int n;  cin >> n;
    int A[n];
    int d, m, res = 0;
    for (int i=0; i<n; i++)
        cin >> A[i];
    cin >> d >> m;
    for (int i=0, temp=0, sum=0; i<n-m+1; i++) {
        while(temp < m) {
            sum += A[i+temp];
            //cout << A[i+temp] << " ";
            temp++;
        }
        if (sum == d)
            res++;
        sum = 0;
        temp = 0;
        //cout << endl;
    }
    cout << res;
    return 0;
}


Problem 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;
}


Post a Comment

0 Comments