Two linear scans to figure out if its ascending or descending. If one of them works then Ordered else Unordered.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int N = sc.nextInt(); out.println("Lumberjacks:"); for (int i = 0; i < N; i++) { int[] arr = new int[10]; for (int j = 0; j < 10; j++) arr[j] = sc.nextInt(); int bad = 0; for (int j = 1; j < 10; j++) { if (arr[j - 1] > arr[j]) { bad++; break; } } for (int j = 1; j < 10; j++) { if (arr[j - 1] < arr[j]) { bad++; break; } } if (bad == 1) { out.println("Ordered"); } else { out.println("Unordered"); } } out.close(); sc.close(); } }