[Solved] Library Time table with Java, C++, Python

Library Time table: With the initiative of the Students Council of Sherland State University, the College Management has inaugurated a mini library in the hostel premises.There are students living in the hostel. Any student can use the library but on a condition that only one student should avail it at a time. Based on this condition, the Hostel Warden came up with a timetable for library’s usage in order to avoid the conflicts:

  • The first student starts to use the library at the time O and should finish the reading not later than at the time A1.
  • The second student starts to use the library at the time A1 and should finish the reading not later than at the time A2.
  • And so on.
  • The N-th student starts to use the library at the time AN-1 and should finish the reading not later than at the time AN

The holidays in Sherland are approaching, so today each of these N students wants to read some new edition of “Heart of Darkness”. The i-th student needs Bi units of time to read the book.
The students have understood that probably not all of them will be able to read everything they want from the book. How many students will be able to read the book without violating the schedule?
 
Input Format:
The first line of input contains a single integer N denoting the number of students. Assume that the maximum value for N as 50.
The second line contains N space-separated integers A1A2, …,AN denoting the moments of time by when the corresponding student should finish reading.
The third line contains N space-separated integers B1B2, …,BN denoting the time required for each of the students to read.

Output Format:
Output a single line containing the number of students that will be able to finish reading.
Refer sample input and output for formatting specifications.

Sample Input 1:
3
1 10 15
1 10 3

Sample Output 1:
2

Sample Input 2:
3
10 20 30
15 5 20

Sample Output 2:
1

Solution

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

class Main
{
public static void main(String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            int count = 0;
            //sauravhathi
            for (int i = 0; i < n; i++)
            {
                a[i] = sc.nextInt();
            }
            for (int i = 0; i < n; i++)
            {
                //sauravhathi
                b[i] = sc.nextInt();
            }
            if (a[0] >= b[0])
            {
                count++;
            }
            for (int i = 0; i < n; i++)
            {
                if (i > 0 && (a[i] - a[i - 1]) >= b[i])
                {
                    count++;
                }
            }
            System.out.println(count);
    }
}
#include <iostream>

using namespace std;

int main()
{
        int n;
        cin >> n;
        long long a[n + 1];
        long long b[n];
        //sauravhathi
        a[0] = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++)
        {

            cin >> b[i];
        }
        int count = 0;
        //sauravhathi
        for (int i = 1; i <= n; i++)
        {

            int x = a[i] - a[i - 1];
            int y = b[i];

            if (x >= y)
            {
                //sauravhathi
                count++;
            }
        }
        cout << count << endl;


    return 0;
}
N = int(input())
arr1 = list(map(int, input().strip().split()))[:N]
arr2 = list(map(int, input().strip().split()))[:N]

count = 0
#sauravhathi
for student in range(0, N):
    if(student == 0):
        if (arr2[student] <= arr1[student]):
            count = count + 1
    else:
        if (arr2[student] <= (arr1[student] - arr1[student-1])):
            count = count + 1

print(count)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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