This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses on “Count Inversion”.

1. What does the number of inversions in an array indicate?

a) mean value of the elements of array

b) measure of how close or far the array is from being sorted

c) the distribution of values in the array

d) median value of the elements of array

View Answer

Explanation: The number of inversions in an array indicates how close or far the array is from being completely sorted. The array is sorted if the number of inversions are 0.

2. How many inversions does a sorted array have?

a) 0

b) 1

c) 2

d) cannot be determined

View Answer

Explanation: When an array is sorted then there cannot be any inversion in the array. As the necessary condition for an inversion is arr[i]>arr[j] and i<j.

3. What is the condition for two elements arr[i] and arr[j] to form an inversion?

a) arr[i]<arr[j]

b) i < j

c) arr[i] < arr[j] and i < j

d) arr[i] > arr[j] and i < j

View Answer

Explanation: For two elements to form an inversion the necessary condition is arr[i] > arr[j] and i < j. The number of inversions in an array indicate how close or far the array is from being completely sorted.

4. Under what condition the number of inversions in an array are maximum?

a) when the array is sorted

b) when the array is reverse sorted

c) when the array is half sorted

d) depends on the given array

View Answer

Explanation: Number of inversions in an array are maximum when the given array is reverse sorted. As the necessary condition for an inversion is arr[i]>arr[j] and i<j.

5. Under what condition the number of inversions in an array are minimum?

a) when the array is sorted

b) when the array is reverse sorted

c) when the array is half sorted

d) depends on the given array

View Answer

Explanation: Number of inversions in an array are minimum when the given array is sorted. As the necessary condition for an inversion is arr[i]>arr[j] and i<j.

6. How many inversions are there in the array arr = {1,5,4,2,3}?

a) 0

b) 3

c) 4

d) 5

View Answer

Explanation: The necessary condition for an inversion is arr[i]>arr[j] and i<j. So there are 5 inversions in the array.

7. Which of the following form inversion in the array arr = {1,5,4,2}?

a) (5,4), (5,2)

b) (5,4), (5,2), (4,2)

c) (1,5), (1,4), (1,2)

d) (1,5)

View Answer

Explanation: The necessary condition for an inversion is arr[i]>arr[j] and i<j. So there are 3 inversions in the array. These are (5,4), (5,2), (4,2).

8. Choose the correct function from the following which determines the number of inversions in an array?

a)

int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i ; j < n; j++) if (arr[i] >= arr[j]) count++; return count; }

b)

int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] > arr[j]) count++; return count; }

c)

int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] > arr[j]) count++; return count + 1; }

d)

int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] < arr[j]) count++; return count + 1; }

Explanation: To determine the number of inversions we apply a nested loop and compare the value of each element with all the elements present after it. Then the count of number of inversions is counted and returned to the main function.

9. What is the time complexity of the following code that determines the number of inversions in an array?

int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] > arr[j]) count++; return count; }

a) O(n)

b) O(n log n)

c) O(n^{2})

d) O(log n)

View Answer

Explanation: The time complexity of the given code is O(n2). It is due to the presence of nested loop.

10. The time complexity of the code that determines the number of inversions in an array using merge sort is lesser than that of the code that uses loops for the same purpose.

a) true

b) false

View Answer

Explanation: The time complexity of the code that determines the number of inversions in an array using merge sort is O(n log n) which is lesser than the time complexity taken by the code that uses loops.

11. What is the time complexity of the code that uses merge sort for determining the number of inversions in an array?

a) O(n^{2})

b) O(n)

c) O(log n)

d) O(n log n)

View Answer

Explanation: The code of merge sort is slightly modified in order to calculate the number of inversions in an array. So the time complexity of merge sort remains unaffected and hence the time complexity is O(n log n).

12. What is the time complexity of the code that uses self balancing BST for determining the number of inversions in an array?

a) O(n^{2})

b) O(n)

c) O(log n)

d) O(n log n)

View Answer

Explanation: When a self balancing BST like an AVL tree is used to calculate the number of inversions in an array then the time complexity is O(n log n) as AVL insert takes O(log n) time.

13. The time complexity of the code that determines the number of inversions in an array using self balancing BST is lesser than that of the code that uses loops for the same purpose.

a) true

b) false

View Answer

Explanation: The time complexity of the code that determines the number of inversions in an array using self balancing BST is O(n log n) which is lesser than the time complexity taken by the code that uses loops.

14. What is the space complexity of the code that uses merge sort for determining the number of inversions in an array?

a) O(n)

b) O(log n)

c) O(1)

d) O(n log n)

View Answer

Explanation: The space complexity required by the code will be O(n). It is the same as the space complexity of the code of standard merge sort.

**Sanfoundry Global Education & Learning Series – Data Structure.**

To practice all areas of Data Structure, __here is complete set of 1000+ Multiple Choice Questions and Answers__.

**Next Steps:**

- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy Data Structure Books
- Practice Computer Science MCQs
- Practice Design & Analysis of Algorithms MCQ
- Apply for Data Structure Internship
- Buy Programming Books