Accessory Collection – 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++ Accessory Collection HackerRank Solution
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
//const int mo=0;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}
int d,n,l,a;
ll sum(int l,int r){
return 1ll*(l+r)*(r-l+1)/2;
}
inline ll val(int x){
int lef=l-(n-1);
int num=lef/x;
int res=lef%x;
ll ans=sum(a-d+1-num+1,a-d+1)*x;
ans+=(a-d+1ll-num)*res;
ans+=sum(a-d+2,a-1)*x;
res=n-1-x*(d-2);
ans+=1ll*res*a;
return ans;
}
int main()
{
#ifdef JCVB
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int _time_jc=clock();
#endif
int te;gn(te);
while(te--){
gn(l);gn(a);gn(n);gn(d);
if(d==1){
printf(lld"\n",1ll*a*l);
continue;
}
if(a<d){
printf("SAD\n");
continue;
}
int lef=l-(n-1);
int l1=cei(lef,a-d+1);
int r1=flo(n-1,d-1);
ll ma=0;
if(l1>r1){
printf("SAD\n");
continue;
}
for (int x=l1;x<=r1;x++)upmax(ma,val(x));
//printf(lld"\n",max(val(l1),val(r1)));
printf(lld"\n",ma);
}
#ifdef JCVB
debug("time: %d\n",int(clock()-_time_jc));
#endif
return 0;
}
Java Accessory Collection HackerRank Solution
import java.io.*;
import java.util.*;
import java.math.*;
public class Vic {
static long Solve(int L, int A, int N, int D) {
if(D<2 || N>L) // no constraint; use only element A
return (long)L*A;
else if(A<D || D>N) return -1; // no solution
else {
// a solution is always
// bestsum of the best set of L elements where each subset
long bestsum = 0;
for(int i = (int)Math.ceil((L-N+1.0)/(A-D+1.0)); i <= (N-1)/(D-1); i++){
int used = N-i*(D-2)-1; // number of A; used <= L.
long sum = (long)used*A;
if(D==2 && used>i){i=used;};//since i has not occurred
// the next num values A-1..A-num are taken min times
long num = (L-used)/i; //integer division will round down
sum += (num*i*(2*A-1-num))/2;
used += num*i;
// a last value keeps the rest
sum += (L-used)*(A-num-1);
if(sum>bestsum) bestsum=sum;
}
return bestsum;
}
} //Solve
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for(int a0 = 0; a0 < T; a0++){
int L = in.nextInt();
int A = in.nextInt();
int N = in.nextInt();
int D = in.nextInt();
long res = Solve(L,A,N,D);
if(res<=0)
System.out.println("SAD");
else
System.out.println(res);
} // for
} //main
}
Python 3 Accessory Collection HackerRank Solution
import math
def total(g):
# g = size of consecutive groups after the first one
if g <= 0:
return 0
first = N - g * (D - 2) - 1
f = (L - first) // g
if first < g or A - f <= 0 or first + g * (A - 1) < L:
return 0
partial = int((2 * (A - 1) - f + 1) / 2 * f)
left = (A - f - 1) * (-f * g + L - first)
return first * A + partial * g + left
for _ in range(int(input())):
L, A, N, D = map(int, input().strip().split())
if D == 1:
print(L * A)
continue
if D == 2:
n1 = (N - 1)
if n1 * A >= L:
ln1 = L // n1
print(int((2 * A + 1 - ln1) / 2 * ln1 * n1) + (A - ln1) * (L - ln1 * n1))
continue
else:
print('SAD')
continue
if A == 1:
print('SAD')
continue
if A == 2:
if D == 2 and (N - D + 1) * 2 >= L:
print(N - D + L + 1)
continue
else:
print('SAD')
continue
g = math.ceil((L - A - N + D) / (A - 2))
if g > N - D:
print('SAD')
continue
else:
# I did the math
# ArgMax of total, A > 2 and D > 2
argmax = int(math.sqrt((2 - 3 * D + D * D) * (1 + L - N) * (1 + L - N)) / (2 - 3 * D + D * D)) + 1
if (D == 3 or D == 4 and A > 2) or (D > 4 and A > D - 2):
# region where enough items are in A (A - f > 0)
argmax = max(int((1 + L - N) / (2 + A - D)), argmax)
# region where first group is the largest (first >= g)
argmax = min(int((N - 1) / (D - 1)), argmax)
# region where total number of items is at least L (first + g * (A - 1) >= L)
if D == 3 or A > D - 1:
argmax = max(int((1 + L - N) / (1 + A - D)), argmax)
elif D > 3 and A < D - 1:
argmax = min(int((1 + L - N) / (1 + A - D)), argmax)
max_haul = max(total(argmax - 1), total(argmax), total(argmax + 1))
print(max_haul if max_haul else 'SAD')
Python 2 Accessory Collection HackerRank Solution
#!/bin/python
for cas in xrange(input()):
l,a,n,d = map(int, raw_input().strip().split())
if d == 1:
res = l*a
else:
res = 0
for rep in xrange(1,l+1):
m = n-1-rep*(d-1)
if m < 0: break
v = max(0,a-((l-m)/rep))
ln = (a-v)*rep + m
if ln < l and v == 0: continue
total = (a-v)*(a-v+1)/2 * rep - (a-v)*(l-m) + a*l
res = max(res, total)
print res or 'SAD'
C Accessory Collection HackerRank Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int T;
scanf("%d",&T);
for(int a0 = 0; a0 < T; a0++){
int L;
int A;
int N;
int D;
scanf("%d %d %d %d",&L,&A,&N,&D);
if (D == 1) {
printf("%lld\n", (long long)A*L);
continue;
}
int max, min, i, q, r;
long long count, result = 0;
max = (N-1)/(D-1);
if ((L-(N-1)+max-1)/max+D-1 > A) {
printf("SAD\n");
continue;
}
min = (L-(N-1)+A-(D-1)-1)/(A-(D-1));
for (i=max; i>=min; i--) {
q = (L-(N-1))/i;
r = (L-(N-1))%i;
count = (long long)(A-(D-1+q))*r + (long long)(A-(D-1+q)+1+A-1)*(D-2+q)/2*i + (long long)A*(N-1-i*(D-2));
if (count <= result) {
break;
}
result = count;
}
printf("%lld\n", result);
}
return 0;
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below