Pergunta de entrevista da empresa Swabhav

Write a function that accepts an array of n integers, array[]. Create a new array, prod[] such that prod[i] is equal to the product of all the elements of array[] except array[i]. The function should have a time complexity of O(n) and handle all possible input scenarios effectively. Ex. Array{}={2,3,4,5} then prod[]={60,40,30,24}

Resposta da entrevista

Sigiloso

28 de jun. de 2025

The approach to this question should be, first find the product of all integers in the array and then divide that product with array[i]. That would give us Product of all elements excluding the array[i] element for array[i] OR You can multiply all numbers before array[i] in a prefix product, then multiply all numbers after array[i] in a suffix product (using two loops) and then multiply both these products. More failsafe than the division method, incase the array contains a Zero (unlikely, but you never know)