[Solved] Age – 2 with Java

Age – 2: 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.

[Solved] Age - 2 with Java

Neerja planned to evacuated the passengers in rows that have an average age greater than x. Given r the number of rows, c the number of columns, a list of integers representing the ages of passengers, can you find the number of rows with an average age greater than x?

Input Format :
The first line of input consists of an integer r, corresponding to the number of rows of seats in the aircraft.
The second line of input consists of an integer c, corresponding to the number of seats in a row.
The next r lines of input consist of c integers that correspond to the ages of passengers.
The next line of input is an integer x corresponding to the cut-off age.

Output Format :
The output consists of an integer corresponding to t the number of rows with an average age greater than x.
Refer sample input and output for further specifications.

Sample Input 1:
4
5
23 34 45 56 67
98 78 65 78 90
85 76 98 1 2
5 6 7 8 9
25

Sample Output 1:
3

Solution

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // sauravhathi
        Scanner in = new Scanner(System.in);
        int r, c, x, i, j, count = 0, sum = 0, avg;
        r = in.nextInt();
        c = in.nextInt();
        // sauravhathi
        int arr[][] = new int[r][c];
        for (i = 0; i < r; i++)
            // sauravhathi
            for (j = 0; j < c; j++)
                arr[i][j] = in.nextInt();
        x = in.nextInt();
        for (i = 0; i < r; i++) {
            for (j = 0; j < c; j++) {
                sum += arr[i][j];
            }
            avg = sum / c;
            if (avg > x)
                count++;
            sum = 0;
            //sauravhathi
        }
        System.out.println(count);
    }
}

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 *