forked from shivammaniharsahu/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Inversion in array.txt
More file actions
42 lines (36 loc) · 1.03 KB
/
Count Inversion in array.txt
File metadata and controls
42 lines (36 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static int solve(int[] arr,int n){
int c=0;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
c++;
}
return c;
}
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0){
int n = Integer.parseInt(br.readLine());
int a[] = new int[n];
String line = br.readLine();
int i = 0;
for (String numStr: line.split("\\s")){
a[i] = Integer.parseInt(numStr);
i++;
}
int ans = solve(a,n);
System.out.println(ans);
}
}
}