Imran Mohammed Abdul Muqsith wrote:
> Hi,
>
> Can anyone give the algo to generate all possible interleavings of two
> arrays (better if its in C or Java)
>
> E.g.
> A1[] = {a,b};
> B1[] = {c,d};
>
> # Results = (2+2)!/(2! .2!) = 6
>
> abcd
> acbd
> acdb
> cabd
> cadb
> cdab
#include <stdio.h>
void interleave(char *a, int ia, char *b, int ib, char *r, int ir)
{
if (ir < 0)
printf("%s\n", r);
else {
if (ia >= 0) {
r[ir] = a[ia];
interleave(a, ia - 1, b, ib, r, ir - 1);
}
if (ib >= 0) {
r[ir] = b[ib];
interleave(a, ia, b, ib - 1, r, ir - 1);
}
}
}
int main(void)
{
char r[1000];
char a[] = "abcde";
char b[] = "ABCD";
int na = sizeof a - 1;
int nb = sizeof b - 1;
int nr = na + nb;
r[nr] = '\0';
interleave(a, na - 1, b, nb - 1, r, nr - 1);
return 0;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---