• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 102
  • Last Modified:

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
0
gudii9
Asked:
gudii9
  • 6
  • 4
  • 3
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now