14,733,301 members
See more:
Hello everyone, am trying to solve codility's Binary Gap question BinaryGap coding task - Learn to Code - Codility[^] but my solution is terrible and it would give you headache but i need help to make it work.
My logic is (1) Convert integer N to binary format (2) Store binary format in a character array (3) Loop through character array starting from the left (the end of the character array) and discard any trailing '0s' till we find the first '1'. (4) If the next character after the first '1' is '0', count the number of '0s' till we find another '1'. (5) If there are still some characters in the array, repeat steps 3 and 4, else return the number of '0s' found as binary gap. This logic should work but putting into good concise Java code is my problem. Please my code is terrible and i couldn't complete it, please if it offends you, kindly forgive me, thanks for any help.

What I have tried:

```public int solution(int N) {

int currentGap = 0, largestGap = 0;

int binaryGap = 0;
// Convert integer to binary value
String binaryString = Integer.toBinaryString(N);

// Put binary value in a charater array
char [] characters = binaryString.toCharArray();

// Run a loop starting from the left and check if the characters are 0 or 1
int j = 0;
for(int i = characters.length - 1; i < characters.length; i--){
char c = characters[i];
// Disregard all trailing zeros till we get to the first 1
if(c == '1'){
j++;
}else if(c == '0'){
// Needs to delete trailing zeros ?
}
}

// I want to set currentGap to the number of zeros before getting to another 1
// then repeat the entire process above if there are still characters in the character array

if(currentGap == largestGap){
return currentGap;
}else if(currentGap < largestGap){
largestGap = binaryGap;
}
return binaryGap;
}
```
Posted
Updated 8-Apr-20 2:49am
v7

## Solution 1

That's a terrible solution: it will never be quick, clean, or efficient. Converting a number of a string is a very poor way to start, as it means the system looking at every bit in the input before you even start looking at them!

Instead, look at Java's binary manipulation operators: https://www.vojtechruzicka.com/bit-manipulation-java-bitwise-bit-shift-operations/[^] - specifically the AND, XOR, and the SHIFT operators.

If you want to create a truly efficient method, then have a look here: A C# implementation of AI MEMO 239, Item 169- Count the one bits in a number[^] - it will not do what you want, but it might give you a clue what you can do with an efficient algorithm and bit manipulation ...
Maciej Los 8-Apr-20 2:15am

5ed!

## Solution 2

Quote:
but my code is TERRIBLE! ! !

Indeed, terrible is the word.
You start with 1 idea (currentGap, largestGap, binaryGap), then switch to another idea in the loop, then come back to first idea when trying to spit out the result.
A complete rewrite is in order.
Advice: Solve the problem by hand, write down how you have done, solve problem again by applying the procedure, change procedure as needed.
When procedure works, it is your algorithm, write code accordingly.

In order to help you understand how your code behave, use the debugger and watch it perform.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

## Solution 3

@OriginalGriff, @Patrice T, thanks a lot.