Team Formation – 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++ Team Formation HackerRank Solution
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
const int NMAX = 100010;
int T, N, V[NMAX], Ans;
multiset<pair<int,int> > S;
int main()
{
// freopen("c.in", "r", stdin);
// freopen("c.out", "w", stdout);
scanf("%i", &T);
for(; T; T --)
{
scanf("%i", &N);
for(int i = 1; i <= N; ++ i)
scanf("%i", &V[i]);
sort(V + 1, V + N + 1);
S.clear();
S.insert(make_pair(V[1] - 1, 0));
Ans = 0x3f3f3f3f;
for(int i = 1; i <= N; ++ i)
{
while(!S.empty() && S.begin() -> first < V[i] - 1)
{
Ans = min(Ans, S.begin() -> second);
S.erase(S.begin());
}
if(S.empty())
S.insert(make_pair(V[i], 1));
else
{
if(S.begin() -> first == V[i])
S.insert(make_pair(V[i], 1));
else
{
int Nr = S.begin() -> second;
S.erase(S.begin());
S.insert(make_pair(V[i], Nr + 1));
}
}
}
while(!S.empty())
{
Ans = min(Ans, S.begin() -> second);
S.erase(S.begin());
}
printf("%i\n", Ans);
}
}
Java Team Formation HackerRank Solution
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;
public class Solution {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
for(int T = ni();T >= 1;T--){
int n = ni();
int[] a = na(n);
if(n == 0){
out.println(0);
continue;
}
Arrays.sort(a);
Queue<Integer> q = new ArrayDeque<Integer>();
int f = 0;
int prev = -1999999999;
int min = 1999999999;
for(int i = 0;i < n;){
int j = i;
for(;j < n && a[i] == a[j];j++);
int cf = j-i;
if(prev+1 < a[i]){
for(int k = 0;k < f;k++){
min = Math.min(min, prev-q.poll()+1);
}
for(int k = 0;k < cf;k++){
q.add(a[i]);
}
}else{
if(cf > f){
for(int k = f;k < cf;k++){
q.add(a[i]);
}
}else{
for(int k = cf;k < f;k++){
min = Math.min(min, a[i]-q.poll());
}
}
}
prev = a[i];
f = cf;
i = j;
}
for(int k = 0;k < f;k++){
min = Math.min(min, prev-q.poll()+1);
}
out.println(min);
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }
private static String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private static int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private static int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
Python 3 Team Formation HackerRank Solution
import collections
import itertools
def answer(skills):
if not skills:
return 0
finished_team_lengths = []
current_team_starts = collections.deque()
def go(s, c):
while len(current_team_starts) < c:
current_team_starts.append(s)
while len(current_team_starts) > c:
finished_team_lengths.append(s - current_team_starts.popleft())
last_s = skills[0] - 1
for s, g in itertools.groupby(skills):
if s > last_s + 1:
go(last_s + 1, 0)
go(s, sum(1 for _ in g))
last_s = s
go(last_s + 1, 0)
return min(finished_team_lengths)
t = int(input())
for i in range(t):
print(answer(sorted(map(int, input().split()[1:]))))
Python 2 Team Formation HackerRank Solution
import sys
t = int(sys.stdin.readline())
for tix in range(t):
line = sys.stdin.readline().split(' ')
if line[-1] == '\n':
line = line[:-1]
xs = map(int, filter(lambda x: len(x) > 0, line))[1:]
xs.sort()
xxs = []
last = None
for x in xs:
if last == x:
xxs[-1] = (x, xxs[-1][1] + 1)
else:
last = x
xxs.append((x, 1))
seq = []
lastsz = 0
last = None
res = len(xs)
for x, sz in xxs:
if last is None or last + 1 < x:
for s in seq:
res = min(res, len(s))
seq = [[x] for ix in range(sz)]
lastsz = sz
last = x
else:
if sz < len(seq):
for ix in range(len(seq) - sz):
s = seq.pop(0)
res = min(res, len(s))
for s in seq:
s.append(x)
seq += [[x] for ix in range(sz - len(seq))]
lastsz = sz
last = x
for s in seq:
res = min(res, len(s))
print 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