DEV Community

loading...

Discussion on: Java Daily Coding Problem #002

Collapse
endangeredsouls profile image
!!!ɘɔɿoꟻ ɘɔɒqꙄ ɘʜT ᴎioႱ • Edited

Java 8:

   int[] array = { 1, 2, 3, 4, 5 };
    final int product = Arrays.stream(array)
            .reduce((x,y) -> x * y )
            .getAsInt();

    int[] newArray = Arrays.stream(array)
            .map(x -> product / x)
            .toArray();

    System.out.println(Arrays.toString(newArray));

Fails for elements with a 0, obviously, could be worked around with filtering.