-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy patharray_of_products.go
58 lines (52 loc) · 1.24 KB
/
array_of_products.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import "fmt"
/**
* Given an array of integers A, find and return the product array of the same size where the ith element of the product array will be equal to the product of all the elements divided by the ith element of the array.
*
* Note: It is always possible to form the product array with integer (32 bit) values. Solve it without using the division operator.
*
*
* Input Format
*
* The only argument given is the integer array A.
* Output Format
*
* Return the product array.
* Constraints
*
* 2 <= length of the array <= 1000
* 1 <= A[i] <= 10
* For Example
*
* Input 1:
* A = [1, 2, 3, 4, 5]
* Output 1:
* [120, 60, 40, 30, 24]
*
* Input 2:
* A = [5, 1, 10, 1]
* Output 2:
* [10, 50, 5, 50]
*/
func ArrayOfProducts(Arr[]int) {
Result := make([]int, len(Arr))
product := 1
// fill left prefix product in result array
for i := 0; i < len(Arr); i++ {
Result[i] = product
product = product * Arr[i]
}
// fill right prefix product in result array
product = 1
for i := len(Arr) - 1; i >= 0; i-- {
Result[i] = Result[i] * product
product = product * Arr[i]
}
for i := 0; i < len(Arr); i++ {
fmt.Print(Result[i]," ")
}
}
func main() {
Arr := []int{5, 1, 10, 1}
ArrayOfProducts(Arr)
}