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)?
- A. bdca
- B. bcda
- C. bcad
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- void h(int n, char a, char b, char c) { if (n == 0) return; h(n - 1, a, c, b); printf("move disk %dn", n); h(n - 1, c, b, a); } During…
- Bubble sort with early exit (a pass that makes no swap ends the sort, and every pass scans the whole array from index 0 to n-2) is run on…
- char s[] = "GATECS"; for (int i = 1; s[i]; i++) s[i] = 'A' + (s[i] + s[i - 1] - 2 'A') % 26; printf("%s", s); What is printed?
- unsigned int i, s = 0; for (i = 5; i-- > 0; ) s += i; printf("%u %u", s, i); What is printed?
- int a = 7, b = 2; float f = a / b + a / 2.0 + (float)(a / b) b; printf("%.2f %d", f, (int)(a / b 2.0)); What is printed?
- int a = 5, b = 3, c = 2; int x = a + b << c > 20; int y = 6 & 3 == 3; int z = 1 + 2 3 % 4 - 5 / 2 | 8 >> 2; printf("%d %d %d", x, y, z)…