First of all your sample have 13 output strings where there should be 16.
[{ "0", "1", "2", "3", "4"},
{ "0", "1", "2", "3&4"},
{ "0", "1", "2&3", "4"},
{ "0", "1", "2&3&4"},
{ "0", "1&2", "3", "4"},
{ "0", "1&2", "3&4"}, missing
{ "0", "1&2&3", "4"},
{ "0", "1&2&3&4"},
{ "0&1", "2", "3", "4"}, missing
{ "0&1", "2", "3&4"},
{ "0&1", "2&3", "4"},
{ "0&1", "2&3&4"}, this one is repeated 2 times in your sample output
{ "0&1&2", "3", "4"}, missing
{ "0&1&2", "3&4"},
{ "0&1&2&3", "4"}, missing
{ "0&1&2&3&4"}]
Quote:
If input is 4, then the output should be
You didn't gave any rule for the 4 missing strings in result and the duplicate.
As programmer, your first job is to make sure requirement and samples make sense and are consistent.
Then you need to make some logic, an algorithm, to solve the problem.
For each string, you get n numbers/digits and n-1 separators, each separator have 2 possibilities. Enumerating all combination of the separators match values of a binary counter.