Last active
November 29, 2019 06:20
-
-
Save ghsatpute/55fa133397b9ff6418c106e59c96cc33 to your computer and use it in GitHub Desktop.
New Year Chaos: Hackerrank (Incomplete)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.math.*; | |
import java.security.*; | |
import java.text.*; | |
import java.util.*; | |
import java.util.concurrent.*; | |
import java.util.regex.*; | |
// https://www.hackerrank.com/challenges/new-year-chaos/copy-from/132089930 | |
public class Solution { | |
// Complete the minimumBribes function below. | |
static void minimumBribes(int[] q) { | |
boolean tooChaotic = false; | |
for (int i = 1; i <= q.length; i++) { | |
int diff = q[i - 1] - i; | |
//System.out.println("i " + i + ": " + diff); | |
if (diff >= 3) { | |
tooChaotic = true; | |
break; | |
} | |
} | |
if (tooChaotic) { | |
System.out.println("Too chaotic"); | |
return; | |
} | |
int numBribes = 0; | |
for (int i = 0; i < q.length; i++) { | |
if (q[i] == i + 1) { | |
continue; | |
} | |
if (q[i] < i + 1) { | |
numBribes += (i + 1 - q[i]); | |
} | |
} | |
System.out.println(numBribes); | |
} | |
private static final Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
int t = scanner.nextInt(); | |
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); | |
for (int tItr = 0; tItr < t; tItr++) { | |
int n = scanner.nextInt(); | |
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); | |
int[] q = new int[n]; | |
String[] qItems = scanner.nextLine().split(" "); | |
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); | |
for (int i = 0; i < n; i++) { | |
int qItem = Integer.parseInt(qItems[i]); | |
q[i] = qItem; | |
} | |
minimumBribes(q); | |
} | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment