Last active
March 19, 2018 04:08
-
-
Save naveedn/ede7ccf065a354fc8b0a47e50ef37ab7 to your computer and use it in GitHub Desktop.
Medium Article Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = [1,3,5,7]; | |
const fn = (inputArr) => | |
inputArr | |
.map(x => [...inputArr]) | |
.map((x, i) => { | |
const firstPart = x.slice(0, i); | |
const lastPart = x.slice(i+1); | |
return [...firstPart, ...lastPart]; | |
}) | |
.map(x => x.reduce((elem, acc) => acc * elem)); | |
console.log(fn(input)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Algorithmic Complexity:
Speed: O(n2)
Space: O(n2)
Alternative Approach: https://gist.github.com/naveedn/9ad30d2f9bc6789aea24f1e5156d6934