File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments