Solved

# bigHeights  challenge

Posted on 2016-08-30
55 Views
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.
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
0
Question by:gudii9
• 6
• 4
• 3

LVL 142

Expert Comment

ID: 41777530
> return the number of "big" steps for a walk starting at the start index and ending at the end index.
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
0

LVL 7

Author Comment

ID: 41780976
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, 6, 7, 2], 2, 4) β 1//7 - 2 = 5 which matches, being in the "array indexes 2-4"why we took 7-2 are not we supposed to take 6-1??
bigHeights([5, 3, 6, 7, 2], 0, 1) β 0//on 2nd example, array 0-1 does not have such a big step, hence 0//why no big step ??is it because 3-5 is only 2 which is not 5 hence not big step?
0

LVL 142

Expert Comment

ID: 41781137
because the steps to consider are from each array item to the NEXT one, not the boundaries

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
0

LVL 7

Author Comment

ID: 41781798
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) β 0
how 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?
0

LVL 142

Expert Comment

ID: 41783680
> We'll say that step is big if it is 5 or more up or down

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

LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 250 total points
ID: 41783684
please see the picture + explanation given on sumHeights example, that should clarify
0

LVL 7

Author Comment

ID: 41788972
``````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;
}
``````

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--------------------->6------------------------>7----------------------->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?
0

LVL 27

Assisted Solution

rrz earned 250 total points
ID: 41789038
total sum of difference is 2+3+1+4=10 which is >=5 so output should be 1 right
You are not suppose to add differences up.
The challenge stated
return the number of "big" steps
and 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.
0

LVL 7

Author Comment

ID: 41790622
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??

0

LVL 7

Author Comment

ID: 41790623
``````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?
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
0

LVL 27

Expert Comment

ID: 41790694
``````    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.
0

LVL 7

Author Comment

ID: 41791349
``````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?
0

LVL 27

Expert Comment

ID: 41791389
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;
}
``````
0

## Featured Post

### Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anβ¦