Skip to content

Instantly share code, notes, and snippets.

@folksilva
Last active July 19, 2018 16:37
Show Gist options
  • Save folksilva/2e8f3bd4cb290028dc30c104447ce390 to your computer and use it in GitHub Desktop.
Save folksilva/2e8f3bd4cb290028dc30c104447ce390 to your computer and use it in GitHub Desktop.
Daily Coding Problem: Problem #2
"""
This problem was asked by Uber.
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. Solve it without using division and in O(n) time.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
https://dailycodingproblem.com/
"""
def problem2solver(numbers):
numbers_product = []
for i in range(0, len(numbers)):
product = 1
for j in range(0, len(numbers)):
if not i is j:
product *= numbers[j]
numbers_product.append(product)
return numbers_product
if __name__ == '__main__':
# Read input, numbers separated by commas
numbers = [int(n) for n in input().split(',')]
print(problem2solver(numbers))
@PulkitAgg
Copy link

Simple solution
Step 1: First find the product of all the array elements and store in some variable let's sat prod.
Step 2: Divide prod variable by each array element.
Algo:
Given Arr = [1,2,3,4,5];
let prod = 1;
let outArr = [];
for(let count =0 ; count < Arr.length ; count++){
prod *= Arr[count];
}

for(let count =0 ; count < Arr.length ; count++){
outArr[count] = prod / Arr[count];
}
console.log(outArr)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment