Thursday, August 25, 2016

A "binary gap" task from http://codility.com with solution

After 24 years of coding I found so my coding skills come to degradation. For last two day I polish them by using http://codility.com I place here some examples from there with my solutions . I use this this blog as chit sheet and to memorise that nice piece of mind twister :
  Task definition: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps.
Write a function:
class Solution { public int solution(int N); }
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.
Assume that:
  • N is an integer within the range [1..2,147,483,647].
Complexity:
  • expected worst-case time complexity is O(log(N));
  • expected worst-case space complexity is O(1).
 Solution:
/ you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int solution(int N) {
        // write your code in Java SE 8
        
        int dimension = binlog(N);
        byte A[] = new byte[dimension];
        int i = 0;
        int zeros = 0;
        int ones = 0;
        int max = -1;
        do
        {
            int shift = N & 0x00000001;
            if( shift == 1 )
            {
                zeros = 0;
                ones += 1;
            }
            else 
            {
                zeros += 1; 
                ones = 0;
            }
            if( zeros > max ) max = zeros;
            
            N >>>= 1;
        }  while ( i ++ < dimension);
        
      return max;
    }
    
   public static int binlog( int bits ) // returns 0 for bits=0
   {
    int log = 0;
    if( ( bits & 0xffff0000 ) != 0 ) { bits >>>= 16; log = 16; }
    if( bits >= 256 ) { bits >>>= 8; log += 8; }
    if( bits >= 16  ) { bits >>>= 4; log += 4; }
    if( bits >= 4   ) { bits >>>= 2; log += 2; }
    return log + ( bits >>> 1 );
   }
}
This even more shorter solution based on recursion
static int solution(int n) {
    return solution(n, 0, 0);
}

static int solution(int n, int max, int current) {
    if (n == 0)
        return max;
    else if (n % 2 == 0)
        return solution(n / 2, max, current + 1);
    else
        return solution(n / 2, Math.max(max, current), 0);
}
Test result:
Running solution...
Compilation successful.

Your test case: [529]
Returned value: 4 

Your test case: [9]
Returned value: 2 

Your test case: [20]
Returned value: 2 

Example test:   1041
OK 

Example test:   15
OK 

Your code is syntactically correct and works properly on the example test.
Note that the example tests are not part of your score. On submission at least 8 test cases not shown here will assess your solution.

No comments: