Java > AP-1 > scoresAverage (CodingBat Solution)

Problem:

Given an array of scores, compute the int average of the first half and the second half, and return whichever is larger. We'll say that the second half begins at index length/2. The array length will be at least 2. To practice decomposition, write a separate helper method
int average(int[] scores, int start, int end) { which computes the average of the elements between indexes start..end. Call your helper method twice to implement scoresAverage(). Write your helper method after your scoresAverage() method in the JavaBat text area. Normally you would compute averages with doubles, but here we use ints so the expected results are exact.

scoresAverage({2, 2, 4, 4}) → 4
scoresAverage({4, 4, 4, 2, 2, 2}) → 4
scoresAverage({3, 4, 5, 1, 2, 3}) → 4

Solution:

public int scoresAverage(int[] scores) {
int avg1 = average(scores, 0, scores.length/2);
int avg2 = average(scores, scores.length/2, scores.length);

return Math.max(avg1,avg2);

}

public int average(int[] scores, int start, int end) {
int sum = 0;
for (int i = start; i < end; i++) {
sum += scores[i];
}
return sum / (end-start);
}


9 comments :

  1. public int scoresAverage(int[] scores) {
    int firsthalf = 0;
    int secondhalf = 0;
    int result1;
    int result2;
    int len = scores.length;
    int count = 0;
    int count2 = 0;
    for(int i=0; iresult2) return result1;
    else return result2;
    }

    ReplyDelete
  2. int sum1 = 0;
    int sum2 = 0;
    for (int i = 0; i < scores.length; i++)
    if (i < scores.length/2)
    sum1 += scores[i];
    else sum2 += scores[i];
    return Math.max(sum1/(scores.length/2), sum2/(scores.length/2));

    ReplyDelete
  3. public int scoresAverage(int[] scores) {
    return Math.max(average(scores, 0, scores.length/2), average(scores, scores.length/2, scores.length));
    }

    public int average(int[] scores, int start, int end) {
    if (start == end) {
    return scores[start];
    }
    return Arrays.stream(scores, start, end).sum() / (end-start);
    }

    ReplyDelete
  4. public int scoresAverage(int[] scores) {
    final int avg1 = average(scores, 0, scores.length/2);
    final int avg2 = average(scores, scores.length/2, scores.length);
    return Math.max(avg1,avg2);
    }

    private int average(int[] scores, int start, int end) {
    if (start == end) {
    return scores[start];
    }
    return Arrays.stream(scores, start, end).sum() / (end-start);
    }

    ReplyDelete
  5. public int scoresAverage(int[] scores) {
    return Math.max(average(scores, 0, scores.length/2), average(scores, scores.length/2, scores.length));
    }

    private int average(int[] scores, int start, int end) {
    return (start == end)? scores[start]: Arrays.stream(scores, start, end).sum() / (end-start);
    }

    ReplyDelete
  6. public int scoresAverage(int[] scores) {
    return Math.max(average(scores, 0, scores.length/2), average(scores, scores.length/2, scores.length));
    }

    private int average(int[] scores, int start, int end) {
    return (start == end)? scores[start]: Arrays.stream(scores, start, end).sum() / (end-start);
    }

    ReplyDelete
  7. public int scoresAverage(int[] scores) {
    int first =average(scores,0,scores.length/2);
    int second =average(scores,scores.length/2,scores.length);
    if(first>second) return first;
    return second;
    }
    int average(int[] scores, int start, int end){
    int sum=0;
    for(int i=start;i<end;i++)
    sum+=scores[i];
    return sum/(end-start);
    }

    ReplyDelete
  8. With Java Stream

    public int scoresAverage(int[] scores) {
    int firstAv = (int) Arrays.stream(scores, 0, scores.length/2)
    .average()
    .getAsDouble();
    int secondAv = (int) Arrays.stream(scores, scores.length/2, scores.length)
    .average()
    .getAsDouble();
    return Math.max(firstAv,secondAv);
    }

    ReplyDelete
  9. public int scoresAverage(int[] scores) {
    int mid = scores.length / 2;
    int firstSum = 0;
    int secondSum = 0;
    for(int i = 0; i < mid; i++) {
    firstSum += scores[i];
    }
    int fav = firstSum / mid;
    for(int i = mid; i < scores.length; i++) {
    secondSum += scores[i];
    }
    int sav = secondSum / mid;
    if(fav > sav) return fav;
    else return sav;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact