
- 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
Left right subarray sum product - JavaScript
We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-array (left and right) containing N/2 elements each and do the sum of the subarrays and then multiply both the subarrays.
For example: If the input array is −
const arr = [1, 2, 3, 4]
Then the output should be −
(2+1) * (3+4) = 21
Example
Following is the code −
const arr = [1, 2, 3, 4] const subArrayProduct = arr => { const { length: l } = arr; const creds = arr.reduce((acc, val, ind) => { let { left, right } = acc; if(ind < l/2){ left += val; }else{ right += val; } return { left, right }; }, { left: 0, right: 0 }); return creds.left * creds.right; }; console.log(subArrayProduct(arr));
Output
Following is the output in the console −
21
- Related Articles
- Subarray with the greatest product in JavaScript
- Product of subarray just less than target in JavaScript
- Maximum contiguous sum of subarray in JavaScript
- Maximum subarray sum in circular array using JavaScript
- Subarrays product sum in JavaScript
- Maximum Product Subarray in Python
- Subarray sum with at least two elements in JavaScript
- Find the following product.\( \left(\frac{-2}{7} a^{4}\right) \times\left(\frac{-3}{4} a^{2} b\right) \times\left(\frac{-14}{5} b^{2}\right) \)
- Maximum Product Subarray | Added negative product case in C++
- Simplify \( \left[\left\{\left(\frac{1}{2}\right)^{2}\right\}^{-2}\right]^{-2} \).
- Removing smallest subarray to make array sum divisible in JavaScript
- Find the following product.\( \left(\frac{-24}{25} x^{3} z\right) \times\left(\frac{-15}{16} x z^{2} y\right) \)
- Largest Sum Contiguous Subarray
- Shift strings Circular left and right in JavaScript
- Find the following product.\( \left(\frac{7}{9} a b^{2}\right) \times\left(\frac{15}{7} a c^{2} b\right) \times\left(\frac{-3}{5} a^{2} c\right) \)

Advertisements