
- 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
All possible odd length subarrays JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first and the only input.
The function picks all possible odd length subarrays from the original array, calculate their sum and return the total sum.
Note that by subarray, we mean a contiguous subsequence of the array and not any possible combination of numbers.
For example -
If the input array is −
const arr = [1, 2, 3, 4, 5];
Then all possible odd length arrays will be −
[1], [2], [3], [4], [5], [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 2, 3, 4, 5]
And their total sum will be −
const output = 57
Example
const arr = [1, 2, 3, 4, 5]; const sumArray = (arr = []) => arr.reduce((a, b) => a + b); const oddSum = (arr = []) => { let len = 1; let sum = 0; const { length } = arr; while(len <= length){ for(let i = 0; i + len <= length; i++){ sum += sumArray(arr.slice(i, i + len)); }; len += 2; }; return sum; }; console.log(oddSum(arr));
Output
This will produce the following output −
57
- Related Articles
- Sum of All Possible Odd Length Subarrays in JavaScript
- Program to find sum of all odd length subarrays in Python
- Reverse only the odd length words - JavaScript
- JavaScript Return an array that contains all the strings appearing in all the subarrays
- Smallest possible length constituting greatest frequency in JavaScript
- Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)
- Count subarrays with same even and odd elements in C++
- Find the Number of Subarrays with Odd Sum using C++
- Merging subarrays in JavaScript
- Generating all possible permutations of array in JavaScript
- Find the Length of the Longest possible palindrome string JavaScript
- Find the Number of Subarrays with m Odd Numbers using C++
- Program to find sum of medians of all odd length sublists in C++
- Finding all possible combinations from an array in JavaScript
- Finding all possible subsets of an array in JavaScript

Advertisements