Engineering Questions with Answers - Multiple Choice Questions

Home » MCQs » Aeronautical Engineering » Count Inversion Multiple Choice MCQ

# Count Inversion Multiple Choice MCQ

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**

Answer: b

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.

How many inversions does a sorted array have?

a) 0

b) 1

c) 2

d) cannot be determined

**
View Answer**

Answer: a

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.

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**

Answer: d

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.

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**

Answer: b

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.

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**

Answer: a

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.

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

a) 0

b) 3

c) 4

d) 5

**
View Answer**

Answer: d

Explanation: The necessary condition for an inversion is arr[i]>arr[j] and i

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**

Answer: b

Explanation: The necessary condition for an inversion is arr[i]>arr[j] and i

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; }

**
View Answer**

Answer: b

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.

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**

Answer: c

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

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**

Answer: a

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.

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**

Answer: d

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).

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

a) O(n2)

b) O(n)

c) O(log n)

d) O(n log n)

**
View Answer**

Answer: d

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.

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**

Answer: a

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.

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**

Answer: a

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.