Tuesday, November 22, 2011

Printing all combinations of given array of Strings


public static void comb(String[] a){
    int n=a.length;
    for(int i=1;i<Math.pow(2, n);i++){
      int pos=0;
      int j=i;
      while(j!=0){
        if((j & 1)==1){
          System.out.print(a[pos]);
        }
        pos++;  
        j=j>>1;  
     }
     System.out.println("");
     }
    }

No comments:

Post a Comment