id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

sspiege3@schools.nyc.gov arrays4.d > countInversions
prev  |  next  |  chance

Given an array of integers named x, write a method named countInversions that returns the number of inversions in the array. An inversion is defined as a pair of elements where the first element is greater than the second element and the first element appears before the second element in the array. The method should return the total count of such inversions.
For example, consider an array with the values {1, 4, 3, 2}. There are 3, inversions since 4 is greater than both 3 and 2 and 3 is greater than 2.


countInversions([1, 4, 3, 2]) → 3
countInversions([1, 2, 4, 3, 5, 6]) → 1
countInversions([1, 2, 3, 4, 5, 6]) → 0

...Save, Compile, Run (ctrl-enter)

public int countInversions(int[] x) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 2.0 Post-solution available

Copyright Nick Parlante 2017 - privacy