Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gokul.demo.InProgress;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class trouble {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- // Read the number of test cases
- int testCases = Integer.parseInt(reader.readLine());
- List<int[]> arr = new ArrayList<>();
- for (int i = 0; i < testCases; i++) {
- // Read the number of values in the list
- int n = Integer.parseInt(reader.readLine());
- // Read the list of values
- String[] line = reader.readLine().split(" ");
- int[] values = new int[n];
- for (int j = 0; j < n; j++) {
- values[i] = (Integer.parseInt(line[j]));
- }
- arr.add(values);
- }
- int c = 1;
- for (int[] values : arr) {
- int firstSortingErrorIndex = checkTroubleSort(values, values.length);
- // Output the result
- System.out.println("Case #" + c + ": " + (firstSortingErrorIndex == -1 ? "OK" : firstSortingErrorIndex));
- c++;
- }
- }
- public static int checkTroubleSort(int a[], int n) {
- Boolean done = false;
- while (!done) {
- done = true;
- for (int i = 0; i < a.length - 2; i++) {
- if (a[i] > a[i + 2]) {
- int temp = a[i];
- a[i] = a[i + 2];
- a[i + 2] = temp;
- done = false;
- }
- }
- }
- for (int i = 0; i < a.length - 1; i++) {
- if (a[i] > a[i + 1]) {
- return i;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement