DEV Community

Java Daily Coding Problem #002

Andrew (he/him) on April 22, 2019

Daily Coding Problem is a website which will send you a programming challenge to your inbox every day. I want to show beginners how to solve some o...
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.

Collapse
 
naumanchaudhary profile image
Nauman Naeem

Hey @andrew
Awesome job.
I recently started doing these problems(in Python) and sharing solutions on a public github repo.
But after only 2, I got really busy.
Looking at your posts gave me motivation, I will try to get going with them again.
Thanks man.

Collapse
 
aminmansuri profile image
hidden_dude

"I find myself really wanting pointers in Java right now."

What is it you wish to accomplish with pointers that you can't do in Java here?

Isn't the index as good as a pointer for this?