Scenario or Problem statement:
It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment by 1 from 1 at the front of the line to n at the back.
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if n = 8 and Person 5 bribes Person 4, the queue will look like this: 1,2,3,5,4,6,7,8.
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Function Description
Complete the function minimumBribes in the editor below. It must print an integer representing the minimum number of bribes necessary, or Too chaotic if the line configuration is not possible.
minimumBribes has the following parameter(s):
q: an array of integers
Input Format
The first line contains an integer , the number of test cases.
Each of the next pairs of lines are as follows: - The first line contains an integer , the number of people in the queue - The second line has space-separated integers describing the final state of the queue.
Output Format
Print an integer denoting the minimum number of bribes needed to get the queue into its final state. Print Too chaotic if the state is invalid, i.e. it requires a person to have bribed more than people.
Sample Input
2
8
5 1 2 3 7 8 6 4
8
1 2 5 3 7 8 6 4
Sample Output
Too chaotic
7
I'm basically trying to create a method that accepts the values of the queue in this(final) state and returns the number of bribes needed to get to the final state starting from 1,2,3,4,5,... state, in case the number of bribes per person in the queue is not more than 2 else "Too chaotic".
The code which fails for a few cases using java streams is as below, I want to know why I'm not able to achieve the output with Java Streams?
static void minimumBribes(int[] q) {
AtomicInteger bribeCount = new AtomicInteger(0);
AtomicReference<String> chaoticString = new AtomicReference<String>();
IntStream.rangeClosed(1, q.length).forEach(i -> {
if (q[i - 1] > i) {
if (q[i - 1] - i > 2) {
chaoticString.set("Too chaotic");
} else {
bribeCount.addAndGet(q[i - 1] - i);
}
}
});
if (chaoticString.get() == "Too chaotic")
System.out.print(chaoticString.get());
else
System.out.print(bribeCount.get());
}
The code that passes without using java streams is given below:
static void minimumBribes(int[] q) {
for (int i = 0; i < q.length; i++) {
if (q[i] - (i + 1) > 2) {
System.out.println("Too chaotic");
return;
}
}
int bribe = 0;
for (int i = 0; i < q.length; i++) {
for (int j = i + 1; j < q.length; j++) {
if(q[i] > q[j]) {
q[j] = q[i] + q[j];
q[i] = q[j] - q[i];
q[j] = q[j] - q[i];
bribe++;
}
}
}
System.out.println(bribe);
}
public class MaximumTwoBribesAllowedForMovingForwardInQueue {
//Method that needs to be filled in
static void minimumBribes(int[] q) {
}
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();
}
}
Can you please help recommend changes if any to achieve this with java streams?
Sample Input:
2
8
5 1 2 3 7 8 6 4
8
1 2 5 3 7 8 6 4
Expected Correct Output:
Too chaotic
7
Actual Wrong Output
Too chaotic
6