The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

Hi,

i am working one below challenge

http://codingbat.com/prob/p148138

i was not clear on below description

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.

how below results are 6,2, 11 each? please advise

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

i am working one below challenge

http://codingbat.com/prob/p148138

i was not clear on below description

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.

how below results are 6,2, 11 each? please advise

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

knowing that arrays start with inded 0, index 2 points to the value 6

index 3 is the value of 7

the difference in height is

in human words: it does not matter if you go uphill or downhill, you perform a walk with a height difference of 1, which is the function of absolute.

knowing that arrays start with inded 0, index 2 points to the value 6why we are taking only index 2 and index 3 why not index 0,1 or index 3, 4 ?

index 3 is the value of 7

the difference in height is absolute( 6 - 7 ) which is +1

not clear completely.

So this is array of altitude some climber climbed? then what are we trying to return maximum height of latitude minus minimum of height altitude in absolute?

sumHeights([5, 3, 6, 7, 2], 2, 4) → 6

means the walk starts at index 2, means value 6

next stop is at value/altitute 7, so there is 1 (unit) walked UP

last stop is at value/altitude 2, so the is 5 (units) walked DOWN

1 + 5 is 6

For example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1 + 5 = 6.

above not clean

means the walk starts at index 2, means value 6above is clear as index 2 is clearly 6 within given array

next stop is at value/altitute 7, so there is 1 (unit) walked UPabove not clear.

how next stop is at value/altitude 7??

sumHeights([5, 3, 6, 7,

as per above test ase given array index 2, index 4 are method arguments right. so we have to look now last argument which is index 4 which is value right?

last stop is at value/altitude 2, so the is 5 (units) walked DOWNnot clear on above too. What is this thing about walk up and walk down. what is the refence point and what is the target point to find difference from target minus reference??

this (excel) graph represents the array, as "altitudes"

the green box is the part defined by the start/end part ( 2 , 4 )

so, to go from any index to the next (step), you just take the difference.

so, from step 2 to step 3: you go from value 6 to value 7, which is +1

from step 3 to 4, you go from value 7 down to value 2, which is -5

this should clarify once and for all for this question, as well as the one bigHeigths

i like to draw similar graph for

sumHeights([5, 3, 6, 7, 2], 0, 4) → 11

to cross check 11

you don't "need" a new graph to cross check...

just do it step by step ...

5 starting step (0)

3 diff: -2 => running sum: 2

6 diff: +3 => running sum : 5

7 diff: +1 => running sum: 6

2 diff: - 5 => runnng sum : 11 , last step (4), this is the result

you don't "need" a new graph to cross check...

just do it step by step ...

5 starting step (0)

3 diff: -2 => running sum: 2

6 diff: +3 => running sum : 5

7 diff: +1 => running sum: 6

2 diff: - 5 => runnng sum : 11 , last step (4), this is the result

can you advise on how to do using excel and paint which is crystal clear in 10 seconds than understanding about numbers which is taking weeks to my mind?

it's for sure not that I could not explain this into details, but the core of the question is not about using excel or paint.

and with due respect, if you are not understanding the question, in regards to the information in the question itself, and after this much explanation, you may go and get another path, and not trying to become a developer.

In your profile you wrote that you are doing meditations, you may consider thinking about this...

you also write that you love java programming, but I wonder what you may have been writing as programs to actually state that...

I migth be completely wrong, and may need to stand corrected, and all of the issue is just a language barrier somewhere, in which case I would like you to consider my apologies upfront, I surely don't want to upset you in any way.

5 starting step (0)

3 diff: -2 => running sum: 2

6 diff: +3 => running sum : 5

7 diff: +1 => running sum: 6

2 diff: - 5 => runnng sum : 11 , last step (4), this is the result

let me understand this. I can understand above need to spend some time with pen paper and draw it out

Are you familiar with the mathematical concept of absolute value?

https://www.mathsisfun.com/numbers/absolute-value.html

this helped me a lot. we are just getting difference and cosidering its absolute value(ie positive irrespective of difference positive or negative) and adding up to get final sum

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

-2 ie2 3 1 -5 ie 5====================>tota

Now challenge is clear let me try to work on code

sorry, but that goes far beyond this question.let me open new question for that different discussion.

i draw on a slate graph with erasable marker similar graph that also helped.

so we are interested in total altitude covered by mountaineer irrespective of climbing up altitude or climbing down altitude right(that is the reason we are taking absolute value of change)

loop thorugh given array from startPsedo Code is as above

find the absolute difference with next element

continue till end by summing above differences

return sum

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

above passed all tests. Any improvements or alternate approaches?
All Courses

From novice to tech pro — start learning today.

https://www.mathsisfun.com/numbers/absolute-value.html

https://www.khanacademy.org/math/algebra-basics/basic-alg-foundations/alg-basics-absolute-value-new/v/absolute-value-and-number-lines

and in the API we have

http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-int-