Skip to main content
Active reading [<https://en.wiktionary.org/wiki/possible#Adjective>]. Word order. Used more standard formatting (we have italics and bold on this platform).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

I'm going to give a quick answer here which might not be the most efficient... You might want to look further into it if you care about efficiency and time complexity.

Anyways... theThe problem in your solution is that you're not checking all possibilepossible couples within the array of numbers.

With this forfor 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

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 forfor 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 only ONCE 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...

I'm going to give a quick answer here which might not be the most efficient... You might want to look further into it if you care about efficiency and time complexity.

Anyways... the problem in your solution is that you're not checking all possibile 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 only ONCE the minimum value between the last iterated couple and the maxInteger value, while you have to that for every couple

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

Stack Overflow is like an encyclopedia, so we prefer to omit these types of phrases. It is assumed that everyone here is trying to be helpful.
Source Link
Dharman
  • 33.9k
  • 27
  • 106
  • 157

I'm going to give a quick answer here which might not be the most efficient... You might want to look further into it if you care about efficiency and time complexity.

Anyways... the problem in your solution is that you're not checking all possibile 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 only ONCE the minimum value between the last iterated couple and the maxInteger value, while you have to that for every couple

Hope this helps

I'm going to give a quick answer here which might not be the most efficient... You might want to look further into it if you care about efficiency and time complexity.

Anyways... the problem in your solution is that you're not checking all possibile 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 only ONCE the minimum value between the last iterated couple and the maxInteger value, while you have to that for every couple

Hope this helps

I'm going to give a quick answer here which might not be the most efficient... You might want to look further into it if you care about efficiency and time complexity.

Anyways... the problem in your solution is that you're not checking all possibile 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 only ONCE the minimum value between the last iterated couple and the maxInteger value, while you have to that for every couple

Source Link
L_Cleo
  • 1.6k
  • 1
  • 15
  • 35

I'm going to give a quick answer here which might not be the most efficient... You might want to look further into it if you care about efficiency and time complexity.

Anyways... the problem in your solution is that you're not checking all possibile 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 only ONCE the minimum value between the last iterated couple and the maxInteger value, while you have to that for every couple

Hope this helps