Pasang Iklan Atas 1

Down Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, October 22, 2018

Java : Codility Test



Question 1
Write a function:

class Solution {
public int solution(int A, int B);
}

that, given two non-negative integers A and B, returns the number of bits set to 1 in the binary representation of the number A * B.

For example, given A = 3 and B = 7 the function should return 3, because the binary representation of A * B = 3 * 7 = 21 is 10101 and it contains three bits set to 1.

Assume that: A and B are integers within the range [0..100,000,000].

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

Answer

public class MyClass {

    public static void main(String args[]) {
        int A = 3;
        int B = 8;
        Integer calc = A * B;
        String binaryParse = Integer.toString(calc,2);
        String[] binArr = binaryParse.split("");
        int result = 0;
        
        for (String a : binArr){
                 if(a.equals("1")){
                     result++;
                 }
        }
        
        System.out.println(result);   
    }
}



Question 2
A non-empty array A consisting of N non-negative integers is given.

For elements A[P] and A[Q] that are distinct, i.e. P ≠ Q, their distance is defined as:

(A[P] − A[Q]) if (A[P] − A[Q]) ≥ 0;
(A[Q] − A[P]) if (A[P] − A[Q]) 0.

Write a function:

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

that, given an array A consisting of N non-negative integers, returns the minimum distance between two distinct elements of A.

For example, given array A such that: A[0] = 8 A[1] = 24 A[2] = 3 A[3] = 20 A[4] = 1 A[5] = 17 the function should return 2, because (A[2] − A[4]) = 2 and no other two distinct elements of A have a smaller distance.

Given array A such that: A[0] = 7 A[1] = 21 A[2] = 3 A[3] = 42 A[4] = 3 A[5] = 7 the function should return 0, because A[0] − A[5] = A[2] − A[4] = 0 and it is the smallest possible distance between two distinct elements of the array.

Write an efficient algorithm for the following assumptions: N is an integer within the range [2..100,000]; each element of array A is an integer within the range [0..1,000,000].

Answer:


public class MyClass {


    public static void main(String args[]) {
        int[] A = {9,3,5,7,8};
        Arrays.sort(A);
        return A[1]-A[0];
    }
}


Link: N/A

Question 3
A six-sided die is a small cube with a different number of pips on each face (side), ranging from 1 to 6. On any two opposite sides of the cube, the number of pips adds up to 7; that is, there are three pairs of opposite sides: 1 and 6, 2 and 5, and 3 and 4. There are N dice lying on a table, each showing the pips on its top face. In one move, you can take one die and rotate it to an adjacent face.

For example, you can rotate a die that shows 1 so that it shows 2, 3, 4 or 5. However, it cannot show 6 in a single move, because the faces with one pip and six pips visible are on opposite sides rather than adjacent. You want to show the same number of pips on the top faces of all N dice. Given that each of the dice can be moved multiple times, count the minimum number of moves needed to get equal faces.

Write a function:

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

that, given an array A consisting of N integers describing the number of pips (from 1 to 6) shown on each die's top face, returns the minimum number of moves necessary for each die to show the same number of pips.

For example, given: A = [1, 2, 3], the function should return 2, as you can pick the first two dice and rotate each of them in one move so that they all show three pips on the top face.

Notice that you can also pick any other pair of dice in this case. A = [1, 1, 6], the function should also return 2.

The only optimal answer is to rotate the last die so that it shows one pip. It is necessary to use two rotations to achieve this. A = [1, 6, 2, 3], the function should return 3.

For instance, you can make all dice show 2: just rotate each die which is not showing 2 (and notice that for each die you can do this in one move).

Assume that: N is an integer within the range [1..100]; each element of array A is an integer within the range [1..6]. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.


Answer:

public class MyClass {


    public static void main(String args[]) {
        int[] A = {1,3,5};
        return A.length - 1;
    }
}

Link: N/A

1 comment :

  1. Kecci Kun: Java : Codility Test >>>>> Download Now

    >>>>> Download Full

    Kecci Kun: Java : Codility Test >>>>> Download LINK

    >>>>> Download Now

    Kecci Kun: Java : Codility Test >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete

Terimakasih sudah berkomentar