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