1

Here is my solution:

public static int minimumAbsoluteDifference(List<Integer> arr) { int absValues = 0; int maxNum = Integer.MAX_VALUE; Collections.sort(arr); for (int i = 0; i < arr.size() - 1; i++){ absValues = Math.abs(arr.get(i) - arr.get(i + 1)); } int absValuesDiff = Math.min(absValues, maxNum); return absValuesDiff; } 

I pass small test cases, but not ones with a larger data set.

1
  • 1
    Are you failing the last test case because the time limit has been exceeded? Commented Jun 27, 2021 at 23:26

2 Answers 2

2

As L_Cleo suggested, you indeed check the minimum value only once. Instead, you should check it on every iteration of the loop:

public static int minimumAbsoluteDifference(List<Integer> arr) { int absValue = Integer.MAX_VALUE; Collections.sort(arr); for (int i = 0; i < arr.size() - 1; i++) { absValue = Math.min(absValue, Math.abs(arr.get(i) - arr.get(i + 1))); } return absValue; } 

I added the minimum check on every iteration. The checking is done between the absValue and the absolute difference of the current loop iteration.

By the end of the loop, absValue will contain the minimum value, because you have compared it to every other possibility.

The longer explanation why this works is this:

The reason why updating the absValue in each loop iteration is enough is this: absValue's first value is Integer.MAX_VALUE. This means that any other value will be smaller than absValue. In the first loop iteration, absValue is updated with the first absolute difference. In all other iterations, the code will always update absValue with the smaller value between the current absValue and the absolute difference you calculate on each iteration.

This guarantees that, for all iterations, the smallest value will be stored in absValue at some point, and in the further iterations, absValue value will not change.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem in your solution is that you're not checking all possible couples within the array of numbers.

With this for loop, for example:

for (int i = 0; i < arr.size() - 1; i++) { absValues = Math.abs(arr.get(i) - arr.get(i + 1)); } 

And iterating over the simple array 1, 4, 6, 7, you would be checking the couples [1,4], [4,6] and [6,7], but you're still missing 1,7 and 4,7.

So step number one would be to iterate over the array per every number of the array (knowing that the order of the pairs doesn't matter [1,6] == [6,1] in this case, you will have to check 1 less number every iteration of the outer loop... here is an example of a correct for loop:

public static int minimumAbsoluteDifference(List<Integer> arr) { Collections.sort(arr); int minAbsoluteValue = Integer.MAX_VALUE; for(int i=0; i < arr.size() - 1; i++) { for(int j=i+1; j < arr.size(); j++) minAbsoluteValue = Math.min(Math.abs(arr.get(i) - arr.get(j)), minAbsoluteValue); } return minAbsoluteValue; } 

As you can see, another missing part in your solution is the tracking of the minimum value. You're checking the minimum value between the last iterated couple and the maxInteger value only once, while you have to that for every couple.

This might not be the most efficient solution...

2 Comments

Actually, in this case, checking only the sequence pairs, i.e. [1,4] and [4,6] is fine here. Because the abs. difference of those will always be smaller then the other combinations.
Absolutely, that seem kinda of a dumb mistake on my side

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.