HackerRank Max Min Problem Solution Yashwant Parihar, June 5, 2023August 1, 2024 In this post, we will solve HackerRank Max Min Problem Solution. You will be given a list of integers, arr, and a single integer k. You must create an array of length & from elements of arr such that its unfairness is minimized. Call that array arr. Unfairness of an array is calculated asmax(arr) – min(arr’)Where: max denotes the largest integer in arr. – min denotes the smallest integer in arr’. Examplearr = [1, 4, 7, 2]k = 2Pick any two elements, say arr’ = [4, 7].un fairness = max(4,7) min(4,7) =7-4=3Testing for all pairs, the solution [1, 2] provides the minimum unfairness.Note: Integers in arr 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 n, the number of elements in array arr.The second line contains an integer k. Sample Input 0 7 3 10 100 300 200 1000 20 30 Sample Output 0 25 HackerRank Max Min Problem Solution Max Min C Solution #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int comparator(const void *a, const void *b) {return ( *(int*)a - *(int*)b ); } int main() { int n,k,i; k=0; n=0; scanf("%d",&n); scanf("%d",&k); int a[n]; for(i=0;i<n;i++) { a[i]=0; } for(i=0;i<n;i++) { int num; scanf("%d",&num); a[i]=num; } qsort(a,n, sizeof(int), comparator); int min=10000000000; for(i=0;i<n-k+1;i++) { int loc=a[i+k-1]-a[i]; if(loc<min) {min=loc;} } // printf("%d%d",n,k); printf("%d",min); } Max Min C++ Solution #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ long n; long k; vector<long> a; long x; cin>>n>>k; for(long i=0;i<n;i++){ cin>>x; a.push_back(x); } sort(a.begin(),a.end()); long d; x=a[n-1]; for(long i=0;i<n-k+1;i++){ d = a[i+k-1]-a[i]; //cout<<a[i]<<"---"<<a[(i+k-1)]<<":"<<d<<" "<<endl; if(x>d){ x=d; } } cout<<x<<endl; return 0; } Max Min C Sharp Solution using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AngryChildren { class Program { static void Main(string[] args) { int n; n = int.Parse(Console.ReadLine()); int k; k = int.Parse(Console.ReadLine()); k--; int[] candy = new int[n]; for (int i = 0; i < n; i++) { candy[i] = int.Parse(Console.ReadLine()); } Array.Sort(candy); int minimumUnfairness = 1000000000; for (int i = 0; i < n - k; i++) { int unfairness = candy[i + k] - candy[i]; if (unfairness < minimumUnfairness) minimumUnfairness = unfairness; } Console.WriteLine(minimumUnfairness); } } } Max Min Java 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 '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); int ans = Integer.MAX_VALUE; for(int i = 0 ; i < arr.size() - k+1 ; i ++){ // System.out.println(arr.get(i+k-1) +" "+ arr.get(i)); ans = Math.min(ans,arr.get(i+k-1) - arr.get(i)); } return ans; } } 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(); } } Max Min JavaScript Solution var input = ''; process.stdin.resume() process.stdin.on("data", function (i) { input = input + i; }); process.stdin.on("end", function () { input = input.split("\n"); var N = parseInt(input.shift()); var K = parseInt(input.shift()); var a = []; for (i in input) { a.push(parseInt(input[i])); } a.sort(function(a,b){return a - b;}); var u = -1; for (i=0;i<a.length-K;i++) { var d = a[i+K-1] - a[i]; if (u > d) u = d; if (u === -1) u = d; } //console.log(a); console.log(u); }); Max Min Python Solution import bisect N = int(input()) K = int(input()) cookies = []; for i in range(N): bisect.insort_left(cookies, int(input())) sol = float('inf'); sol = min([cookies[i+K-1] - cookies[i] for i in range(N-K+1)]) print (sol) c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython