
- 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
Subarray sum with at least two elements in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a single Integer, target as the second and first argument. Our function should check whether there exists a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n can be any integer.
We return true if it exists, false otherwise.
For example, if the input to the function is −
const arr = [23, 2, 6, 4, 7]; const target = 6;
Then the output should be −
const output = true;
Output Explanation:
Because [23, 2, 6, 4, 7] is a continuous subarray of size 5 and sums up to 42.
Example
The code for this will be −
const arr = [23, 2, 6, 4, 7]; const target = 6; const checkSubarraySum = (arr = [], target = 1) => { let sum = 0 const hash = {} hash[0] = -1; for (let i = 0; i<arr.length; i++) { sum += arr[i] if (target!=0) sum %= target if ( hash[sum] !== undefined ) { if(i-hash[sum]>1) return true } else { hash[sum] = i } }; return false; }; console.log(checkSubarraySum(arr, target));
Output
And the output in the console will be −
true
- Related Articles
- Shortest Subarray with Sum at Least K in C++
- Largest sum subarray with at-least k numbers in C++
- Maximum Subarray Sum after inverting at most two elements in C++
- Maximum sum subsequence with at-least k distant elements in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
- JavaScript Program to Find the subarray with least average
- Common element with least index sum in JavaScript
- Maximize the subarray sum after multiplying all elements of any subarray with X in C++
- Find all elements in array which have at-least two greater elements in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Maximum Subarray Sum Excluding Certain Elements in C++
- Check for Subarray in the original array with 0 sum JavaScript
- Design a DFA of a string with at least two 0’s and at least two 1’s
- Maximum contiguous sum of subarray in JavaScript
- JavaScript program for Size of the Subarray with Maximum Sum

Advertisements