Asked inInfosys,SDE

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 index i.
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...
Infosys SDE Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter