
- 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
Equal partition of an array of numbers - JavaScript
We are required to write a function that returns true if we can partition an array into one element and the rest, such that this one element is equal to the product of all other elements excluding itself, false otherwise.
For example: If the array is −
const arr = [1, 56, 2, 4, 7];
Then the output should be true
Because, 56 is equal to −
2 * 4 * 7 * 1
Example
Following is the code −
const arr = [1, 56, 2, 4, 7]; const isEqualPartition = arr => { const creds = arr.reduce((acc, val) => { let { prod, max } = acc; if(val > max || !max){ prod *= (max || 1); max = val; }else{ prod *= val; } return { prod, max }; }, { prod: 1, max: null }); return creds.max === creds.prod; }; console.log(isEqualPartition(arr));
Output
Following is the output in the console −
true
- Related Articles
- Are array of numbers equal - JavaScript
- Maximum average sum partition of an array in C++
- Partition Array Into Three Parts With Equal Sum in Python
- Smallest Common Multiple of an array of numbers in JavaScript
- Realtime moving average of an array of numbers in JavaScript
- Product of all other numbers an array in JavaScript
- Sum of all prime numbers in an array - JavaScript
- Calculating variance for an array of numbers in JavaScript
- Equal Tree Partition in C++
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Finding missing element in an array of numbers in JavaScript
- Performing power operations on an array of numbers in JavaScript
- Partition Equal Subset Sum in C++
- Squared and square rooted sum of numbers of an array in JavaScript
- Maximum count of equal numbers in an array after performing given operations in C++

Advertisements