CSES Solutions - Reading Books
Last Updated :
28 Mar, 2024
There are N books, and Kotivalo and Justiina are going to read them all. For each book, you know the time it takes to read it, given as arr[] such that arr[i] is the time it takes to read ith book.
They both read each book from beginning to end, and they cannot read a book at the same time. What is the minimum total time required?
Examples:
Input: N = 3, arr[] = {2, 8, 3}
Output: 16
Explanation:
- At time = 0, Kotivalo starts reading the book with reading time = 8 and Justiina starts reading the book with reading time = 2.
- At time = 2, Kotivalo is still reading the book with reading time = 8 and Justiina starts reading the book with reading time = 3.
- At time = 5, Kotivalo is still reading the book with reading time = 8 and Justiina finished reading all the other book and waits for Kotivalo to finish the book.
- At time = 8, Kotivalo finished reading the book with reading time = 8 and starts reading book with reading time = 2. Justiina starts reading the book with reading time = 8.
- At time = 10, Kotivalo starts reading the book with reading time = 3 and Justiina is still reading the book with reading time = 8.
- At time = 13, Kotivalo finished reading all the books and Justiina is still reading the book with reading time = 8.
- At time = 16, both Kotivalo and Justiina finished reading all the books.
Input: N = 4, arr[] = {1, 2, 10, 10}
Output: 23
Explanation:
- At time = 0, Kotivalo starts reading the first book with reading time = 10 and Justiina starts reading the book with reading time = 1.
- At time = 1, Kotivalo is still reading the first book with reading time = 10 and Justiina starts reading the book with reading time = 2.
- At time = 3, Kotivalo is still reading the first book with reading time = 10 and Justina starts reading the second book with reading time = 10.
- At time = 10, Kotivalo finishes reading the first book with reading time = 10 and starts reading the book with reading time = 1. Justiina is still reading the second book with reading time = 10.
- At time = 11, Kotivalo starts reading the book with reading time = 2 and Justiina is still reading the second book with reading time = 10.
- At time = 13, Kotivalo starts reading the second book with reading time = 10 and Justiina starts reading the first book with reading time = 10.
- At time = 23, both Kotivalo and Justiina finished reading all the books.
Approach: To solve the problem, follow the below idea:
The problem can be solved using Greedy Approach. Sort the reading time arr[] in ascending order. Now, ask Kotivalo to read the book with the maximum reading time and ask Justiina to start reading from the book with the minimum reading time. After Kotivalo finishes reading his book, he can also start from the book with the minimum reading time. We can have two cases by the time Kotivalo finishes reading the book with maximum reading time:
Case 1: Justiina would have read all the other books and has been waiting for Kotivalo to finish reading the book. This will happen when the reading time of the book Kotivalo read is greater than the reading time of all other books combined. In this case, the total reading time to finish all the books will be:
= maximum reading time of a book + sum of remaining books + waiting time of Justiina
= maximum reading time of a book + sum of remaining books + (maximum reading time of a book - sum of remaining books)
= 2 * maximum reading time of a book
Case 2: Justiina has not read all the remaining books yet or read all the remaining books at the same time as Kotivalo finished reading the book with maximum time. This will happen when the reading time of the book Kotivalo read is lesser than or equal to the reading time of all other books combined. In this case, the total reading time to finish all the books will be:
= maximum reading time of a book + sum of remaining books
Step-by-step algorithm:
- Sort the array arr[] in increasing order.
- Declare a variable, say largest to store the largest reading time.
- Declare a variable, say sumOfSmaller to store the sum of all the books except the book with largest reading time.
- If largest <= sumOfSmaller, return (sumOfSmaller + largest) as the answer.
- Otherwise, return (sumOfSmaller + largest + largest - sumOfSmaller) = 2 * largest as the answer.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
// Function to find the total time required by Kotivalo and
// Justiina to read all the books
ll solve(vector<ll>& arr, ll N)
{
// Sort the books in increasing order of their reading
// times
sort(arr.begin(), arr.end());
// Store the largest reading time
ll largest = arr[N - 1];
// Store the sum of remaining remaining times
ll sumOfSmaller
= accumulate(arr.begin(), arr.end() - 1, 0LL);
// If largest <= sumOfSmaller, then none of them will
// have to wait and answer will be the sum of reading
// times of all the books
if (largest <= sumOfSmaller) {
return sumOfSmaller + largest;
}
// Otherwise, one of them have to wait and the answer
// will be 2 * largest reading time
return 2 * largest;
}
int main()
{
// Sample Input
ll N = 4;
vector<ll> arr = { 1, 2, 10, 10 };
cout << solve(arr, N);
// your code goes here
return 0;
}
Java
import java.util.Arrays;
public class GFG {
public static int solve(int[] arr) {
// Sort the books in increasing order of their reading times
Arrays.sort(arr);
// Store the largest reading time
int largest = arr[arr.length - 1];
// Store the sum of remaining times
int sumOfSmaller = 0;
for (int i = 0; i < arr.length - 1; i++) {
sumOfSmaller += arr[i];
}
if (largest <= sumOfSmaller) {
return sumOfSmaller + largest;
}
// Otherwise, one of them has to wait and
// answer will be 2 * largest reading time
return 2 * largest;
}
public static void main(String[] args) {
int[] arr = {1, 2, 10, 10};
System.out.println(solve(arr));
}
}
JavaScript
function solve(arr) {
// Sort the books in increasing order of their reading times
arr.sort((a, b) => a - b);
// Store the largest reading time
const largest = arr[arr.length - 1];
// Store the sum of remaining times
const sumOfSmaller = arr.slice(0, -1).reduce((acc, current) => acc + current, 0);
// If largest <= sumOfSmaller, then none of them will have to wait
// and answer will be the sum of reading times of all the books
if (largest <= sumOfSmaller) {
return sumOfSmaller + largest;
}
// Otherwise, one of them has to wait and the answer will be 2 * the largest reading time
return 2 * largest;
}
// Driver code
const arr = [1, 2, 10, 10];
console.log(solve(arr));
Python3
# Function to find the total time required by Kotivalo and Justiina to read all the books
def solve(arr):
# Sort the books in increasing order of their reading times
arr.sort()
# Store the largest reading time
largest = arr[-1]
# Store the sum of remaining times
sumOfSmaller = sum(arr[:-1])
# If largest <= sumOfSmaller, then none of them will have to wait
# and answer will be the sum of reading times of all the books
if largest <= sumOfSmaller:
return sumOfSmaller + largest
# Otherwise, one of them has to wait and the answer will be 2 * largest reading time
return 2 * largest
# Driver code
arr = [1, 2, 10, 10]
print(solve(arr))
Time Complexity: O(N * logN), where N is the number of books to read.
Auxiliary Space: O(N)
Similar Reads
CSES Solutions - Book Shop
You are in a book shop which sells N different books. You know the price and number of pages of each book. You have decided that the total price of your purchases will be at most X. What is the maximum number of pages you can buy? You can buy each book at most once. Examples: Input: N = 4, X = 10, p
7 min read
CSES Problem Set Solutions
In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES
8 min read
C Programming - GATE CSE Previous Year Questions
The C Programming section in GATE is one of the most important areas, and it covers fundamental concepts that form the foundation of the exam. To assist you in your preparation, we've organized the previous year questions (PYQs) into multiple sets. These sets cover a wide range of C Programming conc
2 min read
Best Books for Learning Low-Level Design(LLD) [2024]
Are you looking to master Low-Level Design (LLD) and enhance your software development skills? We have a list of the best books for learning LLD in 2024. Whether you're a beginner or an experienced programmer, these books will guide you through the essential concepts and techniques of LLD. From unde
4 min read
CBSE Class 11 | Problem Solving Methodologies
Problem Solving Process The process of problem-solving is an activity which has its ingredients as the specification of the program and the served dish is a correct program. This activity comprises of four steps : 1. Understanding the problem: To solve any problem it is very crucial to understand th
13 min read
7 Best System Design Books [2025]
Have you thought about how companies are building and handling complex and large-size applications with millions and millions of users, like Instagram and Gmail? The answer to this is system design. You can simply understand this as the process of identifying the requirements of organizations and en
10 min read
How to Score Above 90% in CBSE Class 10 Exams 2024
The CBSE Class 10 Board Exams are a defining moment in your academic journey. They are a stepping stone to higher education and your future aspirations. Scoring above 90% in these exams might seem daunting, but with the right strategies and dedication, it's a goal within reach. This article will hel
8 min read
NCERT Solutions for Class 9 English Beehive Chapter 1 The Fun They Had
We have tried to provide NCERT Solutions for Class 9 English Chapter 1 The Fun They Had covering each and every question of the textbook Beehive. Students will improve their answer writing skills by referring to the following NCERT Solutions for Class 9 English Beehive. The following NCERT Solutions
14 min read
Stuck in Programming: Get The Solution From These 12 Best Websites
Cheating in programming is acceptable. If you are stuck in your code, google it or try to find the answer from other resources. It's a smart way to learn from each other. Getting stuck in programming is quite normal for all the developers. Most of the beginners and even experienced programmers take
8 min read
Maximizing Readability-Coefficient for Book Printing
A book publisher collected data on the readability level of his n books. Book publisher can print any book in 1 unit of time. Readability-Coefficient of a book is defined as the time taken to print that book including previous books multiplied by its readability level, i.e. total_time * readability[
8 min read