Mean median mode of unsorted array

You are given an array 'ARR' of 'N' integers and you have to calculate 3 things for the given array:-

1. Mean - function mean(): This function should calculate the mean of the array.

2. Median - function median(): This function should calculate the median of the array.

3. Mode - function mode(): This function should calculate the mode of the array.

Note :
It can be shown that Mean and Median is in the form of P/Q, where P and Q are coprime integers and Q != 0. You need to return P and Q.

For Mode, if the highest frequency of more than one element is the same, return the smallest element.

For Example, for the given array {1, 1, 2, 2, 3, 3, 4}, the mode will be 1 as it is the smallest of all the possible modes i.e 1, 2 and 3.
Input format :
The first line of input contains an integer T denoting the number of queries or test cases. 

The first line of every test case contains an integer N denoting the size of the input array.

The second line of every test case contains N single space-separated integers representing the elements of the input array. 
Output format :
For each test case, 
The first line of output will contain 2 single space-separated integers representing P, and Q for the Mean of the array.

The second line of output will contain 2 single space-separated integers representing P, and Q for the Median of the array.

The third line of the output will contain an integer representing the Mode of the array.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given functions.
Constraints :
1 <= T <= 5
1 <= N <= 10^5 
1 <= ARR[i] <= 10^6 

Where 'ARR[i]' denotes the 'ith' element of the array.

Time limit: 1 sec
CodingNinjas
author
2y

The mean was easy to compute calculating the sum of the entire array, then I sorted the array for median and checked the size of the array. If the size was odd I returned the middle element else I ret...read more

CodingNinjas
author
2y
Brute force

For the mean and median of the array, we will first calculate both the results in N / D form where N denotes the numerator of the result and D denotes the denominator. Then for the P/ Q fo...read more

Help your peers!
Add answer anonymously...
Microsoft Corporation Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter