Total Number – Divisible number


public static void main(String[] args) throws ParseException {
int[] input = { 3, 4719, 5761, 80, 30, 9907, 10, 5, 9084, 17};

int T = Integer.valueOf(input[0]);
int proces = 1;
while (T != 0) {
for (int i = 1; i = 1 && A <= B && B < 10000) {
if (K < 10000) {
int totalDiv = (int) Math.floor(B / K);
int excludeDivisibles = (int) Math.floor((A - 1) / K);
System.out.println("Case " + i + ": " + (totalDiv - excludeDivisibles));
T--;
}
}
}
}

}

Advertisements

How to loop a Map in Java

How to loop iterate map  or hash map in java

Map<String, String> map = new HashMap<String, String>();
map.put("1", "SATU");
map.put("2", "DUA");
map.put("3", "TIGA");

//loop a Map
for (Map.Entry<String, String> entry : map.entrySet()) {
     System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
//Java 8 only
map.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v));

Test Java Codility

Interesting Patterns Test

In this task we will investigate interesting patterns that could be observed on a digital clock. Such a clock represents a point in time using the format “HH:MM:SS” where:

  • HH” is the hour of the day (00 through 23), as two decimal digits;
  • MM” is the minute within the hour (00 through 59), as two decimal digits;
  • SS” is the second within the minute (00 through 59), as two decimal digits.

We say that a point in time is interesting if it uses at most two
different digits to print. The goal is to count the number of
interesting points that can be observed on the clock in a given period
of time.

Write a function:

class Solution { public int solution(String S, String T); }

that, given strings S and T specifying points in time in the format “HH:MM:SS“,
returns the number of interesting points in time between S and T
(inclusive). You can assume that S indicates a point in time before T on
the same day.

For example, given “15:15:00” and “15:15:12“, your function should return 1, because there is only one interesting point in time between these points (namely “15:15:11“), Given “22:22:21” and “22:22:23“, your function should return 3; interesting points in time are “22:22:21“, “22:22:22“, and “22:22:23“.

Assume that:

  • strings S and T follow the format “HH:MM:SS” strictly;
  • string S describes a point in time before T on the same day.

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

