I am looking for help in understanding what the Time/Space complexity of my solution, for finding permutations of an array, is. I know that because I am using an Array.forEach method that my time complexity includes O(n), however since I am also using recursion I do not know how the time complexity changes. The recursion seems to me to be in O(n) time complexity as well. Does that make the overall time complexity of the algorithm O(n^2)? And for space complexity is that 0(n) as well? since each recursion call returns a bigger memory array? Thanks in advance.
function getPermutations(array) {
if (array.length <= 1){
return array.length === 0 ? array : [array];
}
let lastNum = array[array.length - 1]
let arrayWithoutLastNum = array.slice(0, array.length - 1);
let permutations = getPermutations(arrayWithoutLastNum);
let memory = []
permutations.forEach(element => {
for(let i = 0; i <= element.length; i++){
let elementCopy = element.slice(0);
elementCopy.splice(i, 0, lastNum)
memory.push(elementCopy)
}
})
return memory
}