Q1. 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 had to wither her fear and start evacuating the passengers on board. She pleaded the hijackers to release the oldest and the youngest person in the aircraft. Heeding to her plea the chief of the hijacker agreed to let go the oldest and the youngest. Given the ages of the passengers find the oldest and the youngest.
Code Constraints :
The first line of input is an integer that is corresponding to the number of passengers in the flight.
The next line of inputs is the age of the passengers
The next line of inputs are corresponding gender(f/m) of the passengers
Input Format :
4
45 67 23 78
f m m f
Output Format :
Largest: 67
Smallest: 45
Solution :
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static int getLargest(int[] a, int total){
Arrays.sort(a);
return a[total-1];
}
public static int getSmallest(int[] a, int total){
int x = 0;
Arrays.sort(a);
for (int i : a) {
if(i > 0){
x = i;
break;
}
}
return x;
}
char f,m;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numberOfPass = input.nextInt();
int[] array = new int[numberOfPass + 1];
for(int i=0; i<numberOfPass; i++){
array[i] = input.nextInt();
}
char[] array2 = new char[numberOfPass + 1];
for(int j = 0; j<numberOfPass; j++){
array2[j] = input.next().charAt(0);
}
//Sub arrays
int[] array3 = new int[numberOfPass + 1];
int[] array4 = new int[numberOfPass + 1];
//Storing in subarrays
// To get the age of the oldest male
for(int k=0; k<numberOfPass; k++){
if(array2[k] == 'm' ){
array3[k] = array[k];
}
}
// To get the age of the youngest female
for(int l=0; l<numberOfPass; l++){
if(array2[l] == 'f' ){
array4[l] = array[l];
}
}
System.out.println("Largest: "+getLargest(array3,numberOfPass + 1));
System.out.println("Smallest: "+ getSmallest(array4,numberOfPass + 1));
}
}
|
Output of the Code |
Comments
Post a Comment
Write your opinion about the above content.
You can also comment here, if you find any error in the data