Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Created December 16, 2014 04:25
Show Gist options
  • Save hyunjun/ea50818dea8a9b4e2193 to your computer and use it in GitHub Desktop.
Save hyunjun/ea50818dea8a9b4e2193 to your computer and use it in GitHub Desktop.
[hackerrank] angry children
#include <stdio.h>
#include <stdlib.h>
int compare(const void* a, const void* b) {
int l = * (int*) a;
int r = * (int*) b;
if ( l > r ) return 1;
if ( l < r ) return -1;
return 0;
}
int distribution_of_candies(int N, int K, int* packets) {
qsort(packets, N, sizeof(int), compare);
int min = 1000000000;
int i = 0;
for ( i = 0; i <= N - K; ++i ) {
int tmp = *(packets + i + K - 1) - *(packets + i);
if ( tmp < min ) min = tmp;
}
return min;
}
int main() {
int N = 0;
fscanf(stdin, "%d", &N);
int K = 0;
fscanf(stdin, "%d", &K);
int* packets = (int*) malloc(sizeof(int) * N);
int i = 0;
for ( i = 0; i < N; ++i ) {
fscanf(stdin, "%d", (packets + i));
}
printf("%d\n", distribution_of_candies(N, K, packets));
free(packets);
}
import sys
def distribution_of_candies(n, packets):
minimal_unfairness, sorted_packets = sys.maxint, sorted(packets)
for i in range(len(packets) - n):
tmp_unfairness = sorted_packets[n + i - 1] - sorted_packets[i]
if tmp_unfairness < minimal_unfairness:
minimal_unfairness = tmp_unfairness
return minimal_unfairness
if __name__ == '__main__':
N = int(raw_input())
K = int(raw_input())
packets = []
for line in sys.stdin:
packets.append(int(line.strip()))
print(distribution_of_candies(K, packets))
import sys
def distribution_of_candies(n, packets):
sorted_packets = sorted(packets)
return min([sorted_packets[n + i - 1] - sorted_packets[i] for i in range(len(packets) - n)])
if __name__ == '__main__':
N, K, packets = int(input()), int(input()), []
[packets.append(int(line.strip())) for line in sys.stdin]
print(distribution_of_candies(K, packets))
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static int distributionOfCandies(final int K, final List<Integer> packets) {
int min = Integer.MAX_VALUE;
Collections.sort(packets);
for ( int i = 0; i <= packets.size() - K; ++i ) {
int tmp = packets.get(i + K - 1) - packets.get(i);
if ( tmp < min ) min = tmp;
}
return min;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
int K = Integer.parseInt(sc.nextLine());
List<Integer> packets = new ArrayList<Integer>();
for ( int i = 0; i< N; ++i ) {
packets.add(Integer.parseInt(sc.nextLine()));
}
System.out.println(distributionOfCandies(K, packets));
}
}
// https://www.hackerrank.com/challenges/angry-children
object Solution {
def distributionOfCandies(K: Int, packets: Array[Int]): Int = {
var min: Int = Int.MaxValue
val sortedPackets = packets.sorted
for ( i <- 0 until sortedPackets.length - K + 1 ) {
val tmp = sortedPackets(i + K - 1) - sortedPackets(i)
if ( tmp < min ) min = tmp
}
min
}
def main(args: Array[String]) {
val inps = io.Source.stdin.getLines.map((s: String) => s.toInt).toArray
val N = inps(0)
val K = inps(1)
val packets = inps.slice(2, inps.length)
println(distributionOfCandies(K, packets))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment