Bytelandian Coins - wrong answer

Not sure what is wrong with this code. These are the input/output that I get
12 --> 13
13 --> 16
When I submit it, the system tell wrong answer. Any suggestions will be appreciated.

import java.util.HashMap;

public class ByteLandianColdCoin {
public static HashMap map = new HashMap(); 
public static void main(String[] args) {
	try {
		BufferedReader br = new BufferedReader(new InputStreamReader(;
		String s;
		while ((s= br.readLine()) != null && s.length()!=0)
			double i = Double.parseDouble(s.trim());
			System.out.println(new Double(change(i, "")).longValue());
	} catch (Exception e) {
		System.err.println("Error:" + e.getMessage());


public static double change(double coin, String ind)
	//System.out.println(ind+" coin: "+coin);
	Double ddollar = ((Double)map.get(coin));
	if(ddollar!= null)
		//System.out.println(ind+" dollar:"+ddollar.doubleValue());
		return ddollar.intValue();

	ind = ind+" * ";
	//System.out.println(ind+" in change -"+coin);

	if(coin <12)
		map.put(coin, coin);
		return coin;

	double dollar = change(Math.ceil(coin/2), ind)+change(Math.ceil(coin/3), ind)+change(Math.ceil(coin/4), ind);
	dollar = coin>dollar?coin:dollar;

	map.put(coin, dollar);
	//System.out.println(ind+" dollar:"+dollar);
	return dollar;


  1. You have parsed input as a Double and printed out its longValue but in your change function you have returned intValue of coins if present in the map.
  2. The recurrence you used dollar = change(Math.ceil(coin/2), ind)+change(Math.ceil(coin/3), ind)+change(Math.ceil(coin/4), ind) seems incorrect.
  3. I am not sure whether you are allowed to write to the error stream on Codechef.


  1. Use Long instead of Double.
  2. Remove ind if its not used.
  3. Re-read the question to get the correct recurrence.
  4. Check FAQ to make sure if you are allowed to write to error stream.

use floor instead of ceil bcoz in question its written “rounded down” and not “rounded up”