// 11 to 9 becomes 11 to 33 so the difference is 22 hours take the remainder with 24 still 22 // 9 to 11 becomes 9 to 35 so the difference becomes 26 take the remainder with 24 back to 2 // row = (row - 2 + size) % size; // col = (col - 1 + size) % size; class One { public static void main(String[] args) { System.out.println(diff(11, 31, 17, 4)); // 5 hours and 33 mins System.out.println(diff(17, 4, 11, 31)); // 18 hours and 27 mins } static String diff(int h1, int m1, int h2, int m2) { int d1 = h1 * 60 + m1; int d2 = h2 * 60 + m2; int magic = (d2 - d1 + 24 * 60) % (24 * 60); return magic / 60 + " hours and " + magic % 60 + " minutes" ; } }