Closet Number Problem HackerRank

  Sorting is useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses as well. In this case, it will make it easier to determine which pair or pairs of elements have the smallest absolute difference between them.


Example


Sorted, . Several pairs have the minimum difference of : . Return the array .


Note

As shown in the example, pairs may overlap.


Given a list of unsorted integers, , find the pair of elements that have the smallest absolute difference between them. If there are multiple pairs, find them all.


Function Description


Complete the closestNumbers function in the editor below.


closestNumbers has the following parameter(s):


int arr[n]: an array of integers

Returns

- int[]: an array of integers as described


Input Format


The first line contains a single integer , the length of .

The second line contains  space-separated integers, .


Constraints


All  are unique in .

Output Format


Sample Input 0


10

-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -646159


Solution :



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 'closestNumbers' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static List<Integer> closestNumbers(List<Integer> arr) {
    // Write your code here
       Collections.sort(arr);
ArrayList<Integer> result= new ArrayList<>();
int n=arr.size();
int mindiff=Integer.MAX_VALUE;

for(int i=0;i<n-1;i++)
{
    int diff=arr.get(i+1)-arr.get(i);
   
    if (diff < mindiff) {
                mindiff = diff;
                result.clear();  // Reset result with the new smallest pair
                result.add(arr.get(i));
                result.add(arr.get(i + 1));
            } else if (diff == mindiff) {
                result.add(arr.get(i));
                result.add(arr.get(i + 1));  // Add more pairs with the same smallest difference
            }
}
    return result;

    }

}

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());

        List<Integer> result = Result.closestNumbers(arr);

        bufferedWriter.write(
            result.stream()
                .map(Object::toString)
                .collect(joining(" "))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence