Min Max Problem HackerRank

 You will be given a list of integers, , and a single integer . You must create an array of length  from elements of  such that its unfairness is minimized. Call that array . Unfairness of an array is calculated as


Where:

- max denotes the largest integer in 

- min denotes the smallest integer in 


Example




Pick any two elements, say .


Testing for all pairs, the solution  provides the minimum unfairness.


Note: Integers in  may not be unique.


Function Description


Complete the maxMin function in the editor below.

maxMin has the following parameter(s):


int k: the number of elements to select

int arr[n]:: an array of integers

Returns


int: the minimum possible unfairness

Input Format


The first line contains an integer , the number of elements in array .

The second line contains an integer .

Each of the next  lines contains an integer  where .


Solution :// Sliding Window Approach


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 'maxMin' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER k
     *  2. INTEGER_ARRAY arr
     */

    public static int maxMin(int k, List<Integer> arr) {
    // Write your code here
Collections.sort(arr);  // Step 1: Sort the array
        int minUnfairness = Integer.MAX_VALUE;  // Initialize unfairness to a large value

        for (int i = 0; i <= arr.size() - k; i++) {
            int unfairness = arr.get(i + k - 1) - arr.get(i);  // Difference between max and min in window
            minUnfairness = Math.min(minUnfairness, unfairness);  // Update minimum unfairness
        }

        return minUnfairness;
    }

}

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

        int k = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = IntStream.range(0, n).mapToObj(i -> {
            try {
                return bufferedReader.readLine().replaceAll("\\s+$", "");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .map(String::trim)
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.maxMin(k, arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        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