GATE Computer ScienceVery hardGATE CS (PYQ pattern)

void perm(char *s, int k) { if (s[k] == 0) { puts(s); return; } for (int i = k; s[i]; i++) { char t = s[k]; s[k] = s[i]; s[i] = t; perm(s, k + 1); t = s[k]; s[k] = s[i]; s[i] = t; } } What is the 10th line printed by perm(s, 0) when s = "abcd" (modifiable array)?

  1. A. bdca
  2. B. bcda
  3. C. bcad
  4. D. cbda

Correct answer: B. bcda

The order generated by this swap-based scheme is abcd abdc acbd acdb adcb adbc bacd badc bcad bcda bdca bdac ... so the 10th line is bcda; it is not lexicographic order.

More Programming and Data Structures (GATE) questions

Exams that ask this topic