January 27, 2021
public static int diagonalDifference(List<List<Integer>> arr) {
// Write your code here
int primaryDiagonal = 0;
int secondaryDiagonal = 0;
int size = arr.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == j) {
primaryDiagonal += arr.get(i).get(j);
if (i == size - 1 - j) {
secondaryDiagonal += arr.get(i).get(j);
}
} else if (i == size - 1 - j) {
secondaryDiagonal += arr.get(i).get(j);
}
}
}
return Math.abs(primaryDiagonal - secondaryDiagonal);
}
static String timeConversion(String s) {
String hour = s.substring(0, 2);
String marker = s.substring(s.length() - 2, s.length());
if (marker.equals("PM") && !hour.equals("12")) {
int temp = Integer.parseInt(hour) + 12;
hour = temp + "";
} else if (marker.equals("AM") && hour.equals("12")) {
hour = "00";
}
String answer = hour + s.substring(2, s.length() - 2);
return answer;
}
static String kangaroo(int x1, int v1, int x2, int v2) {
int n = 0;
int biggerGap = v1 > v2 ? v1 : v2;
while (true) {
int l1 = x1 + v1 * n;
int l2 = x2 + v2 * n;
if (l1 == l2) {
return "YES";
}
if (l2 > l1 && v2 >= v1 || l1 > l2 && v1 >= v2) {
return "NO";
}
n++;
}
}
static int saveThePrisoner(int n, int m, int s) {
int answer = m + s - 1;
if (answer > n) {
answer = answer % n;
}
return answer == 0 ? n : answer;
}
Source