Average Marks

Easy
0/40
Average time to solve is 5m
148 upvotes
Asked in companies
HikeIBMHyland

Problem statement

You are given the name of a student in the form of a character ‘firstLetterOfName’ and 3 integers ‘M1’, ‘M2’, ‘M3’ representing the marks of the student in 3 subjects. You have to print the ‘firstLetterOfName’ of the student and the average marks obtained by the student.

Note: You need to print the integer part of the average only and neglect the decimal part.

For Example :
If ‘firstLetterOfName’ = ‘K’, ‘M1’ = 10, ‘M2’ = 6 and ‘M3’ = 9, then the average marks would be around 7.67. Hence, the output should be as follows:
K 7
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains a single integer T representing the number of test cases.

The first line of each test case contains a character ‘firstLetterOfName’ denoting the first character of the name of the student.

The second line of each test case contains 3 space-separated integers ‘M1’, ‘M2’, and ‘M3’ representing marks of the student in 3 subjects.
Output Format :
For each test case, print two space-separated values i.e. first character of the name of the student and the average marks of the student.

Output for every test case will be printed in a separate line.
Constraints:
1 <= T <= 10
‘A’ <= ‘firstLetterOfName’ <= ‘Z’
0 <= M1 <= 100
0 <= M2 <= 100
0 <= M3 <= 100

Time Limit: 1 sec
Sample Input 1:
2
A
3 4 6
T
7 3 8
Sample Output 1:
A 4
T 6
Explanation For Sample Input 1:
For sample case 1, average marks of the student are (3 + 4 + 6)/3 = 4.33 and his/her first character of the name is ‘A’. Therefore, the output is : A 4
For sample case 2, average marks of the student are (7 + 3 + 8)/3 = 6 and his/her first character of the name is ‘T’. Therefore, the output is : T 6
Sample Input 2:
2
Q
10 34 96
P
71 80 3
Sample Output 2:
Q 46
P 51
Explanation For Sample Input 2:
For sample case 1, average marks of the student are (10 + 34 + 96)/3 = 46.67 and his/her first character of the name is ‘Q’. Therefore, the output is : Q 46
For sample case 2, average marks of the student are (71 + 80 + 3)/3 = 51.33 and his/her first character of the name is ‘P’. Therefore, the output is : P 51
Hint

What arithmetic operations will you do to find out the average marks of the 3 subjects?

Approaches (1)
Maths

The idea is to simply find out the sum of marks in the 3 subjects and then divide it by the total number of subjects to find the average marks. 

 

Here is the algorithm:

  1. Initialize integer variable ‘TOTAL’ = ‘M1’ + ‘M2’ + ‘M3’.
  2. Initialize integer variable ‘AVERAGE’ = ‘TOTAL’ / 3. (Note that the value of average is automatically rounded off to its floor when stored in an integer variable).
  3. Print ‘firstLetterOfName’ and ‘AVERAGE’ separated by space.
Time Complexity

O(1)

 

Since no loop is used, total time complexity is O(1).

Space Complexity

O(1)

 

Since, we are not using any extra space for solving this problem, effective space complexity is O(1).

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Average Marks
Full screen
Console