Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Architect’s Dilemma..! problem solution

YASH PAL, 31 July 202416 February 2026
In this HackerEarth Architect’s Dilemma..! problem solution An Architect has been assigned a project and some number of workers work for him. Each of the workers is assigned a fixed amount of work. Suddenly, he has been appointed to build a new elegant office by the mayor. He won’t disappoint the mayor and finish the task as quickly as possible.
 
Now, the architect has some amount of extra work to build the new office. He wants to assign this extra work to workers in such a way that there is a maximum number of workers with the same amount of total work. It might not be possible to give an equal amount of work to each worker. He wants to find a maximum number of workers possible with the same amount of total work. It is not necessary that the architect assigns all of his extra work.
 
 
HackerEarth Architect's Dilemma..! problem solution

 

 

HackerEarth Architect’s Dilemma..! problem solution.

import java.util.*;
import java.io.*;

public class Problem {

public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int w=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Arrays.sort(a);
int max=1;
int sum=0,j=n-1;

for(int i=n-1;i>0;i--)
{

while(j>=0)
{
sum+=a[i]-a[j];
if(sum>w)
{
int temp=i-j;
if(temp>max)
max=temp;
sum-=a[i]-a[j];
sum-=(temp-1)*(a[i]-a[i-1]);
break;
}
j--;

}
if(j==-1 && sum<=w)
{
max=Math.max(max,i+1);
}
}
System.out.println(max);


}
}
 

Second solution

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,w,a[100005],pre[100005],maxx[100005];
bool f(int x)
{
for(int i=0,j=x;j<=n;j++,i++)
{
if(x*maxx[j]-(pre[j]-pre[i])<=w)return 1;
}
return 0;
}
int main()
{
cin>>n;
cin>>w;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
//cout<<a[i]<<" ";
pre[i]=pre[i-1]+a[i];
maxx[i]=max(maxx[i-1],a[i]);
}
int l=1,r=n,ans;
while(l<=r)
{
int mid=(l+r)/2;
if(f(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
cout<<ans<<"n";
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes