Infosys Logo

Asked in Infosys

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
4mo

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!
Select
Add answer anonymously...

Top SDE Interview Questions Asked at Infosys

Q. What is annotation in Spring Boot?
Q. Boundary Traversal of Binary Tree Given a binary tree of integers, your task is ...read more
Q. Bipartite Graph Problem Check whether a given graph is bipartite or not. Return ...read more
SDE Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits