Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi,

i am working one below challenge

http://codingbat.com/prob/p197710

i was not clear on below description

(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.

how below results are 1,0,1 each? please advise

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

i am working one below challenge

http://codingbat.com/prob/p197710

i was not clear on below description

(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.

how below results are 1,0,1 each? please advise

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

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Expected Run

bigHeights([5, 3, 6, 7, 2], 2, 4) → 1 0 X

bigHeights([5, 3, 6, 7, 2], 0, 1) → 0 0 OK

bigHeights([5, 3, 6, 7, 2], 0, 4) → 1 0 X

bigHeights([5, 3, 6, 7, 3], 0, 4) → 0 0 OK

bigHeights([5, 3, 6, 7, 2], 1, 1) → 0 0 OK

bigHeights([5, 13, 6, 7, 2], 1, 2) → 1 0 X

bigHeights([5, 13, 6, 7, 2], 0, 2) → 2 0 X

bigHeights([5, 13, 6, 7, 2], 1, 4) → 2 0 X

bigHeights([5, 13, 6, 7, 2], 0, 4) → 3 0 X

bigHeights([5, 13, 6, 7, 2], 0, 3) → 2 0 X

bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 0, 3) → 0 0 OK

bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 4, 8) → 1 0 X

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 0, 3) → 1 0 X

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 7, 8) → 1 0 X

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 3, 8) → 2 0 X

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 2, 8) → 3 0 X

other tests

X

not clear still yet.

bigHeights([5, 3,

bigHeights([

bigHeights([5, 3, 6, 7, 2], 2, 4)

you check 6 vs 7 and 7 vs 2

bigHeights([5, 13, 6, 7, 2], 0, 2)

you check 5 vs 13 and 13 vs 6

are from each array item to the NEXT one,where it mentioned in challenge about NEXT neighbor element?

bigHeights([5, 3, 6, 7, 2], 0, 1) → 0how above is 0

5-3 is 2 which is not big(as less than 5) so 0. i do not see challenge mentioned about 9 anywhereif big (ie greater than 5 ) then return 1?

that is the sentence telling when to consider a step being "big"

please see the picture + explanation given on sumHeights example, that should clarify

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
public int bigHeights(int[] heights, int start, int end) {
int diff=0;
int sum=0;
int absDiff=0;
int result=0;
for(int i=start; i<end; i++){
diff=heights[i+1]-heights[i];
absDiff=Math.abs(diff);
sum=sum+absDiff;
if(sum>=5){
result=1;
}else if(sum<5){
result=0;
}
}
return result;
}
```

i am failing some tests. please advise

Expected Run

bigHeights([5, 3, 6, 7, 2], 2, 4) → 1 1 OK

bigHeights([5, 3, 6, 7, 2], 0, 1) → 0 0 OK

bigHeights([5, 3, 6, 7, 2], 0, 4) → 1 1 OK

bigHeights([5, 3, 6, 7, 3], 0, 4) → 0 1 X

bigHeights([5, 3, 6, 7, 2], 1, 1) → 0 0 OK

bigHeights([5, 13, 6, 7, 2], 1, 2) → 1 1 OK

bigHeights([5, 13, 6, 7, 2], 0, 2) → 2 1 X

bigHeights([5, 13, 6, 7, 2], 1, 4) → 2 1 X

bigHeights([5, 13, 6, 7, 2], 0, 4) → 3 1 X

bigHeights([5, 13, 6, 7, 2], 0, 3) → 2 1 X

bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 0, 3) → 0 0 OK

bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 4, 8) → 1 1 OK

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 0, 3) → 1 1 OK

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 7, 8) → 1 1 OK

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 3, 8) → 2 1 X

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 2, 8) → 3 1 X

other tests

X

bigHeights([5, 3, 6, 7, 3], 0, 4) → 0 1 X

how above is 0. i thought it should output 1 since

5-------------->3---------

diff 2 diff3 diff 1 difference 4

total sum of difference is 2+3+1+4=10 which is >=5 so output should be 1 right

if totalsum of diference less than 5 then 0 right?

total sum of difference is 2+3+1+4=10 which is >=5 so output should be 1 rightYou are not suppose to add differences up.

The challenge stated

return the number of "big" stepsand as Guy Hengel posted

if the absolute height differences is >=5, add up 1 to the result

You are not accumulating the steps. Your returned result is only 1 at the most because you have

```
if(sum>=5){ result=1;}
```

also the following code
```
sum=sum+absDiff;
```

is not necessary. You need to return the number of big steps. You don't need to add up the differences.
You need to return the number of big steps.

5-------------->3------------------- -->6------ ---------- -------->7 ---------- ---------- --->3

diff 2 diff3 diff 1 difference 4

out of all above differences check if any one of them greater than equal to 5 if yes return 1??

as above differences 2, 3,1, 4 none of them greater than 5 so return 0??

please advise

```
public int bigHeights(int[] heights, int start, int end) {
int diff=0;
int sum=0;
int absDiff=0;
int result=0;
for(int i=start; i<end; i++){
diff=heights[i+1]-heights[i];
// absDiff=Math.abs(diff);
// sum=sum+absDiff;
if(diff>=5){
result=1;
}else if(diff<5){
result=0;
}
}
return result;
}
```

like above?i am failing some tests. please advise

Expected Run

bigHeights([5, 3, 6, 7, 2], 2, 4) → 1 0 X

bigHeights([5, 3, 6, 7, 2], 0, 1) → 0 0 OK

bigHeights([5, 3, 6, 7, 2], 0, 4) → 1 0 X

bigHeights([5, 3, 6, 7, 3], 0, 4) → 0 0 OK

bigHeights([5, 3, 6, 7, 2], 1, 1) → 0 0 OK

bigHeights([5, 13, 6, 7, 2], 1, 2) → 1 0 X

bigHeights([5, 13, 6, 7, 2], 0, 2) → 2 0 X

bigHeights([5, 13, 6, 7, 2], 1, 4) → 2 0 X

bigHeights([5, 13, 6, 7, 2], 0, 4) → 3 0 X

bigHeights([5, 13, 6, 7, 2], 0, 3) → 2 0 X

bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 0, 3) → 0 0 OK

bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 4, 8) → 1 1 OK

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 0, 3) → 1 1 OK

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 7, 8) → 1 1 OK

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 3, 8) → 2 1 X

bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 2, 8) → 3 1 X

other tests

X

```
if(diff>=5){
result=1;
```

That keeps the result at 1 at the most. You are not summing the number of big steps. Also, you are just finding the big up steps. What about the big down steps? ```
else if(diff<5){
result=0;
}
```

That should be removed.
```
public int bigHeights(int[] heights, int start, int end) {
int diff=0;
int sum=0;
int absDiff=0;
int result=0;
for(int i=start; i<end; i++){
diff=Math.abs(heights[i+1]-heights[i]);
// absDiff=Math.abs(diff);
// sum=sum+absDiff;
if(diff>=5){
result=result+1;
}
}
return result;
}
```

something like above? it passes all tests. please advise on improvements, alternate approaches?
something like above?Yes, but if you cleaned it up, then it would be more readable.

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

Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

if the absolute height differences is >=5, add up 1 to the result

7 - 2 = 5 which matches, being in the "array indexes 2-4"

on 2nd example, array 0-1 does not have such a big step, hence 0

the results could be higher than 1, that's why there is no boolean