sumHeights challenge

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
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>or example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1 + 5 = 6

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 absolute( 6 - 7 ) which is +1
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.
0
gudii9Author Commented:
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 absolute( 6 - 7 ) which is +1
why we are taking only index 2 and index 3 why not index 0,1 or index 3, 4 ?
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?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the array is the altitudes of the walk.

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
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gudii9Author Commented:
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 6
above is clear as index 2 is clearly 6 within given array
next stop is at value/altitute 7, so there is 1 (unit) walked UP
above not clear.
how next stop is at value/altitude 7??

sumHeights([5, 3, 6, 7, 2], 2, 4)
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 DOWN
not 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??
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Ok, I will try to explain with a picture:
graphical represntation of the array  {5, 3, 6, 7, 2}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
0
gudii9Author Commented:
how did you draw this graph online and pasted here?

i like to draw similar graph for
sumHeights([5, 3, 6, 7, 2], 0, 4) → 11

to cross check 11
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I did that using excel  + reworked with ms paint the screenshot ...

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
0
gudii9Author Commented:
I did that using excel  + reworked with ms paint the screenshot ...

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?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, but that goes far beyond this question.
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.
0
gudii9Author Commented:
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
0

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
gudii9Author Commented:
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
       -2 ie2   3           1            -5 ie 5====================>total is 2 plus 3 plus 1 plus 5 is 11

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)
0
gudii9Author Commented:
loop thorugh given array from start
find the absolute difference with next element
continue till end by summing above differences
return sum
Psedo Code is as above
0
rrzCommented:
Yes, that sounds like a good strategy.
0
gudii9Author Commented:
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;
}

Open in new window

above passed all tests. Any improvements or alternate approaches?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no, that's also the way I would write it, unless you want to add some comments to the code for readability/understanding later...
0
rrzCommented:
My code is basically the same as yours.
public int sumHeights(int[] heights, int start, int end) {
  int sum = 0;
  for(int i = start; i < end; i++){
    sum += Math.abs(heights[i] - heights[i +1]);
  }
  return sum;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.