Java > AP-1 > bigHeights (CodingBat Solution)

Problem:

(A variation on the sumHeights problem.) We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the number of "big" steps for a walk starting at the start index and ending at the end index. We'll say that step is big if it is 5 or more up or down. The start end end index will both be valid indexes into the array with start <= end.

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


Solution:

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


No comments :

Post a Comment

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