Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.
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
Join the community of 500,000 technology professionals and ask your questions.