Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Inspired by this questionquestion I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$log(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$log(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$log(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}
typo: lon to log
Source Link

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$lon(N)\$\$log(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$lon(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$log(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}
jaxify
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b100000100010b10000010001, thus the answer is 5). The task also requires the complexiticomplexity of O(log(N))\$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is lon(N)\$lon(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexiti of O(log(N)). As far as I understand it means that it means linear complexity in terms of digits, whose number is lon(N). Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == 0b10000010001, thus the answer is 5). The task also requires the complexity of \$O(log(N))\$. As far as I understand it means that it means linear complexity in terms of digits, whose number is \$lon(N)\$. Am I correct in this conclusion?

private int biggestBinaryGap(int n) {
    while (endsWithZero(n))
        n >>= 1;
    return biggestBinaryGapRec(n >> 1);
}

private int biggestBinaryGapRec(int n) {
    if (n == 0)
        return 0;

    int gap = 0;
    for (; endsWithZero(n); n >>= 1, gap++);
    return Math.max(gap, biggestBinaryGapRec(n >> 1));

}

private boolean endsWithZero(int n) {
    return n > 0 && (n & 1) == 0;
}
Source Link
Loading