Skip to content

Commit beb9029

Browse files
Merge pull request #186 from RAshid602/master
inversions count
2 parents 6664a30 + 1c67d0a commit beb9029

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

GeeksForGeeks/Inversion_Count.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Given an array of integers. Find the Inversion Count in the array.
2+
3+
4+
import java.util.Scanner;
5+
6+
7+
public class Inversion_Count {
8+
public static int[] input() { //This function is sued to take input from the user
9+
System.out.println("Enter size of array: ");
10+
Scanner sc = new Scanner(System.in);
11+
int n = sc.nextInt();
12+
int a[] = new int[n];
13+
for (int i = 0; i < n; i++) { //This forloop will help to store array
14+
a[i] = sc.nextInt();
15+
}
16+
return a;
17+
18+
}
19+
//This is the main logic of the solution
20+
//The first loop will run till the length of array starting from 0
21+
//Second loop will run till length f array starting from (i+1);
22+
public static void inversecount(int a[]) {
23+
int count=0;
24+
for(int i=0;i<a.length;i++){
25+
for(int j=i+1;j<a.length;j++)
26+
if(a[i]>a[j]){
27+
int temp=a[i];
28+
a[i]=a[j];
29+
a[j]=temp;
30+
count++;
31+
}
32+
}
33+
System.out.println("The Answer is: ");
34+
System.out.println(count);//The final answer is printed using this line
35+
}
36+
public static void main(String[] args) {
37+
38+
Scanner sc=new Scanner(System.in);
39+
System.out.println("Enter number of test cases you want to perform: ");
40+
int t = sc.nextInt();//"t " stores the number of test cases you want to perform
41+
for (int p = 0; p < t; p++) {
42+
int a[] = input();
43+
inversecount(a);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)