Skip to content

Commit e9e1977

Browse files
authored
Add files via upload
This is the code to count inversion problem.
1 parent ea4904d commit e9e1977

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

GeeksForGeeks/Inversion_Count.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Scanner;
2+
3+
public class Inversion_Count {
4+
public static int[] input() {
5+
System.out.println("Enter size of array: ");
6+
Scanner sc = new Scanner(System.in);
7+
int n = sc.nextInt();
8+
int a[] = new int[n];
9+
for (int i = 0; i < n; i++) {
10+
a[i] = sc.nextInt();
11+
}
12+
return a;
13+
14+
}
15+
16+
public static void inversecount(int a[]) {
17+
int count=0;
18+
for(int i=0;i<a.length;i++){
19+
for(int j=i+1;j<a.length;j++)
20+
if(a[i]>a[j]){
21+
int temp=a[i];
22+
a[i]=a[j];
23+
a[j]=temp;
24+
count++;
25+
}
26+
}
27+
System.out.println("The Answer is: ");
28+
System.out.println(count);
29+
}
30+
public static void main(String[] args) {
31+
32+
Scanner sc=new Scanner(System.in);
33+
System.out.println("Enter number of test cases you want to perform: ");
34+
int t = sc.nextInt();
35+
for (int p = 0; p < t; p++) {
36+
int a[] = input();
37+
inversecount(a);
38+
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)