-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNCRProblem.java
More file actions
34 lines (26 loc) · 821 Bytes
/
Copy pathNCRProblem.java
File metadata and controls
34 lines (26 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package recursion;
/*
Formula:
nCr = (n-1) C (r-1) + (n-1) C (r)
*/
public class NCRProblem {
static int nCr(int n, int r) {
if (n == r || r == 0) return 1;
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
static int josephusProblem(int n, int k) {
if (n == 1) return 0; // if there is only one player then that the only player, so he is the winner
return (josephusProblem(n - 1, k) + k) % n;
}
static boolean isPalindrome(String str, int l, int r ) {
if( l >= r) return true;
if(str.charAt(l) == str.charAt(r)) {
return isPalindrome(str, l+1, r-1);
}
return false;
}
public static void main(String[] args) {
String str = "racr";
System.out.println(isPalindrome(str, 0, str.length()-1));
}
}