Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Utkarsh and Distributing Books problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes