CSES Solutions Restaurant Customers (original) (raw)

Last Updated : 23 Jul, 2025

You are given the arrival and leaving times of **N customers in a restaurant as array **customers[][], such that customer[i][0] is the arrival time and customer[i][1] is the leaving time. What was the **maximum number of customers in the restaurant at any time? You may assume that all arrival and leaving times are distinct.

**Note: A customer is not counted to be inside the restaurant at any time >= leaving time and < arrival time.

**Examples:

**Input: N = 3, customer[][] = {{5, 8}, {2, 4}, {3, 9}}
**Output: 2
**Explanation: Two customers are present in the restaurant at time = 3, 5, 6 and 7.

**Input: N = 4, customer[][] = {{1, 2}, {2, 3}, {3, 5}, {4, 5}}
**Output: 2
**Explanation: Two customers are present in the restaurant at time = 4.

**Approach: To solve the problem, follow the below idea:

The problem can be solved by **sorting both the **arrival times and the **departure times separately in **ascending order. Now, we can maintain **two pointers to mark the arrival and departure of customers. We can start from the first index of both the arrays and compare the arrival and departure, if the arrival time is lesser so increment the number of customers and move to the next arrival time. Otherwise, decrement the number of customers and move to the next departure time. After iterating over both the arrays, return the maximum customers at any point of time.

**Step-by-step algorithm:

Below is the implementation of the algorithm:

C++ `

#include <bits/stdc++.h> using namespace std;

int solve(vector<vector >& customers, int N) { // Store the arrival and departure time in two different // arrays int arr[N], dep[N]; for (int i = 0; i < N; i++) { arr[i] = customers[i][0]; dep[i] = customers[i][1]; }

// Sort the arrival and departure time in ascending
// order
sort(arr, arr + N);
sort(dep, dep + N);

int i = 0, j = 0;
// Variables to store the number of customers in the
// shop and the maximum customers so far
int currentCustomers = 0, maxCustomers = 0;
while (i < N && j < N) {
    // If the arrival time is less than the departure
    // time
    if (arr[i] < dep[j]) {
        currentCustomers++;
        maxCustomers
            = max(maxCustomers, currentCustomers);
        i++;
    }
    // If the arrival time is greater than the departure
    // time
    else {
        j++;
        currentCustomers--;
    }
}
return maxCustomers;

}

int main() { // Sample Input int N = 3; vector<vector > customers = { { 5, 8 }, { 2, 4 }, { 3, 9 } };

cout << solve(customers, N) << endl;

}

Java

import java.util.Arrays;

public class Main { static int solve(int[][] customers, int N) { // Store the arrival and departure time in two // different arrays int[] arr = new int[N]; int[] dep = new int[N]; for (int i = 0; i < N; i++) { arr[i] = customers[i][0]; dep[i] = customers[i][1]; }

    // Sort the arrival and departure time in ascending
    // order
    Arrays.sort(arr);
    Arrays.sort(dep);

    int i = 0, j = 0;
    // Variables to store the number of customers in the
    // shop and the maximum customers so far
    int currentCustomers = 0, maxCustomers = 0;
    while (i < N && j < N) {
        // If the arrival time is less than or equal to
        // the departure time
        if (arr[i] <= dep[j]) {
            currentCustomers++;
            maxCustomers = Math.max(maxCustomers,
                                    currentCustomers);
            i++;
        }
        else {
            j++;
            currentCustomers--;
        }
    }
    return maxCustomers;
}

public static void main(String[] args)
{
    // Sample Input
    int N = 3;
    int[][] customers
        = { { 5, 8 }, { 2, 4 }, { 3, 9 } };

    // Call the solve method and print the result
    System.out.println(solve(customers, N));
}

}

Python

def solve(customers): # Create separate lists for arrival and departure times arrival = sorted([i[0] for i in customers]) departure = sorted([i[1] for i in customers])

n = len(arrival)
i = 0
j = 0
current_customers = 0
max_customers = 0

# Traverse both arrays and update max_customers
while i < n and j < n:
    # Customer arrives before or exactly at the departure of 
    # the previous customer
    if arrival[i] <= departure[j]:
        current_customers += 1
        i += 1
    # Customer arrives after the departure of the previous customer
    else:
        j += 1
        current_customers -= 1
    max_customers = max(max_customers, current_customers)

return max_customers

Sample Input

customers = [[5, 8], [2, 4], [3, 9]] print(solve(customers))

JavaScript

function solve(customers, N) { // Store the arrival and departure time in two different arrays let arr = new Array(N); let dep = new Array(N); for (let i = 0; i < N; i++) { arr[i] = customers[i][0]; dep[i] = customers[i][1]; }

// Sort the arrival and departure time in ascending order
arr.sort((a, b) => a - b);
dep.sort((a, b) => a - b);

let i = 0, j = 0;
// Variables to store the number of customers in the shop and the maximum customers so far
let currentCustomers = 0, maxCustomers = 0;
while (i < N && j < N) {
    // If the arrival time is less than the departure time
    if (arr[i] < dep[j]) {
        currentCustomers++;
        maxCustomers = Math.max(maxCustomers, currentCustomers);
        i++;
    }
    // If the arrival time is greater than the departure time
    else {
        j++;
        currentCustomers--;
    }
}
return maxCustomers;

}

// Sample Input let N = 4; let customers = [ [ 1, 2 ], [ 2, 3 ], [ 3, 5 ], [ 4, 5 ] ];

console.log(solve(customers, N));

`

**Time Complexity: O(N * logN), where **N is the size of input array **customers[].
**Auxiliary Space: O(N)