bigHeights challenge

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.
how below results are 1,0,1 each? please advise
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
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:
> 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
gudii9Author Commented:
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
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gudii9Author Commented:
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
Guy Hengel [angelIII / a3]Billing EngineerCommented:
> 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
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please see the picture + explanation given on sumHeights example, that should clarify
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:
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;
}

Open in new window


i am failing some tests. please advise
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
rrzCommented:
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;}

Open in new window

also the following code
sum=sum+absDiff;

Open in new window

is not necessary. You need to return the number of big steps. You don't need to add up the differences.
0
gudii9Author Commented:
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??

please advise
0
gudii9Author Commented:
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;
}

Open in new window

like above?
i am failing some tests. please advise
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
rrzCommented:
    if(diff>=5){
      result=1;

Open in new window

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;
    }

Open in new window

That should be removed.
0
gudii9Author Commented:
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;
}

Open in new window

something like above? it passes all tests. please advise on improvements, alternate approaches?
0
rrzCommented:
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;
}

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.