Java > AP-1 > sumHeights (CodingBat Solution)

Problem:

We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the sum of the changes for a walk beginning at the start index and ending at the end index. For example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1 + 5 = 6. The start end end index will both be valid indexes into the array with start <= end.

sumHeights({5, 3, 6, 7, 2}, 2, 4) → 6
sumHeights({5, 3, 6, 7, 2}, 0, 1) → 2
sumHeights({5, 3, 6, 7, 2}, 0, 4) → 11


Solution:

public int sumHeights(int[] heights, int start, int end) {
  int tmp = 0;
  for (int i = start; i <= end-1; i++) {
    tmp += Math.abs(heights[i] - heights[i+1]);
  }
  return tmp;
}

2 comments:

  1. public int sumHeights(int[] heights, int start, int end) {

    int sum=0;

    for(int i=start;i<end;i++){

    int x=Math.abs(heights[i]-heights[i+1]);
    sum=sum+x;


    }
    return sum;


    }

    ReplyDelete
  2. Recursive way to look at it.

    public int sumHeights(int[] heights, int start, int end) {

    if (start >= end) return 0;
    return Math.abs(heights[start] - heights[start+1]) + sumHeights(heights, start+1, end);

    }

    ReplyDelete