-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnCr.java
More file actions
53 lines (48 loc) · 1.38 KB
/
Copy pathnCr.java
File metadata and controls
53 lines (48 loc) · 1.38 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ncr;
import java.io.*;
import java.util.*;
/**
*
* @author dnandiha
*/
public class Ncr {
public static void generate_combinations(ArrayList a,int n,int r,int cur,int flag,String str){
if(flag==1){
str+=a.get(cur-1)+" ";
}
if(n<r||r==0){
System.out.println(str);
return;
}
if(n==r){
for(int i=cur; i<a.size(); i++){
str+=a.get(i)+" ";
}
System.out.println(str);
return;
}
generate_combinations(a,n-1,r-1,cur+1,1,str);
generate_combinations(a,n-1,r,cur+1,0,str);
}
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of elements");
try{
int num=Integer.parseInt(br.readLine());
ArrayList list=new ArrayList();
for(int i=1; i<=num; i++){
list.add(new Integer(i));
}
System.out.println("Enter value of r:");
int r = Integer.parseInt(br.readLine());
generate_combinations(list,num,r,0,0,"");
}
catch(Exception e){
e.printStackTrace();
}
}
}