문제:

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).
Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.


결과: https://codility.com/demo/results/trainingHWYAS7-MEK/


// 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 max = 0; int cnt = 0; //int k=0; String result = ""; result = Integer.toBinaryString(N); for(int i=0; i < result.length(); i++) { //k++; if(result.charAt(i) == '0') { cnt++; //if( (result.length() == k ) && (cnt > max) ) { // max = cnt; //} } else { if(cnt > max) { max = cnt; } cnt = 0; } } return max; } }

문제:

https://codility.com/programmers/lessons/2-arrays/

Task description

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

Write a function:

class Solution { public int[] solution(int[] A, int K); }

that, given a zero-indexed array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given array A = [3, 8, 9, 7, 6] and K = 3, the function should return [9, 7, 6, 3, 8].

Assume that:

  • N and K are integers within the range [0..100];
  • each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2016 by Codility Limited. All Rights Reserved.

결과: 


https://codility.com/demo/results/trainingM2FM5Q-6BC/


// 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[] A, int K) { // write your code in Java SE 8 int temp = 0; int cnt = K; if(A == null || A.length < 2 || K <= 0) { return A; } while( cnt > 0 ){ temp = A[A.length-1]; for(int i=A.length-1; i >0 ; i--) { A[i] = A[i-1]; } A[0] = temp; cnt--; } return A; } }

+ Recent posts