An inversion in a list is a pair of positions (i, j) where i comes before j but the value at i is greater than the value at j. Write a method that counts the total number of inversions in a list of integers. Return 0 for a null list or a list with fewer than two elements.

Examples:

Input: [1, 2, 3] Output: 0 Explanation: The list is already sorted, so there are no inversions Input: [3, 2, 1] Output: 3 Explanation: The pairs (3,2), (3,1), and (2,1) are all inversions Input: [2, 1, 3, 1] Output: 3
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay November 2026 (Nov 1–15)Sponsored by FLXBL

Contest runs November 1 - 15. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In