Mean, Median, Mode Calculation

You are given an array 'ARR' consisting of 'N' integers. Your task is to calculate the three statistical measures for the given array:

  1. Mean - Implement the function mean() to calculate the mean of the array.
  2. Median - Implement the function median() to calculate the median of the array.
  3. Mode - Implement the function mode() to find the mode of the array.

Explanation:

Mean and Median should be calculated in the form of P/Q, where P and Q are coprime integers, with Q ≠ 0.

For Mode, if there are multiple elements with the same highest frequency, return the smallest element.

For example, given the array {1, 1, 2, 2, 3, 3, 4}, the mode would be 1, as it is the smallest of the possible modes 1, 2, and 3.

Input:

The first line of input contains an integer T, denoting the number of test cases.
The first line of each test case contains an integer N, the size of the array.
The second line of each test case contains N space-separated integers representing elements of the array.

Output:

For each test case, output three lines:
1. First line: Two space-separated integers P and Q for the Mean of the array.
2. Second line: Two space-separated integers P and Q for the Median of the array.
3. Third line: An integer representing the Mode of the array.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 105
  • 1 <= ARR[i] <= 106 where ARR[i] denotes the i-th element of the array.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given functions.
AnswerBot
4mo

Implement functions to calculate mean, median, and mode of an array of integers.

  • Implement a function mean() to calculate the mean of the array by summing all elements and dividing by the total count.

  • I...read more

Help your peers!
Select
Add answer anonymously...

Microsoft Corporation Software Developer Intern interview questions & answers

A Software Developer Intern was asked 7mo agoQ. What were your responsibilities during your internship?
A Software Developer Intern was asked 7mo agoQ. What is the difference between polymorphism and inheritance?
A Software Developer Intern was asked 9mo agoQ. Given a string, reverse the order of characters using standard data structures a...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 6mo agoQ1. What were your responsibilities during your internship?
A Software Developer Intern was asked 6mo agoQ2. What is the difference between polymorphism and inheritance?
A Software Developer Intern was asked 8mo agoQ3. Given a string, reverse the order of characters using standard data structures a...read more
Microsoft Corporation Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits