Thursday, October 24, 2013

[Microsoft] All subset of a set

Generate all subset of a given set. Example: Given set- {1,2,3}
Output: {}, {1}, {2}, {3},{1,2},{1,3},{2,3},{1,2,3}

1 comment:

  1. #include
    int main()
    {
    int arr[8] = {1, 2, 3};

    int i, j, k =1;

    for(i=0 ; i<8; i++)
    {
    printf("{ ");
    j = i;
    k = 1;
    while(j != 0)
    {
    if(j & 1)
    printf("%d ", k);
    j = j >> 1;
    k = k + 1;
    }
    printf("}\n");
    }
    return 0;
    }

    ReplyDelete