From 52c341b1c141bb528a257ddd8efd6f387183c65a Mon Sep 17 00:00:00 2001 From: Darsh Gandhi <32832454+darshmgandhi@users.noreply.github.com> Date: Sat, 3 Oct 2020 01:10:08 +0530 Subject: [PATCH] Solution for problem code COINS from codechef This is for hacktoberfest --- COINS.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 COINS.java diff --git a/COINS.java b/COINS.java new file mode 100644 index 0000000..5754756 --- /dev/null +++ b/COINS.java @@ -0,0 +1,28 @@ +import java.util.Scanner; +import java.util.HashMap; + +class COINS{ + + public static HashMap hm = new HashMap<>();; + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + long t; + while (sc.hasNext()) { + t = sc.nextLong(); + System.out.println(split(t)); + } + } + + public static long split(long coin_value) { + if (hm.containsKey(coin_value)) { + return hm.get(coin_value); + } else if (coin_value > (coin_value/2 + coin_value/3 + coin_value/4) || coin_value == 0) { + hm.put(coin_value, coin_value); + return coin_value; + } else { + hm.put(coin_value, (split(coin_value/2) + split(coin_value/3) + split(coin_value/4))); + return hm.get(coin_value); + } + } +}