Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Max Min solution

YASH PAL, 31 July 202410 September 2024

In this HackerRank Max-Min interview preparation kit problem You will be given a list of integers, arr, and a single integer k. You must create an array of length k from elements of arr such that its unfairness is minimized. 

HackerRank Max Min Interview preparation kit solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/usr/bin/env python

import collections, sys


if __name__ == '__main__':
    N = int(sys.stdin.readline())
    K = int(sys.stdin.readline())
    x = sorted(int(sys.stdin.readline()) for _ in range(N))
    print(min(x[i + K - 1] - x[i] for i in range(0, N - K - 1)))

Problem solution in Java Programming.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the maxMin function below.
    static int maxMin(int k, int[] arr) {
        int min=1000000000;
        Arrays.sort(arr);
        for(int i=0;i<arr.length-k+1;i++){
            min=Math.min(min,arr[i+k-1]-arr[i]);
        }
        return min;
    }
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = scanner.nextInt();
        scanner.skip("(rn|[nru2028u2029u0085])?");

        int k = scanner.nextInt();
        scanner.skip("(rn|[nru2028u2029u0085])?");

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            int arrItem = scanner.nextInt();
            scanner.skip("(rn|[nru2028u2029u0085])?");
            arr[i] = arrItem;
        }

        int result = maxMin(k, arr);

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

        bufferedWriter.close();

        scanner.close();
    }
}

Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int arr[100010];

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)cin>>arr[i];
    sort(arr,arr+n);
    int ans=1e9;
    for(int i=k-1;i<n;i++){
        ans=min(arr[i]-arr[i-k+1],ans);
    }
    cout<<ans<<endl;
    return 0;
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int compare2uints(const void * a, const void * b) {
	return ( *(unsigned int*)a - *(unsigned int*)b );
}

int main() {
	int n,k,i;
	unsigned int x[100001];
	unsigned int j,minunfair;
	
	if (scanf(" %d",&n) != 1) return 1;
	if (scanf(" %d",&k) != 1) return 1;
	for (i=0; i<n; i++) if (scanf(" %d",x+i) != 1) return 1;
	if (k==1) return 0;
	qsort(x, n, sizeof(int), compare2uints);
	minunfair = 0x7fffffff;
	i = 0;
	j = k-1;
	while (j<n) {
		if (x[j]-x[i]<minunfair) minunfair=x[j]-x[i];
		i++;
		j++;
	}
	printf("%un",minunfair);
	return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
    var cases = input.trim().split("n"),
        N = - -cases.shift(),
        K = - -cases.shift(),
        result;
    for (var i = 0; i < N; i++) {
        cases[i] = - -cases[i];
    }
    cases.sort(function(a,b){return a-b});
    result = cases[K-1] - cases[0];
    for (var i = K; i < N; i++) {
        result = Math.min(result, cases[i] - cases[i-K+1]);
    }
    console.log(result);
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

coding problems solutions interview prepration kit

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes