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+ 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+ }
You can’t perform that action at this time.
0 commit comments