Count Inversions Problem Statement
Given an integer array ARR
of size N
containing all distinct values, determine the total number of inversions present in the array.
An inversion is defined for a pair of integers in the array when the following two conditions are met:
ARR[i] > ARR[j]
i < j
Here, i
and j
are the indices ranging from [0, N)
.
Input:
N
ARR[0], ARR[1], ..., ARR[N-1]
Output:
Single integer indicating the total count of inversions in the array.
Example:
Input:
5
2 3 8 6 1
Output:
5
Explanation:
The inversions are: (2, 1), (3, 1), (8, 6), (8, 1), (6, 1).
Constraints:
1 ≤ N ≤ 105
1 ≤ ARR[i] ≤ 109
ARR[i]
denotes the array element at indexi
.
Note:
You are not required to print anything; it has already been taken care of. Just implement the given function.

AnswerBot
8d

Count the total number of inversions in an integer array.
Iterate through the array and for each pair of elements, check if the conditions for inversion are met.
Use a nested loop to compare each elemen...read more

Help your peers!
Add answer anonymously...
Top Infosys SDE interview questions & answers
Popular interview questions of SDE
Top HR questions asked in Infosys SDE
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app