-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jaebradley/minimum-absolute-difference
Minimum absolute difference
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## Problem | ||
|
||
From [this Hackerrank problem](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array). | ||
|
||
> Given an array of N integers, find and print the minimum absolute difference between any two elements in the array. | ||
## Approach | ||
|
||
1. If there are less than two values in the array, throw an `IllegalArgumentException`. It's hard to find a difference | ||
between `0` or `1` integers. | ||
2. Sort the input array. | ||
3. Set the initial minimum absolute difference to the absolute value of the difference between the value in index `1` | ||
and the value in index `0`. | ||
4. Iterate through the sorted array and see if the absolute difference between two indices are less than the current | ||
absolute difference. If so, this absolute difference becomes the new minimum value. | ||
5. Return the minimum value. | ||
|
||
## Implementation | ||
|
||
<!-- language: lang-java --> | ||
public class MinimumAbsoluteDifferenceIdentifier { | ||
public static int identifyMinimumAbsoluteDifference(int[] values) { | ||
if (values.length <= 1) { | ||
throw new IllegalArgumentException("Array must have at least two elements"); | ||
} | ||
|
||
Arrays.sort(values); | ||
|
||
int minimumAbsoluteDifference = Math.abs(values[1] - values[0]); | ||
|
||
for (int i = 1, j = 2; j < values.length; i = j - 1, j++, i++) { | ||
int absoluteDifference = Math.abs(values[j] - values[i]); | ||
|
||
if (absoluteDifference < minimumAbsoluteDifference) { | ||
minimumAbsoluteDifference = absoluteDifference; | ||
} | ||
} | ||
|
||
return minimumAbsoluteDifference; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/algorithms/implementations/MinimumAbsoluteDifferenceIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package algorithms.implementations; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array | ||
* | ||
* Given an array of N integers, find and print the minimum absolute difference between any two elements in the array. | ||
*/ | ||
|
||
public class MinimumAbsoluteDifferenceIdentifier { | ||
public static int identifyMinimumAbsoluteDifference(int[] values) { | ||
if (values.length <= 1) { | ||
throw new IllegalArgumentException("Array must have at least two elements"); | ||
} | ||
|
||
Arrays.sort(values); | ||
|
||
int minimumAbsoluteDifference = Math.abs(values[1] - values[0]); | ||
|
||
for (int i = 1, j = 2; j < values.length; i = j - 1, j++, i++) { | ||
int absoluteDifference = Math.abs(values[j] - values[i]); | ||
|
||
if (absoluteDifference < minimumAbsoluteDifference) { | ||
minimumAbsoluteDifference = absoluteDifference; | ||
} | ||
} | ||
|
||
return minimumAbsoluteDifference; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/algorithms/implementations/MinimumAbsoluteDifferenceIdentifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package algorithms.implementations; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class MinimumAbsoluteDifferenceIdentifierTest { | ||
@Test | ||
public void testMinimumAbsoluteDifference() { | ||
final int[] values = new int[] { 3, -7, 0 }; | ||
assertEquals(3, MinimumAbsoluteDifferenceIdentifier.identifyMinimumAbsoluteDifference(values)); | ||
} | ||
|
||
@Test | ||
public void itThrowsForEmptyValues() { | ||
try { | ||
MinimumAbsoluteDifferenceIdentifier.identifyMinimumAbsoluteDifference(new int[] {}); | ||
} catch (IllegalArgumentException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void itReturnsMinimumAbsoluteDifferenceForTwoElementArray() { | ||
final int[] values = new int[] { 3, -7 }; | ||
assertEquals(10, MinimumAbsoluteDifferenceIdentifier.identifyMinimumAbsoluteDifference(values)); | ||
} | ||
} |