HackerRank Subarray Division 1 problem solution YASH PAL, 31 July 20249 August 2024 In this HackerRank Subarray Division 1 problem, you need to determine how many ways she can divide the chocolate. Hackerrank subarray division problem solution Problem solution in Python programming. #!/bin/python3 import sys n = int(input().strip()) squares = list(map(int, input().strip().split(' '))) d,m = input().strip().split(' ') d,m = [int(d),int(m)] # your code goes here count = 0 for i in range(m, n+1): # print(i-m, i, squares[i-m:i], d) if sum(squares[i-m:i]) == d: count += 1 print(count) Problem solution in Java Programming. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] squares = new int[n]; for(int squares_i=0; squares_i < n; squares_i++){ squares[squares_i] = in.nextInt(); } int d = in.nextInt(); int m = in.nextInt(); // your code goes here if(m>n){ System.out.println(0); return; } int ct = 0; int sum = 0; for(int i=0; i<m-1; i++){ sum+=squares[i]; } for(int j=m-1; j<n; j++){ sum -= (j-m)<0 ? 0 : squares[j-m]; sum += squares[j]; if(sum==d) ct++; } System.out.println(ct); } } Problem solution in C++ programming. #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; } Problem solution in C programming. #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); // your code goes here int count = 0; for (int i = 0; i < n; i++) { int sum = 0; if (i + m <= n) { for (int j = 0; j < m; j++) { sum+=squares[i+j]; } if (sum == d) { count++; } } } printf("%dn", count); return 0; } Problem solution in JavaScript programming. process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n = parseInt(readLine()); squares = readLine().split(' '); squares = squares.map(Number); var d_temp = readLine().split(' '); var d = parseInt(d_temp[0]); // sums to this var m = parseInt(d_temp[1]); //how many blocks to sum together var count = 0; var sum; for (var i = 0; i < squares.length; i++){ sum = squares.slice(i, m + i).reduce((prev, curr) => prev + curr); if (sum === d){ count++; } } console.log(count); } algorithm coding problems AlgorithmsHackerRank