Open In App

CSES Solutions – Reading Books

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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))

Output
23

Time Complexity: O(N * logN), where N is the number of books to read.
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads