Challenge: Out of Order

Lab Java Beginner 10 points

A librarian wants to know whether a shelf of book numbers is in order (each number at least as big as the one before). Print sorted, or break at <i> where i is the first index whose number is smaller than the one before it.

Input

Line 1: n. Line 2: n whole numbers.

Output

sorted or break at i.

Example

Input

5
1 3 3 7 9

Output

sorted
Sample input available Sample output available
Editor
Java

Hints

Compare each item with the one just before it.

Equal neighbours are fine.

Solution explanation

Checking is only O(n) – much cheaper than sorting. Always worth doing before an expensive step.

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            if (a[i] < a[i - 1]) {
                System.out.println("break at " + i);
                return;
            }
        }
        System.out.println("sorted");
    }
}
Output
Answer
HOME LABS COMMUNITY DASHBOARD