1

I am trying to work out the solution to the above problem and I came up with this

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


public class Subset_K {
public static void main(String[]args)
{
Set<String> x;
int n=4;
int k=2;
int arr[]={1,2,3,4};
StringBuilder sb=new StringBuilder();
for(int i=1;i<=(n-k);i++)
    sb.append("0");
for(int i=1;i<=k;i++)
    sb.append("1");
String bin=sb.toString();
x=generatePerm(bin);
Set<ArrayList <Integer>> outer=new HashSet<ArrayList <Integer>>();
for(String s:x){
    int dec=Integer.parseInt(s,2);
    ArrayList<Integer> inner=new ArrayList<Integer>();
    for(int j=0;j<n;j++){
        if((dec&(1<<j))>0)
            inner.add(arr[j]);
    }
    outer.add(inner);
}
for(ArrayList<Integer> z:outer){
    System.out.println(z);
}
}

public static Set<String> generatePerm(String input)
{
Set<String> set = new HashSet<String>();
if (input == "")
    return set;

Character a = input.charAt(0);

if (input.length() > 1)
{
    input = input.substring(1);

    Set<String> permSet = generatePerm(input);

    for (String x : permSet)
    {
        for (int i = 0; i <= x.length(); i++)
        {
            set.add(x.substring(0, i) + a + x.substring(i));
        }
    }
}
else
{
    set.add(a + "");
}
return set;
}
}

I am working on a 4 element set for test purpose and using k=2. What I try to do is initially generate a binary string where k bits are set and n-k bits are not set. Now using this string I find all the possible permutations of this string. And then using these permutations I output the respective element in the set. Now i cant figure out the complexity of this code because I used the generatePerm method from someone else. Can someone help me with the time complexity of the generatePerm method and also the overall time complexity of my solution. I found other recursive implementation of this problem in here Find all subsets of length k in an array However I cant figure out the complexity of it either. So need some help there.

Also I was trying to re-factor my code so that its not just for integers but for all types of data. I dont have much experience with generics. so when I try to modify ArrayList< Integer> to ArrayList< ?> in line 21 eclipse says

Cannot instantiate the type ArrayList< ?> How do I correct that?

Community
  • 1
  • 1
Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
  • Well think about it. There are C(n,k) ways to take a subset of size k out of a set of n elements. If the generatePerm function is reasonable then the time complexity of the algorithm will be equal to C(n,k). See https://en.wikipedia.org/wiki/Binomial_coefficient for the definition of the C function. – Rag Dec 24 '13 at 21:23
  • Thanks for the above. Can you tell me the complexity of the recursive implementation given here http://stackoverflow.com/questions/12548312/find-all-subsets-of-length-k-in-an-array and is C(n,k) the best possible complexity? – Abhiroop Sarkar Dec 24 '13 at 22:00
  • Can someone tell me the space complexity of this solution? – Abhiroop Sarkar Dec 25 '13 at 19:19

1 Answers1

3

You can use ArrayList<Object> throughout. That will accept any kind of object. If you want a specific type that is determined by the calling code, you will need to introduce a generic type parameter.

Note that in your generatePerm method, you should not use the test

if (input == "")

Instead, you should use:

if ("".equals(input))

Your current code will only succeed if input is the interned string "". It will not work, for instance, if input is computed as a substring() with zero length. In general you should always compare strings with .equals() rather than with == (except under very specific conditions when you are looking for object identity rather than object equality).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks for the above. Can you please elaborate on why the error occurs with ArrayList> instantiation? – Abhiroop Sarkar Dec 24 '13 at 22:07
  • 1
    @Sarkar - With generics, the wildcard means "some specific but currently unknown type". When you create an `ArrayList` you need to specify a specific _known_ (or at least parameterized) type. For more information, see the topic [Wildcards](http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html) in the Java tutorial on generics. – Ted Hopp Dec 24 '13 at 22:18