Looks like you missed a small detail in your task description.

There, the difference is defined as

|arr[i]-arr[j]|

But you are calculating the difference simply as

arr[j] - arr[i]

Conclution

Use

`Math.abs(value)`

to solve the problem.For more information about

`Math.abs()`

see e.g. here: Java - abs() Method[^] I hope it helps.

But since, once again, you haven't provided any test cases, or any details of the problem, we can't tell you.

1 - 4: Difference is -3

2 - 1: Difference is 1

But that is maybe waht you need?

abs(1-4)= 3

abs(2-1)= 1

... and it is mentioned in the task:

|arr[i]-arr[j]|

where the two '|' on the left and on the right mean absolute value ;)