Super simple linear scan.
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11764 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for (int i = 1; i <= T; i++) { int N = sc.nextInt(); int high = 0; int low = 0; int last = sc.nextInt(); for (int j = 1; j < N; j++) { int t = sc.nextInt(); if (t < last) { low++; } else if (t > last) { high++; } last = t; } out.println("Case " + i + ": " + high + " " + low); } out.close(); sc.close(); } }