Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum absolute difference #9

Merged
merged 4 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions codereview/minimumAbsoluteDifferenceIdentifier.md
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;
}
}
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;
}
}
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));
}
}