← Back to challenges

A Product Pair

JavaScriptHardalgorithmsarrayslogicloops

Instructions

Mubashir needs your help in the simple task of multiplication of elements in a given array.

Create a function that takes an array of integers arr and a positive integer k and returns the minimum and maximum possible product of k elements taken from the array. If enough elements are not available in the array, return null.

Examples

productPair([1, -2, -3, 4, 6, 7], 1) ➞ [-3, 7]

productPair([1, -2, -3, 4, 6, 7], 2) ➞ [-21, 42]
// -3*7, 6*7

productPair([1, -2, -3, 4, 6, 7], 3) ➞ [-126, 168]
// -3*6*7, 4*6*7

productPair([1, -2, -3, 4, 6, 7], 7) ➞ null
// There are only 6 elements in the array

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.