Angry Children 2
Problem Statement :
Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has brought a box of packets of candies and would like to distribute one packet to each of the children. Each of the packets contains a number of candies. He wants to minimize the cumulative difference in the number of candies in the packets he hands out. This is called the unfairness sum. Determine the minimum unfairness sum achievable. For example, he brings n = 7 packets where the number of candies is packets = [3,3,4,5,7,9,10]. There are k=3 children. The minimum difference between all packets can be had with 3,3,4 from indices 0,1 and 2. We must get the difference in the following pairs: {(0,1),(0,2),(1,2)}. We calculate the unfairness sum as: packets candies 0 3 indices difference result 1 3 (0,1),(0,2) |3-3| + |3-4| 1 2 4 (1,2) |3-4| 1 Total = 2 Function Description Complete the angryChildren function in the editor below. It should return an integer that represents the minimum unfairness sum achievable. angryChildren has the following parameter(s): k: an integer that represents the number of children packets: an array of integers that represent the number of candies in each packet Input Format The first line contains an integer n. The second line contains an integer k. Each of the next n lines contains an integer packets[i]. Constraints 2 <= n <=10^5 2 <= k <= n 0 <= packets[i] <= 10^9 Output Format A single integer representing the minimum achievable unfairness sum.
Solution :
Solution in C :
In c++ :
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100005;
long long a[N], sum[N];
long long s(int a, int b) { return sum[b] - (a ? sum[a - 1] : 0LL); }
int main()
{
//freopen("test.in", "r", stdin);
int n, k;
scanf("%i %i", &n, &k);
for(int i = 0; i < n; i++)
scanf("%lld", &a[i]);
sort(a, a + n);
sum[0] = a[0];
for(int i = 1; i < n; i++)
sum[i] = sum[i - 1] + a[i];
long long curr = 0;
for(int i = 0; i < k; i++)
curr += (long long)(2 * i + 1) * a[i];
long long res = curr;
for(int i = 0; i + k - 1 < n; i++)
{
res = min(res, curr - k * s(i, i + k - 1));
curr -= a[i] + 2LL * s(i + 1, i + k - 1);
curr += (long long)(2 * k - 1) * a[i + k];
}
printf("%lld\n", res);
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static long curTime;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
long[] x = new long[n];
for(int i = 0; i < n; i++) x[i] = in.nextInt();
Arrays.sort(x);
curTime = System.currentTimeMillis();
System.out.println(f(n, k, x));
}
private static long f(int n, int k, long[] x){
long t = unFair(k, x), min = t, s = 0;
//long s = sum(1, k-1, x);
for(int i = 1; i < k; i++) s += x[i];
for(int i = 1; i + k-1 < x.length; i++){
//System.out.println(t);
t += (k-1)*(x[i-1] + x[i+k-1]);
t -= 2*s;
if(t < min) min = t;
s += x[i+k-1] -x[i];
}
return min;
}
private static long unFair(int k, long[] x){
long s = 0;
for(int i = 0; i < k; i++){
s += x[i] *(2*i-k+1);
}
return s;
}
private static long sum(int i, int j, long[] x){
long s = 0;
for(int k = i; k <= j; k++) s += x[k];
return s;
}
/*private static long aSum(int i, int j, long[] x){
if(i == j) return x[i];
return x[j] - aSum(i, j-1, x);
}*/
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
long long int min(long long int a,long long int b){
if(a<b)
return a;
return b;
}
int cmpr(const void * a,const void *b){
return (*(long long int *)a)-(*(long long int *)b);
}
int main(){
long long int n,k,i=0,j;
scanf("%lld %lld",&n,&k);
long long int a[n];
while (i<n) {
scanf("%lld",&a[i]);
i++;
}
qsort(a, n, sizeof(long long int), cmpr);
i=0;
j=1-k;
long long int sum=0,sum1=0;
while(i<k){
sum+=a[i]*j;
sum1+=a[i];
j+=2;
i++;
}
i=0;
j=k;
long long int ans=sum;
while (j<n) {
sum1-=a[i];
sum=sum-(2*sum1)-((1-k)*a[i])+(k-1)*a[j];
sum1+=a[j];
ans=min(ans,sum);
i++;
j++;
}
printf("%lld\n",ans);
return 0;
}
In Python3 :
n = int(input())
k = int(input())
a = []
for i in range(n):
a.append(int(input()))
a.sort()
ps = [0]
for i in range(n):
ps.append(ps[-1] + a[i])
cur = 0
for i in range(k):
cur += i * a[i] - ps[i]
ans = cur
for i in range(1, n - k + 1):
cur -= ps[i + k - 1] - ps[i - 1] - k * a[i - 1]
cur += k * a[i + k - 1] - ps[i + k] + ps[i]
ans = min(ans, cur)
print(ans)
View More Similar Problems
Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →