Created
June 25, 2021 18:41
-
-
Save JonathanLalou/b16253e86772153c750dcb8ad2d4c7d1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.io.*; | |
import java.math.*; | |
import java.security.*; | |
import java.text.*; | |
import java.util.*; | |
import java.util.concurrent.*; | |
import java.util.function.*; | |
import java.util.regex.*; | |
import java.util.stream.*; | |
import static java.util.stream.Collectors.joining; | |
import static java.util.stream.Collectors.toList; | |
class Result { | |
/* | |
* Complete the 'minimumAbsoluteDifference' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts INTEGER_ARRAY arr as parameter. | |
*/ | |
public static int minimumAbsoluteDifference(List<Integer> arr) { | |
Collections.sort(arr); | |
int min = Integer.MAX_VALUE; | |
for (int i =0; i<arr.size()-1; i++){ | |
int difference = Math.abs(arr.get(i) - arr.get(i+1)); | |
if ( difference < min){ | |
min = difference; | |
} | |
/* | |
for (int j=i+1; j<arr.size()/2; j++){ | |
int difference = Math.abs(arr.get(i) - arr.get(j)); | |
if ( difference < min){ | |
min = difference; | |
} | |
} | |
*/ | |
} | |
return min; | |
} | |
} | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); | |
int n = Integer.parseInt(bufferedReader.readLine().trim()); | |
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
int result = Result.minimumAbsoluteDifference(arr); | |
bufferedWriter.write(String.valueOf(result)); | |
bufferedWriter.newLine(); | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2' for the first version (O(n*n)), 7' to optimize (O(n+n ln(n)))