ProgrammingVery hardProduct company OA
After the call below, what is c?
After the call below, what is c?
int c = 0;
void perm(char *s, int k, int n) {
if (k == n) { c++; return; }
for (int i = k; i < n; i++) {
if (i != k && s[i] == s[k]) continue;
char t = s[k]; s[k] = s[i]; s[i] = t;
perm(s, k + 1, n);
t = s[k]; s[k] = s[i]; s[i] = t;
}
}
/* in main: */ char s[] = "AAB"; perm(s, 0, 3);- A. 6
- B. 2
- C. 3
- D. 4
Correct answer: C. 3
Without the skip line this would generate all 3! = 6 orderings. The skip refuses to swap a character into position k if it equals the one already there, so the duplicate A–A swap at the top level is skipped: the branches are AAB → {AAB, ABA} and BAA → {BAA}. That is 3, the number of distinct arrangements of AAB.
More C Programming questions
- What is printed? c int steps = 0; int gcd(int a, int b) { steps++; return b == 0 ? a : gcd(b, a % b); } int main(void) { int g = gcd(1071…
- What is printed? c unsigned v = 0; const char b = "10110"; while (b) v = (v << 1) | (b++ - '0'); printf("%u", v);
- With 4-byte int, what is printed? c int a[2][3][4]; printf("%zu %zu %td", sizeof a[1], sizeof a[1][2], (char )&a[1][2][3] - (char )a);
- Assume ASCII. What is printed? c int my(const char a, const char b) { while (a && a == b) { a++; b++; } return a - b; } int main(void) {…
- In what order are the nodes linked after the four calls? c struct N { int v; struct N nx; }; void ins(struct N h, struct N n) { while (h…
- A function in a.c is declared static int helper(void) { ... }. Which statement is true?