Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Java/Recursion/GCD.class
Binary file not shown.
16 changes: 16 additions & 0 deletions Java/Recursion/GCD.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
41 changes: 41 additions & 0 deletions Java/Recursion/countAndSay.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
}