Solved

sumHeights  challenge

Posted on 2016-08-30
17
60 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
  • 8
  • 6
  • 3
17 Comments
 
LVL 142

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 142

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
 
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 142

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 142

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 142

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 142

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now