File tree 1 file changed +56
-0
lines changed
src/main/java/com/hackerrank/interviewpreparation/arrays
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .hackerrank .interviewpreparation .arrays ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ /**
6
+ * @author rpatra16
7
+ * @since 02/11/2018
8
+ */
9
+ public class NewYearChaos {
10
+
11
+ /**
12
+ * To solve this question, we just need to count the number of persons
13
+ * that overtake a particular person.
14
+ *
15
+ * @param q the queue
16
+ */
17
+ private static void minimumBribes (int [] q ) {
18
+ int bribes = 0 ;
19
+ for (int i = q .length - 1 ; i >= 0 ; i --) {
20
+ if (q [i ] - i - 1 > 2 ) {
21
+ System .out .println ("Too chaotic" );
22
+ return ;
23
+ }
24
+ for (int j = Math .max (0 , q [i ] - 2 ); j < i ; j ++) {
25
+ if (q [j ] > q [i ]) bribes ++;
26
+ }
27
+ }
28
+ System .out .println (bribes );
29
+ }
30
+
31
+ private static final Scanner scanner = new Scanner (System .in );
32
+
33
+ public static void main (String [] args ) {
34
+ int t = scanner .nextInt ();
35
+ scanner .skip ("(\r \n |[\n \r \u2028 \u2029 \u0085 ])?" );
36
+
37
+ for (int tItr = 0 ; tItr < t ; tItr ++) {
38
+ int n = scanner .nextInt ();
39
+ scanner .skip ("(\r \n |[\n \r \u2028 \u2029 \u0085 ])?" );
40
+
41
+ int [] q = new int [n ];
42
+
43
+ String [] qItems = scanner .nextLine ().split (" " );
44
+ scanner .skip ("(\r \n |[\n \r \u2028 \u2029 \u0085 ])?" );
45
+
46
+ for (int i = 0 ; i < n ; i ++) {
47
+ int qItem = Integer .parseInt (qItems [i ]);
48
+ q [i ] = qItem ;
49
+ }
50
+
51
+ minimumBribes (q );
52
+ }
53
+
54
+ scanner .close ();
55
+ }
56
+ }
You can’t perform that action at this time.
0 commit comments