Author Archive


February 22, 2015 Leave a comment

Problem Statement

At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes?

Input Format
The first line contains the number of test cases T, T lines follow.
Each line then contains an integer N, the total number of Board of Directors of Acme.

Output Format

Print the number of handshakes for each test-case in a new line.


1 <= T <= 1000
0 < N < 106

This problem is about combinations. The formula is

For factorial calculation I’m using recursive with memoization to optimization process. Read more…

The Love-Letter Mystery

February 22, 2015 Leave a comment

James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter into palindromes.

To do this, he follows 2 rules:

  1. He can reduce the value of a letter, e.g. he can change ‘d’ to ‘c’, but he cannot change ‘c’ to ‘d’.
  2. In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes ‘a’. Once a letter has been changed to ‘a’, it can no longer be changed.

Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.

Read more…

Categories: Umum Tags: , ,

MongoDB Learning Notes – Simulate automate failover

February 22, 2015 Leave a comment

To simulate automate failover I kill the primary node with Ctrl+C or use command db.shutdownServer(). When the primary killed, the secondary become primary because a majority of the original nodes (the arbiter and the secondary) are still able ping each other.

myapp:PRIMARY> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
myapp:PRIMARY> use admin
switched to db admin
myapp:PRIMARY> db.shutdownServer()

And in secondary node log:

2015-02-22T13:58:33.558+0700 [rsHealthPoll] replSet member bluething-PC:20000 is now in state DOWN
2015-02-22T13:58:34.391+0700 [conn501] replSet info voting yea for bluething-PC:20001 (1)
2015-02-22T13:58:36.565+0700 [rsHealthPoll] warning: Failed to connect to, reason
: errno:10061 No connection could be made because the target machine actively refused it.
2015-02-22T13:58:36.565+0700 [rsHealthPoll] replset info bluething-PC:20000 heartbeat failed, retrying
2015-02-22T13:58:36.632+0700 [rsHealthPoll] replSet member bluething-PC:20001 is now in state PRIMARY

Connect to secondary and check status Read more…

MongoDB Learning Notes – Replication Overview and Setup replica set

February 22, 2015 Leave a comment

MongoDB provides two flavors of replication: master-slave replication and replica sets. For both, a single primary node receives all writes, and then all secondary nodes read and apply those write to themselves asynchronously. Replica sets additionally ensure automated failover: if the primary goes offline for any reason, then one of the secondary nodes will automatically be promoted to primary, if possible.

The minimum recommended replica sets configuration consists of three nodes. Two of these nodes serve as first-class, persistent mongod instance. Either can act as the replica set primary, and both have a full copy of the data. The third node in the set is an arbiter, which doesn’t replicate data, but merely acts as a kind of neutral observer, when failover is required, the arbiter helps to elect a new primary node.

1. Creating a data directory
I’m using windows and data directory is C:\data

mkdir C:\data\node1
mkdir C:\data\node2
mkdir C:\data\arbiter

2. Start each node as separate mongod (in separate terminal) Read more…

Categories: MongoDB, NoSQL Tags: , ,

Send logs by Email Notification using Apache log4j SMTPAppender

February 15, 2015 Leave a comment

Apache log4j provides log Appender (called SMTPAppender) to send email alerts for the log level configured in log4j configuration file. For example I using simple struts 2 application.


<project xmlns="" xmlns:xsi=""
 <name>SimpleStruts2 Maven Webapp</name>







Read more…

Categories: JAVA



Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly because it is a pangram. ( pangrams are sentences constructed by using every letter of the alphabet at least once. ) After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s, tell Roy if it is a pangram or not.

From Wikipedia, A pangram or holoalphabetic sentence for a given alphabet is a sentence using every letter of the alphabet at least once. Check both uppercase and lowercase(+32).

Using Java String function indexOf() to check if the character appear in the sentence.

Java code:

import java.util.Scanner;

public class Pangram {

public static void main(String[] args) {
 Scanner in = new Scanner(;
 String input = in.nextLine();
 if(testPangram(input)) {
 } else {
 System.out.println("not pangram");

 static boolean testPangram(String input) {
 for(char a = 'A' ; a <= 'Z' ; a++) {
 if(input.indexOf(a) < 0 && input.indexOf((char) a+32) < 0) {
 return false;
 return true;

Result for input “We promptly judged antique ivory buckles for the prize “:





Categories: JAVA Tags: , ,

Maximizing XOR


Given two integers: L and R, find the maximal values of A xor B given, L ≤ A ≤ B ≤ R

Java code:

import java.util.Scanner;

public class MaximizingXOR {

public static void main(String[] args) {
Scanner in = new Scanner(;
int l = in.nextInt();
int r = in.nextInt();
int max = 0;
for(int b=l; b<= r; b++) {
for(int a=b ; a <=r; a++) {
max = max > (a ^ b) ? max : (a ^ b);

Result for input 1 and 10:


Result for input 10 and 15:



Categories: Umum Tags: , ,

Get every new post delivered to your Inbox.