Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

centeredAverage challenge

Posted on 2016-08-01
8
Medium Priority
?
222 Views
Last Modified: 2016-08-02
Hi,

I am working on below challenge
http://codingbat.com/prob/p136585
I wrote my code as below

public int centeredAverage(int[] nums) {
   int large=0;
  int small=0;
  for(int n:nums){
    
  large=  Math.max(large, n);
    //large=n;
  }
  
  for(int n:nums){
    
   small= Math.min(large, n);
   // small=n;
  }
  return (large-small);
}

Open in new window


I am failing few tests.

I am not clear on below



Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.

centeredAverage([1, 2, 3, 4, 100]) → 3
centeredAverage([1, 1, 5, 5, 10, 8, 7]) → 5//how it is 5 sum of 1 plus 5 plus 5 plus 8 plus 7 is 26 devided by 5 right?
centeredAverage([-10, -4, -2, -4, -2, 0]) → -3//how it is -3

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 41738738
You shouldn't use two loops to determine large and small. You can do that in one.

Returning large - small is not what is asked for.

You should make the sum of all values, then subtract large from it, then subtract small from it, then divide by the length of the array minus 2.
That's the value to return.
0
 
LVL 53

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 1000 total points
ID: 41738740
HI,

to find the result I would sum the nums subtract the min and the max of it then divide (int) by the number of items of the array -2 (array length)

[-10, -4, -2, -4, -2, 0] becomes (-4 - 2 - 4 - 2)/4 = -12/4 = -3
and
[1, 1, 5, 5, 10, 8, 7] becomes (1+5+5+7+8)/5 but as integers 26(int)/5(int) = 5(int)

Regards
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41738741
@Rgonzo1971
That's exactly what I said in the comment before yours.

@guidii9,

here's the code:

public int centeredAverage(int[] nums) {
  int large=Integer.MIN_VALUE;
  int small=Integer.MAX_VALUE;
  int sum=0;
  for(int n:nums){
    large = Math.max(large, n);
    small = Math.min(small, n);
    sum += n;
  }

  return (sum-large-small)/(nums.length-2);
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:d-glitch
ID: 41739037
gudii9  posts code that suggests he hasn't even read the problem and doesn't attempt to describe his algorithm or approach.

zzynx posts a hint, then waits six minutes and posts the complete solution with no interaction from the author.

This is not how ExEx is supposed to work.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41739078
The questions of gudii9 are no so called "home work" questions. (after *all* those questions we know already)

Previous week I refrained from posting full code. Another "expert" did. (based on the hints I gave)
I filed a request for attention.
That was rejected since the author answered "No" to the question if it was home work.
The poster of the full code got the majority of the points.
Is that how EE is supposed to work?

This time I posted the full code (knowing this is not home work) and I got a attention for request filed against me.
Is that how EE is supposed to work?

I look forward to the decision that will be taken...
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41739159
The behavior gudii9 is the problem, but homework and self-study should be treated the same way.
None of these questions are worth much, and the points we get are not worth the aggravation.
I got a bundle of points from gudii9 last month, and I would give them all back if he would just grow up.

I'm looking forward to some resolution as well.  

And the list of aggravated experts is growing:
   https://www.experts-exchange.com/questions/28960861/sum28-challenge.html#a41738792
0
 
LVL 7

Author Comment

by:gudii9
ID: 41739843
my approach is kind of similar but i missed to subtract large and small from sum

public int centeredAverage(int[] nums) {
  int large=Integer.MIN_VALUE;
  int small=Integer.MAX_VALUE;
  int sum=0;
  for(int n:nums){
    large = Math.max(large, n);
    small = Math.min(small, n);
    sum += n;
  }

  return (sum-large-small)/(nums.length-2);
}

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 41740181
>> my approach is kind of similar
Your approach was this (once again, badly indented) code:
public int centeredAverage(int[] nums) {
   int large=0;
  int small=0;
  for(int n:nums){
    
  large=  Math.max(large, n);
    //large=n;
  }
  
  for(int n:nums){
    
   small= Math.min(large, n);
   // small=n;
  }
  return (large-small);
}

Open in new window

Which is absolutely not similar.

Why do you just repeat my code in your last comment?
Why did Rgonzo1971 got points for just repeating me?

@gudii9, I guess this was my last post on one of your questions...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A solution for Fortify Path Manipulation.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question