Wednesday, 22 October 2014

Hackerrank Angry Childrem Solution


The Solution for hackerrank poblem, Angry Childrem Solution using C++ program.
Passed Testcases : 15 out of 15
Source Code:

 #include <stdio.h>  
 #include <string.h>  
 #include <math.h>  
 #include <stdlib.h>  
 #include <algorithm>  
 using namespace std;  
 #define MAX 100000  
 #define MAX_VAL 1000000001  
 int candies[MAX];  
 int main() {  
   int N,K;  
   int i,upper,lower[MAX];  
   scanf("%d %d",&N,&K);  
   for(i = 0;i < N;i++){  
     scanf("%d",candies + i);  
   }  
   sort(candies,candies+N);  
   int unfairness =  candies[K-1]-candies[0];  
 for(i = 0;i < N-K-1;i++){  
   if(unfairness>candies[i+K-1]-candies[i])  
     unfairness = candies[i+K-1]-candies[i];  
 }  
   printf("%d\n",unfairness);  
   return 0;  
 }  

0 comments:

Post a comment