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 Utkarsh and Distributing Books problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Utkarsh and Distributing Books problem solution Utkarsh’s mother recently received N piles of books as a gift from someone. The ith pile contains Bi books.
 
She neither wants to keep the voluminous books with herself nor she wants to throw them away. So, she decided to distribute them to students of the nearby school. She decided to call K students to her home and ask one of her sons (Utkarsh or Saharsh) to distribute books.
 
Also, she will get happy only if the following condition is satisfied: For every pile i there must be at least one student who receives more than one book from pile i.
 
She knows that Utkarsh is very lazy. So he will randomly pick a student and give him a book from any pile. Since he distributes randomly, he might make her sad.
 
On the other hand, Saharsh is smart and obedient so he will always find a way of distribution (if possible) that will make her happy.
 
You need to output 2 integers: The maximum value of K
 
When Utkarsh is asked to distribute the books. His mother must remain happy irrespective of the way of his distribution.
 
When Saharsh is asked to distribute the books.
 
 
HackerEarth Utkarsh and Distributing Books problem solution

 

 

HackerEarth Utkarsh and Distributing Books problem solution.

#include <iostream>
#include <cassert>
using namespace std;

int n;
int a[1010];

void solve() {
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>a[i];
assert(2 <= a[i] && a[i] <= 1000);

}

int minA = 1E9, sum;
for (int i = 1; i <= n; i++) {
minA = min(minA, a[i] - 1);
sum += a[i];

}

cout<<minA<<" "<<sum - n <<endl;

}

int main()
{
int test;
cin>>test;
while (test--) {
solve();
}
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