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.
yo are right indeed..! solving with created functions gives no good..my java code is really big but understandable..
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
double mean;
int[]arr=new int[a];
for(int i=0;i
}
int sum=0;
//mean
for(int i=0;i<a;i++)
{
sum=sum+arr[i];
}
mean=(double)sum/a;
//median
Arrays.sort(arr);
double median;
int A=arr.length;
if(A%2==1)
{
median=arr[(A-1)/2];
}
else{
int c,d;
c=arr[(A-1)/2];
d=arr[((A-1)/2)+1];
median=(double)(c+d)/2;
}
//mode
int mode=0; int itr; int count; int max=0;
for(int i=0;i<a;i++)
{
itr=arr[i];
count=0;
for(int c=0;c<a;c++)
{
if(itr==arr[c])
{count++;}
if(count>max)
{ max=count;
if(max==1)
{
mode=arr[0];
}
mode=itr;
}
}
}
System.out.println(mean);
System.out.println(median);
System.out.println(mode);
}
Cookie support is required to access HackerRank
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 →
yo are right indeed..! solving with created functions gives no good..my java code is really big but understandable.. { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); double mean; int[]arr=new int[a]; for(int i=0;i