
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding n subarrays with equal sum in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument.
The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum.
For example −
If the inputs are −
const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4;
The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal to 5.
Example
Following is the code −
const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4; const canFormSubarray = (arr = [], num) => { const total = arr.reduce((sum, num) => sum + num, 0); if (total % num !== 0) { return false; } const target = total / num; const visited = new Array(arr.length).fill(false); const canPartition = (start, numberOfSubsets, currentSum) => { if (numberOfSubsets === 1) { return true; } if (currentSum === target) { return canPartition(0, numberOfSubsets - 1, 0); }; for (let i = start; i < arr.length; i++) { if (!visited[i]) { visited[i] = true; if (canPartition(i + 1, numberOfSubsets, currentSum + arr[i])) { return true; } visited[i] = false; }; }; return false; }; return canPartition(0, num, 0); }; console.log(canFormSubarray(arr, num));
Output
Following is the console output −
true
- Related Articles
- Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)
- Binary subarrays with desired sum in JavaScript
- JavaScript Total subarrays with Sum K
- Subarrays product sum in JavaScript
- Largest sum of subarrays in JavaScript
- Check if it possible to partition in k subarrays with equal sum in Python
- Binary Subarrays With Sum in C++
- JavaScript: Finding nearest prime number greater or equal to sum of digits - JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Count subarrays with Prime sum in C++
- Sum of All Possible Odd Length Subarrays in JavaScript
- Print all subarrays with 0 sum in C++
- Finding sum of multiples in JavaScript
- Finding the group with largest elements with same digit sum in JavaScript
- Finding three elements with required sum in an array in JavaScript

Advertisements