Count Array Inversions
Updated
Problem statement
Given an integer array nums, return the number of inversions in it.
An inversion is a pair of indices (i, j) such that i < j and nums[i] > nums[j]. Equal values do not form an inversion.
Function
countInversions(nums: int[]) → longExamples
Example 1
nums = [2,4,1,3,5]return = 3The inverted pairs are (2,1), (4,1), and (4,3).
Example 2
nums = [5,4,3,2,1]return = 10Every pair is inverted in a length-five descending array.
Constraints
1 <= nums.length <= 200000-2^31 <= nums[i] <= 2^31 - 1- The answer fits in a signed 64-bit integer.
More Tata Consultancy Services Online Assessment questions
Similar questions at other companies
- API Rate Limit Violation DetectionTekion · OATekion interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Largest Team Where Every Pair OverlapsWalmart · OAWalmart interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA