C
C#2y ago
Xellez

Wrong count for my inversions function

It is supposed to be 10 inversion count but I am getting 13
var array = new int[] { 1, 8, 2, 0, 4, 3, 7, 6, 5 };

int inv_count = 0;
for (int i = 0; i < array.Length - 1; i++) {
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
inv_count++;
}
}
return inv_count;
}
var array = new int[] { 1, 8, 2, 0, 4, 3, 7, 6, 5 };

int inv_count = 0;
for (int i = 0; i < array.Length - 1; i++) {
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
inv_count++;
}
}
return inv_count;
}
What am I screwing up or missing something ?
3 Replies
TheBoxyBear
TheBoxyBear2y ago
What do you mean by inversion?
Xellez
XellezOP2y ago
it is for a puzzle problem
Now find the number of inversion, by counting tiles precedes the another tile with lower number. In our case, 1,2,3,4,5,6,7 is having 0 inversions, and 8 is having 1 inversion as it's preceding the number 7. Total number of inversion is 1 (odd number) so the puzzle is insolvable.
trying to apply the same method
JimmahDean
JimmahDean2y ago
just counting, i get 12 13 but i'm lacking about four cups of coffee so idk i'm not sure what you're trying to do really 1 precedes 0 : 1 8 precedes 2 0 4 3 7 6 5 : 7 (8) 2 precedes 0 : 1 (9) 0 precedes nothing 4 precedes 3 : 1 (10) 3 precedes nothing 7 precedes 6 5 : 2 (12) 6 precedes 5 : 1 (13)
Want results from more Discord servers?
Add your server