Answer:


    private int task2(String S, String T) {
         int res=0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            Date date1 = sdf.parse(S);
            Date date2 = sdf.parse(T);
            long diff = date2.getTime() - date1.getTime();
            long diffSeconds = diff / 1000 % 60;
            //long diffMinutes = diff / (60 * 1000) % 60;
            //long diffHours = diff / (60 * 60 * 1000) % 24;

            String[] result = new String[Integer.valueOf(((int) diffSeconds) + 2) - 1];
            for (int i = 0; i < result.length; i++) {
                if (i == 0) {
                    result[0] = S;
                } else {
                    Date d1 = sdf.parse(result[0]);
                    Date d = new Date(d1.getTime() + ((i) * 1000));
                    result[i] = sdf.format(d);
                }
            }
           
            HashSet<Integer> set = new HashSet<Integer>();
            for (int i = 0; i < result.length; i++) {
                set = new HashSet<Integer>();
                String str = result[i];
                String[] strA = str.split(":");
                for (int j = 0; j < strA.length; j++) {
                    String strD = strA[j];
                    char[] c = strD.toCharArray();
                    char c1 = c[0];
                    char c2 = c[1];
                    if (c1 == c2) {
                        set.add(Integer.valueOf(c1));
                    } else {
                        set.add(Integer.valueOf(c1));
                        set.add(Integer.valueOf(c2));
                    }
                }
                if(set.size() <= 2 ){
                    res++;
                }
            }
            
        } catch (ParseException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

 

Table Array Test

You have an array of numbers and you would like to print these
numbers in a tabular format to make them look more organized. Each cell
of the table contains exactly one number and is surrounded by exactly
four edges:

Capture1

As you can see above, each corner of the cell is represented by a “+” sign, vertical edges by “-” signs and horizontal edges by “|
signs. The width of the cell adjusts to accommodate the number of
digits of the number written within it. There can be many cells in a
row. Adjacent cells share an edge:

Capture1

Note that each cell has the same width. The width of the cell adjusts
to match the width of the longest number in the table. The numbers in
cells are aligned to the right, with any unused area in each cell filled
with spaces.

The table can consist of many rows, and adjacent rows share an edge:

capture2.png

Your goal is to output a table containing all the numbers from a
given array such that each row contains exactly K numbers. The last row
can contain fewer numbers.

Write a function:

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

that, given a non-empty array A consisting of N integers and an
integer K, prints a string representing the formatted array. The numbers
in the table should appear in the same order as the numbers in the
array.

For example, given array A = [4, 35, 80, 123, 12345, 44, 8, 5] and K =
10, the resultant table will contain exactly one row, as shown below:

Capture3

For K = 4, the table would appear as follows:

capture31.png

Given A = [4, 35, 80, 123, 12345, 44, 8, 5, 24, 3, 22, 35] and K = 4, the table would appear as follows:

Capture3

The function shouldn’t return any value.

You can print a string to the output (without or with the end-of-line character) as follows:

System.out.print("sample string");
System.out.println("whole line");

Assume that:

  • N is an integer within the range [1..200];
  • K is an integer within the range [1..1,000,000,000];
  • each element of array A is an integer within the range [0..1,000,000,000].

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

Answer:

 public static void main(String[] args) {
        // TODO code application logic here
        int[] A = new int[10];
        A[0] = 4;
        A[1] = 35;
        A[2] = 80;
        A[3] = 123;
        A[4] = 12345;
        A[5] = 44;
        A[6] = 8;
        A[7] = 5;
        A[8] = 24;
        A[9] = 3;

        int K = 2;
        Integer c = (Integer.valueOf(A[0]).toString().toCharArray()).length;
        char[] c1 = c.toString().toCharArray();

        for (int i = 0; i < A.length; i++) {
            c1 = Integer.valueOf(A[i]).toString().toCharArray();
            if (c < c1.length) {
c = c1.length;
}
}
tes(A, c, K);
}
private static void tes(int[] A, Integer c, int loop) {
int K = loop;
if (loop > A.length) {
            K = loop - (loop % A.length);
        }

        for (int i = 0; i < K; i++) {
            System.out.print("+");
            for (int j = 0; j < c; j++) {
System.out.print("-");
}
}
System.out.println("+");
int v = 0;
int e = 0;
if (A.length % loop > 0) {
            e = (A.length / loop) + 1;
        } else {
            e = A.length / loop;
        }

        for (int r = 0; r < e; r++) {
            System.out.print("|");
            for (int i = 0; i < K; i++) {
for (int j = c; j >= 1; j--) {
                    if (v >= A.length) {
                        break;
                    }
                    if ((Integer.valueOf(A[v]).toString().toCharArray()).length == j) {
                        System.out.print(A[v]);
                        j = j - (Integer.valueOf(A[v]).toString().toCharArray()).length;
                    } else {
                        System.out.print(" ");
                    }
                }
                if (v >= A.length) {
                    break;
                }
                System.out.print("|");
                v++;
            }
            System.out.print("\n");
            //footer
            if (v >= A.length) {
                if (A.length > loop) {
                    for (int i = 0; i < (A.length % loop == 0 ? loop : A.length % loop); i++) {
                        System.out.print("+");
                        for (int j = 0; j < c; j++) {
                            System.out.print("-");
                        }
                    }
                    System.out.println("+");
                } else {
                    for (int i = 0; i < K; i++) {
                        System.out.print("+");
                        for (int j = 0; j < c; j++) {
                            System.out.print("-");
                        }
                    }
                    System.out.println("+");
                }
                break;
            }
            for (int i = 0; i < K; i++) {
                System.out.print("+");
                for (int j = 0; j < c; j++) {
                    System.out.print("-");
                }
            }
            System.out.println("+");
        }
    }

Test Java

1. A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

For example, in array A such that:

A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9

  • the elements at indexes 0 and 2 have value 9,
  • the elements at indexes 1 and 3 have value 3,
  • the elements at indexes 4 and 6 have value 9,
  • the element at index 5 has value 7 and is unpaired.

Answer:

   int x =0;
   for (int i = 0; i < A.length; i++) {
	x = x^A[i];
   }
   System.out.println(x);

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).

Answer:

		String n = Integer.toBinaryString(20);
		int gap =0;
		int gap2 =0;
		int d = 0;
		char[] c =n.toCharArray();
		boolean s = false;
		for (int i = 0; i < c.length; i++) { 			if(d > 0){
				if('0' == c[i]){
					gap++;
				}else{
					d = 0;
				}
			}
			if('1' == c[i]){
				d = 1;
				if(gap > gap2){
					gap2 = gap;
				}
				gap = 0;
			}
		}
		System.out.println(gap2);			

3. 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].

Answer :

      int[] A = new int[5];
		A[0] = 3;
		A[1] = 8;
		A[2] = 9;
		A[3] = 7;
		A[4] = 6;
		
		int K =3;
		
		int[] B = new int[A.length];
		
		for (int i = 0; i < A.length; i++) {
			System.out.println((i + K ) % A.length);
			B[(i + K ) % A.length] = A[i];
		}
		
		System.out.println(B);

Membuat Segitiga Menggunakan JAVA

1. Contoh membuat segitiga seperti dibawah ini

*
* *
* * *
* * * *

for (int i = 0; i < 4; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}

2. Segitiga terbalik

* * * * * * *
  * * * * *
    * * *
       *
 int i, j;
int n =4;
for (i = n; i >= 1; i--) {
for (j = 0; j < n - i; j++){
System.out.print(" ");
}
for (j = 0; j < 2 * i - 1; j++){
System.out.print("*");
}
System.out.println("");
}
System.out.println("\n");

3. Segitiga terbalik 2
*****
****
***
**
*

int n = 5;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < (n - i); j++) {
System.out.print(" * ");
}
for (int x = 0; x < (2 * i - 1); x++) {
System.out.print(" ");
}
System.out.println("");
}

3. Segitiga seperti berikut:

*****
****
***
**
*

int n = 5;
for (int i = 0; i <= n; i++) {
   for (int x = 0; x < i; x++) {
      System.out.print(" ");
   }
   for (int j = 0; j < (n - i); j++) {
      System.out.print("*");
   }
System.out.println("");
}

Test Algoritma

  • Mencari  bilangan terbesar dari array int, contoh input array [2 3 1 1 4 5 2 5] dan output yang dihasilkan 2.
int max = ar[0];
int res=0;
for(int i=0; i= max){
max = ar[i];
}
}
for(int i=0; i< n; i++){
if(ar[i] == max){
res++;
}
}System.out.println(res)
  • Konversi waktu dari format 12 hour ke format 24 hour, contoh input 07:05:45PM output 19:05:45
try{
SimpleDateFormat ori = new SimpleDateFormat("hh:mm:ssaa");
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date date = ori.parse("07:05:45PM");
System.out.println(df.format(date));
}catch(Exception e){
System.err.println(e);
}
  • There are two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location and moves at a rate of meters per jump. The second kangaroo starts at location and moves at a rate of meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they’ll ever land at the same location at the same time?
static String kangaroo(int x1, int v1, int x2, int v2) {
if(v1 > v2 && ((x2-x1)%(v1-v2) == 0))
return "YES";
else
return "NO";
}
public static void main(String[] args) {
String result = kangaroo(0, 3, 4, 2);
System.out.println(result);
}
  • Consider two sets of positive integers, A and B . We say that a positive integer,X , is between sets A and B if the following conditions are satisfied:All elements in A are factors of X .
    X is a factor of all elements in B .In other words, some is between and if that value of satisfies for every in and also satisfies for every in . For example, if and , then our possible values are and .Given A and B , find and print the number of integers (i.e., possible X’s) that are between the two sets.
public static boolean hasFactors(int num, int[] arr){
for(int i=0;i<arr.length;i++){
if(num%arr[i]!=0){
return false;
}
}
return true;
}

public static boolean isFactor(int num, int[] arr){
for(int i=0;i<arr.length;i++){
if(arr[i]%num!=0){
return false;
}
}
return true;
}

public static void main(String[] args) {
try{

Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int[] b = new int[m];
for(int b_i=0; b_i < m; b_i++){
b[b_i] = in.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
int min = a[0];
int max = b[b.length-1];
int count=0;
//System.out.println(max+" "+min);
for(int i=min;i<=max;i++){
if(hasFactors(i,a) && isFactor(i,b)){
count++;
}
}
System.out.println(count);
}

Error Linux – Centos “No acceptable C compiler found in $PATH”

Pernah ngalamin error pas mau install php pdo di Centos, dengan error “No acceptable C compiler found in $PATH” ? ini solusinya

# yum install gcc

RabbitVCS tidak jalan di Ubuntu 13.04 64 bit (Solved)

Masalah ini gw dapatkan pada saat menginstall RabbitVCS di ubuntu 64 bit, pada waktu install RabbitVCS di ubuntu 32 bit gw tidak menemukan masalah sama sekali. oke cukup basa – basinya langsung aja…

1. Buat link
$ sudo ln -sf /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 /usr/lib/

$ sudo ln -sf /usr/lib/x86_64-linux-gnu/libpython2.7.so.1 /usr/lib/

2. Download RabbitVCS.py

download RabbitVCS yang khusus untuk 64 bit disini, lalu copy-kan file tersebut ke
/usr/share/nautilus-python/extensions/RabbitVCS.py setelah itu logut dan login kembali, rasakan perubahannya…

Sumber : AskUbuntu

Kaos Despicable Me Football – Part 1

Kaos Despicable Me Football – Part 1.

Enable SSH di Linux Ubuntu 13.04

Jumpa lagi…..

Pada posting kali ini saya akan menunjukkan bagaimana menginstall ssh di Ubuntu 13.04. d ubuntu ketika kamu menginstall open-ssh server, maka ssh akan otomatis di-enable kan oleh ubuntu.

Step 1.

# sudo apt-get install openssh-server

Step 2.

Restart ssh dengan perintah berikut :
# sudo /etc/init.d/ssh restart

Step 3.

Jika anda ingin mengganti port ssh, silahkan ganti di sshd_config
# gksudo gedit /etc/ssh/sshd_config