Skip to content

Instantly share code, notes, and snippets.

@tgvdinesh
Created January 14, 2017 04:00
Show Gist options
  • Save tgvdinesh/8e5dfe65608856a11b1f5bd9df076633 to your computer and use it in GitHub Desktop.
Save tgvdinesh/8e5dfe65608856a11b1f5bd9df076633 to your computer and use it in GitHub Desktop.
Count Inversions in an array (Possible inversion 3) - http://www.geeksforgeeks.org/counting-inversions/
public class App {
public static void main(String[] args) {
int[] prices = {5, 8, 6, 1, 4, 5};
int count = 0;
if (prices.length >= 1 && prices.length <= 5000) {
for (int i = 0; i < prices.length; i++) {
if (i <= prices.length - 2) {
for (int j = i + 1; j < prices.length; j++) {
if (j <= prices.length - 1) {
if (prices[i] > prices[j]) {
for (int k = j + 1; k < prices.length; k++) {
if (prices[i] > prices[j] && prices[j] > prices[k]) {
++count;
}
}
}
}
}
}
}
System.out.println(count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment