 Open in App
Not now

# Count of seats booked on each of the given N flights

• Difficulty Level : Hard
• Last Updated : 23 Jul, 2021

Given an integer N denoting the number of flights and array bookings[] with each row of the form {a, b, K} denoting that there are K seats booked from flight a to flight b ( consider 1-based indexing), the task is to find the sequence of the number of seats booked in N flights.

Examples:

Input: bookings[][] = {{1, 2, 10}, {2, 3, 20}, {2, 5, 25}}
Output: 10 55 45 25 25
Explanation:
Initially, there are no seats booked in any of the flights. So the resultant sequence is {0, 0, 0, 0, 0}
Book 10 seats in flights 1 to 2. The sequence becomes {10, 10, 0, 0, 0}.
Book 20 seats in flights 2 to 3. The sequence becomes {10, 30, 20, 0, 0}.
Book 25 seats in flights 2 to 5. The sequence becomes {10, 55, 45, 25, 25}.

Input: bookings[][] = {{1, 3, 100}, {2, 6, 100}, {3, 4, 100}}
Output: 100 200 300 200 100 100

Naive Approach: Follow the steps below to solve the problem in simplest approach possible:

• Initialize an array seq[] of size N to store the count of allocated seats in each flight.
• Traverse the array bookings[].
• For each query {a, b, K}, add the element K in the array seq[] from the index a to b.
• After completing the above steps, print the array seq[] as the result.

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: To optimize the above approach, the idea is to use the concept of Prefix Sum. Follow the steps below to solve the problem:

