Data Structures & AlgorithmsModerateGATE CS · competitive programming
What is the minimum number of swaps needed to transpose a 6×6 matrix in place?
- A. 30
- B. 15
- C. 36
- D. 12
Correct answer: B. 15
Only the pairs with i < j need one swap each, giving 6·5/2 = 15. Swapping all 36 cells would swap every pair twice and cancel out.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For (i = 1; i < n; i = 2) and, inside it, (j = 0; j < i; j++), with n = 1024, how many times does the inner body run in total?
- How many times does the body of for (i = 2; i i ≤ n; i++) run for n = 10⁶?
- For (i = 1; i ≤ n; i = 3) with n = 1000, how many times does the body run?
- Naive recursion fib(n) = fib(n−1) + fib(n−2), with base cases n < 2, makes calls to itself. How many calls in total does fib(10) make…
- rec(n) returns at once if n ≤ 1; otherwise it calls rec(n/2) twice with integer division. How many calls are made for n = 16, counting the…
- A singly linked list has a tail of 4 nodes leading into a cycle of 6 nodes. Floyd's method moves a slow pointer 1 step and a fast pointer…