Search This Blog

Tuesday, April 15, 2014

Algorithms - Warmup - Angry Children

Angry Children

The following is the solution to Hacker Rank problem Angry Children using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 30/30

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

// The part of the program involving reading from STDIN and writing to STDOUT has been provided by us.

public class Solution {
       static BufferedReader in = new BufferedReader(new InputStreamReader(
                     System.in));
       static StringBuilder out = new StringBuilder();

       public static void main(String[] args) throws NumberFormatException,
                     IOException {
              int numPackets = Integer.parseInt(in.readLine());
              int numKids = Integer.parseInt(in.readLine());
              int[] packets = new int[numPackets];

              for (int i = 0; i < numPackets; i++) {
                     packets[i] = Integer.parseInt(in.readLine());
              }
              Arrays.sort(packets);

              int unfairness = Integer.MAX_VALUE;

              int min = Integer.MAX_VALUE, max = 0;

              for (int i = 0; i < (numPackets - numKids); i++) {
                     min = packets[i];
                     max = packets[numKids + i - 1];

                     if ((max - min) < unfairness) {
                           unfairness = max - min;
                     }
              }

              // Write your code here, to process numPackets N, numKids K, and the
              // packets of candies
              // Compute the ideal value for unfairness over here

              System.out.println(unfairness);
       }
}

No comments:

Post a Comment

Labels