diff --git a/Java/Recursion/GCD.class b/Java/Recursion/GCD.class new file mode 100644 index 0000000..87f4271 Binary files /dev/null and b/Java/Recursion/GCD.class differ diff --git a/Java/Recursion/GCD.java b/Java/Recursion/GCD.java new file mode 100644 index 0000000..697ddac --- /dev/null +++ b/Java/Recursion/GCD.java @@ -0,0 +1,16 @@ +public class GCD { + + // Recursive function to find GCD + static int gcd(int a, int b) { + if (b == 0) + return a; + return gcd(b, a % b); + } + + public static void main(String[] args) { + int num1 = 48; + int num2 = 18; + + System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd(num1, num2)); + } +} diff --git a/Java/Recursion/countAndSay.java b/Java/Recursion/countAndSay.java new file mode 100644 index 0000000..131b0fa --- /dev/null +++ b/Java/Recursion/countAndSay.java @@ -0,0 +1,41 @@ +package Recusrion; + +public class countAndSay { + public String __countAndSay__(int n) { + return countAndMerge(n); + } + + private static String countAndMerge(int n) { + if (n == 1) + return "1"; + else if (n == 2) + return "11"; + else if (n == 3) + return "21"; + else if (n == 4) + return "1211"; + else { + StringBuilder answer = new StringBuilder(); + String xyz = (countAndMerge(n - 1)); + int[] helper = new int[10]; + int prev = xyz.charAt(0) - '0'; + for (int i = 0; i < xyz.length(); i++) { + int current = xyz.charAt(i) - '0'; + helper[xyz.charAt(i) - '0']++; + if (prev != current) { + answer.append(helper[prev]); + answer.append(prev); + helper[prev] = 0; + prev = current; + } + if (i == xyz.length() - 1) { + answer.append(helper[xyz.charAt(i) - '0']); + answer.append(xyz.charAt(i)); + } + } + + return answer.toString(); + + } + } +}