
- 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
Sum which is divisible by n in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, num, as the second argument. Our function should return the number of (contiguous, non-empty) subarrays that have a sum divisible by num.
For example, if the input to the function is −
const arr = [4, 5, 0, -2, -3, 1]; const num = 5;
Then the output should be −
const output = 7;
Output Explanation
There are 7 subarrays with a sum divisible by 5 −
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Example
The code for this will be −
const arr = [4, 5, 0, -2, -3, 1]; const num = 5; const divisibleSum = (arr = [], num = 1) => { const map = {}; let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; const key = ((sum % num) + num) % num; map[key] = map[key]+1||1; }; let s = 0; for (let i = 0; i < num; i++) { if (map[i] > 1) { s += (map[i] * (map[i] - 1)) / 2; } } return s + (map[0]||0); }; console.log(divisibleSum(arr, num));
Output
And the output in the console will be −
7
- Related Articles
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Sum of first N natural numbers which are divisible by X or Y
- Find N digits number which is divisible by D in C++
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Generating a random number that is divisible by n in JavaScript
- Smallest number that is divisible by first n numbers in JavaScript
- Greatest number divisible by n within a bound in JavaScript
- Check if product of first N natural numbers is divisible by their sum in Python
- Greatest Sum Divisible by Three in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Maximize the number of sum pairs which are divisible by K in C++
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- JavaScript Program to Count rotations which are divisible by 10
- Smallest possible number divisible by all numbers from 1 to n in JavaScript

Advertisements