An algorithm that turns an integer into binary and returns the biggest amount of zeros in the ranges which are between the character “1” of it.

import java.util.*;

class Algorithm {

    public int[] algorithm(int Num) {
        return Arrays.stream(
                Integer
                        .toBinaryString(Num) // turn to binary
                        .replaceAll("(^0+|0+$)", "") // remove the requences which are not between the caracter
                        .split("1")) // create a array where each value is the zeros sequences
                .map(String::length)  // create a map with the length of each sequence
                .reduce(0, Integer::max); // return the max value found

    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s