Mid Aged: The Pan Am 73 flight from Bombay to New York en route Karachi and Frankfurt was hijacked by a few Palestinian terrorists at the Karachi International Airport. The senior flight purser Neerja Banhot withered her fear and helped evacuating the passengers on board.
Neerja very well knew that she would not be able to evacuate all passengers dodging the hijackers. So she wanted to hand over the responsibility of evacuating in the senior citizens(above 60 years of age) and children(below 18 years of age) in the flight to the mid-aged passengers seated in the diagonals.
Given n the number of rows of seats and the number of seats in a row and the ages of passengers in each seat can you find the number of mid-aged passengers seated in the diagonals.
Input Format :
The first line of input consists of an integer n, corresponding to the number of rows of seats and the number of seats in the aircraft.
The next n lines of input consist of n integers that correspond to the ages of passengers.
Output Format :
The output consists of an integer corresponding to the number of mid-aged passengers seated in the diagonals.
Refer sample input and output for further specifications.
Sample Input 1:
3
3
21 3 4
78 25 19
50 23 6
Sample Output 1:
3
Solution
import java.util.*;
public class Main {
public static void main(String args[]) {
// sauravhathi
int i, j, n, c, tot_kids = 0, tot_mid = 0, tot_sen = 0;
int tot_kids_dia = 0, tot_mid_dia = 0, tot_sen_dia = 0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
// sauravhathi
c = sc.nextInt();
int[][] age = new int[n][c];
for (i = 0; i < n; i++) {
for (j = 0; j < c; j++) {
age[i][j] = sc.nextInt();
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < c; j++) {
if (i == j) {
if (age[i][j] < 18) {
tot_kids_dia += 1;
} else if (age[i][j] > 60) {
tot_sen_dia += 1;
} else {
tot_mid_dia += 1;
}
}
//sauravhathi
if (age[i][j] < 18) {
tot_kids += 1;
} else if (age[i][j] > 60) {
tot_sen += 1;
// sauravhathi
} else {
tot_mid += 1;
}
}
}
// sauravhathi
System.out.println(tot_kids);
}
}
Happy Learning – If you require any further information, feel free to contact me.