Total number Of valid Home delivery arrangements

Given number of orders, find the number of valid arrangements of orders where delivery of ith order is always after the pickup of ith order.Examples:

Input: N = 1
Output: 1
Here total event is 2. They are {P1, D1}.
Total possible arrangement is 2! = 2. [P1, D1] and [D1, P1].
So only valid arrangement possible: [P1, D1].
[D1, P1] is invalid arrangement as delivery of 1st order is done before pickup of 1st order.

Input: N = 2
Output: 6
Here total event is 4. They are {P1, D1, P2, D2}.
Here total possible arrangements are 4! = 24.
Among them 6 are valid arrangements:
[P1, P2, D1, D2], [P1, D1, P2, D2], [P1, P2, D2, D1], [P2, P1, D2, D1], [P2, P1, D1, D2], and [P2, D2, P1, D1].
Rest all are invalid arrangements.
Some invalid arrangements:
[P1, D1, D2, P2] – Delivery of 2nd order is done before pickup
[P2, D1, P1, D2] – Delivery of 1st order is done before pickup
[D1, D2, P2, P1] – Delivery of both order is before pickup

Approach 1:

  1. Consider N = 4, we have total of 8 events.
  2. There are 4 events for pickup {P1, P2, P3, P4} and 4 events for delivery {D1, D2, D3, D4}.
  3. If we consider only pickup events, there are no restrictions in arrangements between pickups. So, total possible arrangements 4!
  4. Now we consider delivery. We start from the last pickup we made.
    • For D4, We can place D4 only after P4.
      That is P1, P2, P3, P4, __. So only 1 valid position.
    • For D3, We can place D3 in any one of this following position.
      They are P1, P2, P3, __, P4, __, D4, __ . So 3 valid position.
    • For D2, We can place D2 in any one of this following position.
      They are P1, P2, __, P3, __, P4, __, D4, __, D3 __ .So 5 valid position.
    • For D1, We can place D1 in any one of this following position.
      They are P1, __, P2, __, P3, __, P4, __, D4, __, D3 __, D2, __ .So, 7 valid positions.

    So total valid arrangements: 4! * (1 * 3 * 5 * 7)

For any N, total valid arrangements:



N! * (1 * 3 * 5 * ... * (2 * N - 1))

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find arrangements
int Arrangements(int N)
{
    int result = 1;
  
    for(int i = 1; i <= N; i++) 
    {
       // Here, i for factorial and
       // (2*i-1) for series 
       result = result * i * (2 * i - 1);
    }
    return result;
}
  
// Driver code
int main()
{
    int N = 4;
  
    cout << Arrangements(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
class GFG{
  
// Function to find arrangements 
public static int Arrangements(int N) 
    int result = 1
      
    for(int i = 1; i <= N; i++)
    
  
        // Here, i for factorial and
        // (2*i-1) for series 
       result = result * i * (2 * i - 1); 
    
    return result; 
  
// Driver code    
public static void main(String[] args)
{
    int N = 4
      
    System.out.print(Arrangements(N));
}
}
  
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
  
# Function to find arrangements
def Arrangements(N):
  
    result = 1
  
    for i in range(1, N + 1):
  
        # Here, i for factorial and
        # (2*i-1) for series 
        result = result * i * (2 * i - 1)
  
    return result
  
# Driver code
N = 4;
print(Arrangements(N));
  
# This code is contributed by Akanksha_Rai

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
  
class GFG{ 
      
// Function to find arrangements 
public static int Arrangements(int N) 
    int result = 1; 
          
    for(int i = 1; i <= N; i++) 
    
  
       // Here, i for factorial and 
       // (2*i-1) for series 
       result = result * i * (2 * i - 1); 
    
    return result; 
      
// Driver code 
public static void Main(String[] args) 
    int N = 4; 
          
    Console.Write(Arrangements(N)); 
  
// This code is contributed by AnkitRai01

chevron_right


Output:

2520

Time complexity: O(N)
Auxiliary Space complexity: O(1)

Approach 2:

  1. For N number of orders, we have

      Total events = 2 * N

  2. So the total number of arrangements possible is  ( 2 * N )!
  3. Now each order can only be valid if delivery is one after pickup.

    For each [Pi, Di], we can’t change this arrangement ie we can’t do [Di, Pi].There is only one valid arrangement for each such order. So we need to divide by 2 for each order. So total valid arrangement is  ( 2 * N )! / 2 ^ N

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find arrangements
int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
int main()
{
    int N = 4;
  
    cout << Arrangements(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
class GFG{
      
// Function to find arrangements
public static int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
public static void main(String args[])
{
    int N = 4;
  
    System.out.print(Arrangements(N));
}
}
  
// This code is contributed by Code_Mech

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
  
# Function to find arrangements
def Arrangements(N):
    result = 1;
  
    for i in range(1, (2 * N) + 1, 2):
        result = (result * i * (i + 1)) / 2;
  
    return int(result);
  
# Driver code
if __name__ == '__main__':
    N = 4;
  
    print(Arrangements(N));
  
# This code is contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
class GFG{
      
// Function to find arrangements
public static int Arrangements(int N)
{
    int result = 1;
  
    for (int i = 1; i <= 2 * N; i += 2)
        result = (result * i * (i + 1)) / 2;
  
    return result;
}
  
// Driver code
public static void Main()
{
    int N = 4;
  
    Console.Write(Arrangements(N));
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

2520

Time complexity: O(N)
Auxiliary Space complexity: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.