We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.Scanner;importjava.util.Arrays;importjava.util.HashMap;// Time Complexity: O(n log n) due to sortingpublicclassSolution{publicstaticvoidmain(String[]args){/* Save input */Scannerscan=newScanner(System.in);intsize=scan.nextInt();int[]array=newint[size];for(inti=0;i<size;i++){array[i]=scan.nextInt();}scan.close();/* Sort array: O(n log n) runtime */Arrays.sort(array);/* Calculate Mean */inttotal=0;for(intnum:array){total+=num;}doublemean=(double)total/size;/* Calculate Median */doublemedian;if(size%2==0){median=(array[size/2-1]+array[size/2])/2.0;}else{median=array[size/2];}/* Calculate Mode - if there's a tie, choose the smaller number */HashMap<Integer,Integer>map=newHashMap<>();intmaxOccurrences=0;intmode=Integer.MAX_VALUE;for(intnum:array){map.merge(num,1,Integer::sum);intoccurrences=map.get(num);if(occurrences>maxOccurrences||(occurrences==maxOccurrences&&num<mode)){maxOccurrences=occurrences;mode=num;}}/* Print results */System.out.println(mean);System.out.println(median);System.out.println(mode);}}
Hi. Try posting your code in a new thread to reach a wider audience and for us to have the ability to see what's wrong. You can tag me in the post and I will take a look also.
// check mode, not necessary to use HashMap
// or double for loops to calculate counts for each item;
// assuming the first item would be the mode
int mode = arr[0];
int max = 1;
int appearance = 1;
for(int i=1; i<arr.length; i++){
if ( arr[i] == arr[i-1] ) {
appearance ++;
} else {
appearance = 1;
}
if( appearance > max ){
max = appearance;
mode = arr[i];
}
}
Yeah, I took advantage of the fact that I sorted the array to find the median.
`
import java.util.Arrays;
Arrays.sort(X); // X is our array of integers
int mode = -1;
int last = -1;
int maxFreq = 0;
int freq = 1;
for (int x : X) {
if (x == last) {
freq++;
} else {
freq = 1;
}
// because numbers are sorted I only have to worry about strictly greater than.
if (freq > maxFreq) {
maxFreq = freq;
mode = x;
}
last = x;
}
Bro @rshaghoulian my Java solution does not run Test case 3 rest its working fine. As test data set is too huge i am not able to find the problem.
Can you please review my code and tell me the possible error?
Thanks in advance !
import java.io.;
import java.util.;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
sc.close();
Arrays.sort(arr);
//mean
float sum=0;
for(int i=0;i<n;i++){
sum = sum + arr[i];
}
System.out.printf("%.1f",sum/n); System.out.println();
//median
int l=arr.length;
if(l%2==1) {
float med=arr[l/2];
System.out.printf("%.1f",med);
}else {
int a=l/2;
int b=a-1;
double medd = (double)(arr[a]+arr[b])/2;
System.out.printf("%.1f",medd);
}
System.out.println();
//mode
int mode=arr[0],max=0,count=0;
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0;i<l-1;i++) {
if(arr[i]==arr[i+1]) {
count++;
map.put(arr[i], count);
}
else {
count=0;
}
}
if(map.isEmpty())System.out.println(mode);
else{
for (int i : map.keySet()) {
if(map.get(i)>max) {
max=map.get(i);
mode=i;
}
}
System.out.println(mode);
}
}
Day 0: Mean, Median, and Mode
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank Java solutions
i m unable to pass test case 3, can anyone help me?
Hi. Try posting your code in a new thread to reach a wider audience and for us to have the ability to see what's wrong. HackerRank solutions.
my code is also not able to pass test case 3,please help
Hi. Try posting your code in a new thread to reach a wider audience and for us to have the ability to see what's wrong. You can tag me in the post and I will take a look also.
HackerRank solutions.
hi buddy here is the one of the easy code for you:
import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) {
//////////////mode code ends here//////////////////
}
}
what is the use of map.merge(num,1,Integer::sum); can you help me please
That's a function that's part of HashMap, as shown here. If num does not exist as a key, it puts num as key, and 1 as a value into the HashMap.
If on the other hand num already exists, then it just increments the value of num by 1.
HackerRank solutions.
other than hashmap there isnt any other code for mode calculation?
If the numbers are within a certain range, like 1 to 10, you can use a 10-element array.
HackerRank solutions.
Yeah, I took advantage of the fact that I sorted the array to find the median.
` import java.util.Arrays;
Arrays.sort(X); // X is our array of integers
Bro @rshaghoulian my Java solution does not run Test case 3 rest its working fine. As test data set is too huge i am not able to find the problem.
Can you please review my code and tell me the possible error? Thanks in advance !
import java.io.; import java.util.;
public class Solution {
}
Thank YOU! I couldnt get the third test with mode to work till i used your hashmap! :) you rock