Youngest and Oldest
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.
Input Format :
The input consists of n+1 lines.
The first line of input consists of an integer n, corresponding to the number of passengers in the aircraft.
The next n lines of input consist of n integers that correspond to the age of the passengers.
Output Format :
The output consists of 2 integers corresponding to the oldest and the youngest.
Print Invalid Input and terminate the process of getting inputs if n or any of the ages is not a non zero positive number.
Sample Input 1:
5
1
3
5
2
4
Sample Output 1:
1 5
Sample Input 2:
6
68
-45
Sample Output 2:
Invalid Input
Sample Input 3:
-6
Sample Output 3:
Invalid Input
Answers
Answered by
3
Answer:
esjsjsjshejennjjhejssj
Answered by
0
The program is as given below:
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of passengers in the aircraft:- ");
int n=sc.nextInt();
int arr[]=new int[n];
int cnt=0;
while(cnt<n){
arr[cnt]=sc.nextInt();
cnt++;
}
Arrays.sort(arr);
System.out.println("Output:- ");
System.out.println(arr[0]);
System.out.println(arr[n-1]);
}
- The above program when run can detect the oldest and the youngest passenger.
- The output is similar to that mentioned in the question.
Similar questions