Solved

sumHeights  challenge

Posted on 2016-08-30
17
86 Views
Last Modified: 2016-09-07
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
17 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41777521
>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
 
LVL 7

Author Comment

by:gudii9
ID: 41780965
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41781142
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 7

Author Comment

by:gudii9
ID: 41781738
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41783683
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
 
LVL 7

Author Comment

by:gudii9
ID: 41787180
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41787352
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
 
LVL 7

Author Comment

by:gudii9
ID: 41787798
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41787875
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
 
LVL 7

Author Comment

by:gudii9
ID: 41787892
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
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 41788005
0
 
LVL 7

Author Comment

by:gudii9
ID: 41788215
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
 
LVL 7

Author Comment

by:gudii9
ID: 41788720
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
 
LVL 27

Expert Comment

by:rrz
ID: 41788841
Yes, that sounds like a good strategy.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41788954
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41789011
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
 
LVL 27

Expert Comment

by:rrz
ID: 41789026
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question