What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.
Become a Premium Member and unlock a new, free course in leading technologies each month.
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
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
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) → 0how above is 0
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
total sum of difference is 2+3+1+4=10 which is >=5 so output should be 1 rightYou are not suppose to add differences up.
return the number of "big" stepsand as Guy Hengel posted
if the absolute height differences is >=5, add up 1 to the result
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.
You need to return the number of big steps.
5-------------->3------------------- -->6------ ---------- -------->7 ---------- ---------- --->3
diff 2 diff3 diff 1 difference 4
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
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.
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?
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
ejb wildfly example | 2 | 111 | |
Code Coverage and Coding Standards | 2 | 81 | |
Java - accessing a parent class member when another class inherits from the parent class | 9 | 57 | |
Firefox and firebub | 5 | 28 |
Join the community of 500,000 technology professionals and ask your questions.