[Solved] Youngest and Oldest with Java, C++

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.

[Solved] Youngest and Oldest with Java, C++
[Solved] Youngest and Oldest with Java, C++

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

Solution

import java.util.*;

public class Main

{
    public static void main(String[] args) {
        // sauravhathi
        Scanner sc = new Scanner(System.in);

        try {
            int n = sc.nextInt();

            int arr[] = new int[n];

            int cnt = 0;
            // sauravhathi
            while (cnt < n) {

                arr[cnt] = sc.nextInt();

                cnt++;
                // sauravhathi
            }
            Arrays.sort(arr);
            // sauravhathi

            System.out.print(arr[0]);

            System.out.print(arr[n - 1]);
        } catch (Exception e) {
            System.out.println("Invalid Input");
        }
    }
}
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

int main()
{
    try
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        int sum=0;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]>=0)
            {
                sum=sum+a[i];
            }
            else
            {
                flag=-1;
                break;
            }
        }
        if(flag==-1)
        {
            cout<<"Invalid Input";
        }
        else
        {
            for(int i=0;i<n;i++)
            {
                cout<<a[i]<<" ";
            }
            cout<<"\n"<<sum;
        }
    }
    catch(...)
    {
        cout<<"Invalid Input";
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *