You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.ArrayList; import java.util.Collections; 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); ArrayList<Double> list=new ArrayList<Double>(); double sum=0, mean=0,median=0, mode; int n,c,max=0; double v; n=sc.nextInt(); for(int i=0;i<n;i++) { v=sc.nextDouble(); sum+=v; list.add(v); } mean=(double) sum/n; Collections.sort(list); int a=n-1; if(n%2==0) { median=(list.get((a/2))+list.get((a/2)+1))/2; } else { median=(list.get(a/2))/2; } mode=list.get(0); for(int j=0;j<n;j++) { double itr=list.get(j); c=0; for(int k=0;k<n;k++) { if((itr==list.get(k))) { c++; if(c>max) {max=c; if(max==1) { mode=list.get(0); } mode=itr; } } } } System.out.println(mean); System.out.println(median); System.out.println((int)mode); sc.close(); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Day 0: Mean, Median, and Mode
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.ArrayList; import java.util.Collections; public class Solution {