Angry Children 2 – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ Angry Children 2 HackerRank Solution
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <numeric>
#include <complex>
#include <string>
#include <ctime>
#include <queue>
/*
// C++ 11 stuff
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#include <array>
#define tup(name, pos) get<(pos)>(name)
#define A1(type, size) array <type, size>
#define A2(type, s1, s2) A1(A1(type, s2), s1)
#define A3(type, s1, s2, s3) A1(A2(type, s2, s3), s1)
//*/
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> pnt;
#define FI(i,a) for (int i=0; i<(a); ++i)
#define FOR(i,s,e) for (int i=(s); i<(e); ++i)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define V(t) vector < t >
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):(-(a)))
#define ALL(a) a.begin(),a.end()
#define dout(a) cerr << a << endl
#define sout(a) cerr << a << " "
#define lbl cerr << "debug_label" << endl;
const double pi = 3.14159265358979323846264338327950288419716939937511;
const double eps = 1e-9;
//*
char ch_ch_ch[1<<20];
inline string gs() {scanf("%s",ch_ch_ch); return string(ch_ch_ch);}
inline string gl() {gets(ch_ch_ch); return string(ch_ch_ch);}
inline int gi() {int x; scanf("%d",&x); return x;}
//*/
const int inf = 1000000000;
// code starts here
int n;
V(int) a;
V(LL) sum;
void solution() {
n = gi();
int K = gi();
FI(i,n) a.pb(gi());
sort(ALL(a));
sum.resize(n);
LL res = inf*1ll*inf;
sum[0] = a[0];
FOR(i,1,n) sum[i] = a[i] + sum[i-1];
LL cur = 0;
FOR(i,1,K) cur += a[i]*1ll*i - sum[i-1];
res = cur;
if (K > 1) FOR(i,K,n) {
cur += (K-1)*1ll*a[i] - (sum[i-1] - sum[i-K]);
cur -= -(K-1)*1ll*a[i-K] + (sum[i-1] - sum[i-K]);
res = min(res,cur);
}
cout << res << endl;
}
// code ends here
int main(int argc, char** argv) {
#ifdef IGEL_ACM
freopen("in.txt","r",stdin);
//freopen("out.txt", "w", stdout);
clock_t beg_time = clock();
#else
//freopen(argv[1],"r",stdin);
//freopen("painting.in", "r", stdin);
//freopen("painting.out", "w", stdout);
#endif
solution();
#ifdef IGEL_ACM
fprintf(stderr,"*** Time: %.3lf ***\n",1.0*(clock()-beg_time)/CLOCKS_PER_SEC);
#endif
return 0;
}
Java Angry Children 2 HackerRank Solution
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);
}*/
}
Python 3 Angry Children 2 HackerRank Solution
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)
Python 2 Angry Children 2 HackerRank Solution
n=long(raw_input())
k=long(raw_input())
v=[]
for i in xrange(0,n):
x=long(raw_input())
v.append(x)
v.sort()
sp=0
sum=0
for i in xrange(1,k):
sp+=v[i-1]
sum+=(i*v[i]-sp)
r=sum
#print str(sp)+' '+str(sum)
sp+=v[k-1]
for i in xrange(k,n):
#il scoatem pe v[i-k]
sp-=v[i-k]
sum-=(sp-(k-1)*v[i-k])
#il adaugam pe v[i]
sum+=((k-1)*v[i]-sp)
sp+=v[i]
r=min(r,sum)
#print str(i)+' '+str(sp)+' '+str(sum)+' '+str(r)
print r
C Angry Children 2 HackerRank Solution
#include<stdio.h>
#define Mx 100000
long long tree[Mx<<1][3];
int N,K,i,j;
long long arr[Mx+9];
long long L[2],R[2],T[2],lsum,rsum,a,res=9223372036854775807LL;
long long Min(long long g,long long gg){return g<gg?g:gg;}
void update(int idx ,long long t,long long l,long long r){while (idx <= N)tree[idx][0] += t,tree[idx][1] += l,tree[idx][2] += r,idx += (idx & -idx);}
void query(int idx,int f){while (idx > 0)T[f]+=tree[idx][0],L[f]+=tree[idx][1],R[f]+=tree[idx][2],idx -= (idx & -idx);}
long long comp (const void * a, const void * b){return ( *(long long *)a - *(long long *)b );}
int main()
{
scanf("%d %d",&N,&K);
for(i=1; i<=N; i++)scanf("%lld",&arr[i]);
qsort(arr+1,N,sizeof(long long int),comp);
for(i=1; i<=N; i++)update(i,arr[i],i*arr[i],(N-i)*arr[i]);
for(i=K; i<=N; i++)
{
for(j=0; j<2; j++)L[j]=R[j]=T[j]=0;
query(i,0),query(i-K,1);
lsum=(L[0]-L[1])-((i-K+1)*(T[0]-T[1])),rsum=(R[0]-R[1])-((N-i)*(T[0]-T[1]));
res=Min(res,lsum-rsum);
}
printf("%lld\n",res);
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below