• Initialize an array seq[] of size (N + 2) to store all the allocated seats.
• Traverse the array bookings[].
• For each query {a, b, K}, increment the array seq[] at index (a – 1) by K and decrement the element at index (b + 1) by K.
• For the resulting sequence, find the prefix sum of the array seq[].
• After completing the above steps, print the array seq[] as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the total of the``// seats booked in each of the flights``void` `corpFlightBookings(``    ``vector >& Bookings,``    ``int` `N)``{``    ``// Stores the resultant sequence``    ``vector<``int``> res(N, 0);` `    ``// Traverse the array``    ``for` `(``int` `i = 0;``         ``i < Bookings.size(); i++) {` `        ``// Store the first booked flight``        ``int` `l = Bookings[i];` `        ``// Store the last booked flight``        ``int` `r = Bookings[i];` `        ``// Store the total number of``        ``// seats booked in flights [l, r]``        ``int` `K = Bookings[i];` `        ``// Add K to the flight L``        ``res[l - 1] = res[l - 1] + K;` `        ``// Subtract K from flight``        ``// number R + 1``        ``if` `(r <= res.size() - 1)``            ``res[r] = (-K) + res[r];``    ``}` `    ``// Find the prefix sum of the array``    ``for` `(``int` `i = 1; i < res.size(); i++)``        ``res[i] = res[i] + res[i - 1];` `    ``// Print the total number of seats``    ``// booked in each flight``    ``for` `(``int` `i = 0; i < res.size(); i++) {``        ``cout << res[i] << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given list of bookings``    ``vector > bookings{ { 1, 3, 100 },``                                   ``{ 2, 6, 100 },``                                   ``{ 3, 4, 100 } };``    ``int` `N = 6;` `    ``// Function Call``    ``corpFlightBookings(bookings, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG``{` `// Function to find the total of the``// seats booked in each of the flights``static` `void` `corpFlightBookings(``int` `[][]Bookings,``                               ``int` `N)``{``  ` `    ``// Stores the resultant sequence``    ``int` `res[] = ``new` `int``[N];` `    ``// Traverse the array``    ``for` `(``int` `i = ``0``;``         ``i < Bookings.length; i++)``    ``{` `        ``// Store the first booked flight``        ``int` `l = Bookings[i][``0``];` `        ``// Store the last booked flight``        ``int` `r = Bookings[i][``1``];` `        ``// Store the total number of``        ``// seats booked in flights [l, r]``        ``int` `K = Bookings[i][``2``];` `        ``// Add K to the flight L``        ``res[l - ``1``] = res[l - ``1``] + K;` `        ``// Subtract K from flight``        ``// number R + 1``        ``if` `(r <= res.length - ``1``)``            ``res[r] = (-K) + res[r];``    ``}` `    ``// Find the prefix sum of the array``    ``for` `(``int` `i = ``1``; i < res.length; i++)``        ``res[i] = res[i] + res[i - ``1``];` `    ``// Print the total number of seats``    ``// booked in each flight``    ``for` `(``int` `i = ``0``; i < res.length; i++)``    ``{``        ``System.out.print(res[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ` `    ``// Given list of bookings``    ``int` `bookings[][] = { { ``1``, ``3``, ``100` `},``                        ``{ ``2``, ``6``, ``100` `},``                        ``{ ``3``, ``4``, ``100` `} };``    ``int` `N = ``6``;` `    ``// Function Call``    ``corpFlightBookings(bookings, N);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program for the above approach` `# Function to find the total of the``# seats booked in each of the flights``def` `corpFlightBookings(Bookings, N):``  ` `    ``# Stores the resultant sequence``    ``res ``=` `[``0``] ``*` `N` `    ``# Traverse the array``    ``for` `i ``in` `range``(``len``(Bookings)):` `        ``# Store the first booked flight``        ``l ``=` `Bookings[i][``0``]` `        ``# Store the last booked flight``        ``r ``=` `Bookings[i][``1``]` `        ``# Store the total number of``        ``# seats booked in flights [l, r]``        ``K ``=` `Bookings[i][``2``]` `        ``# Add K to the flight L``        ``res[l ``-` `1``] ``=` `res[l ``-` `1``] ``+` `K` `        ``# Subtract K from flight``        ``# number R + 1``        ``if` `(r <``=` `len``(res) ``-` `1``):``            ``res[r] ``=` `(``-``K) ``+` `res[r]` `    ``# Find the prefix sum of the array``    ``for` `i ``in` `range``(``1``, ``len``(res)):``        ``res[i] ``=` `res[i] ``+` `res[i ``-` `1``]` `    ``# Print total number of seats``    ``# booked in each flight``    ``for` `i ``in` `range``(``len``(res)):``        ``print``(res[i], end ``=` `" "``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `    ``# Given list of bookings``    ``bookings ``=` `[ [ ``1``, ``3``, ``100` `],``               ``[ ``2``, ``6``, ``100` `],``               ``[ ``3``, ``4``, ``100` `] ]``    ``N ``=` `6` `    ``# Function Call``    ``corpFlightBookings(bookings, N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{` `// Function to find the total of the``// seats booked in each of the flights``static` `void` `corpFlightBookings(``int` `[,]Bookings,``                               ``int` `N)``{``  ` `    ``// Stores the resultant sequence``    ``int` `[]res = ``new` `int``[N];` `    ``// Traverse the array``    ``for` `(``int` `i = 0;``         ``i < Bookings.GetLength(0); i++)``    ``{` `        ``// Store the first booked flight``        ``int` `l = Bookings[i,0];` `        ``// Store the last booked flight``        ``int` `r = Bookings[i,1];` `        ``// Store the total number of``        ``// seats booked in flights [l, r]``        ``int` `K = Bookings[i,2];` `        ``// Add K to the flight L``        ``res[l - 1] = res[l - 1] + K;` `        ``// Subtract K from flight``        ``// number R + 1``        ``if` `(r <= res.Length - 1)``            ``res[r] = (-K) + res[r];``    ``}` `    ``// Find the prefix sum of the array``    ``for` `(``int` `i = 1; i < res.Length; i++)``        ``res[i] = res[i] + res[i - 1];` `    ``// Print the total number of seats``    ``// booked in each flight``    ``for` `(``int` `i = 0; i < res.Length; i++)``    ``{``        ``Console.Write(res[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ` `    ``// Given list of bookings``    ``int` `[,]bookings = { { 1, 3, 100 },``                        ``{ 2, 6, 100 },``                        ``{ 3, 4, 100 } };``    ``int` `N = 6;` `    ``// Function Call``    ``corpFlightBookings(bookings, N);``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output:

`100 200 300 200 100 100`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up