-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayOfProducts.java
38 lines (31 loc) · 1.16 KB
/
ArrayOfProducts.java
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
import java.util.Arrays;
import java.util.List;
/**
* 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.
*
* 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].
*/
public class ArrayOfProducts {
public static void main(String[] args){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] arrayOfProducts = getArrayOfProducts(list);
System.out.println("Array of products: ");
for(int number : arrayOfProducts){
System.out.print(number + ", ");
}
}
private static int[] getArrayOfProducts(List<Integer> list) {
int[] array = new int[list.size()];
Integer productOfAll = 1;
for(int number : list){
productOfAll *= number;
}
System.out.println("productOfAll : " + productOfAll);
for(int i=0; i < list.size(); i++){
array[i] = productOfAll / list.get(i);
}
return array;
}
}