Given two integers N and M, the task is to find the number of pairs of arrays (A, B) such that array A and B both are of size M each where each entry of A and B is an integer between 1 and N such that for each i between 1 and M, A[i] ≤ B[i]. It is also given that the array A is sorted in non-descending order and B is sorted in non-ascending order. Since the answer can be very large, return answer modulo 109 + 7.
Input: N = 2, M = 2
1: A= [1, 1] B=[1, 1]
2: A= [1, 1] B=[1, 2]
3: A= [1, 1] B=[2, 2]
4: A= [1, 2] B=[2, 2]
5: A= [2, 2] B=[2, 2]
Input: N = 5, M = 3
Approach: Notice that if there is a valid pair of arrays A and B and if B is concatenated after A the resultant array will always be either an ascending or a non-descending array of size of 2 * M. Each element of (A + B) will be between 1 and N (It is not necessary that all elements between 1 and N have to be used). This now simply converts the given problem to finding all the possible combinations of size 2 * M where each element is between 1 to N (with repetitions allowed) whose formula is 2 * M + N – 1CN – 1 or (2 * M + N – 1)! / ((2 * M)! * (N – 1)!).
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sort first half in ascending and second half in descending order | 1
- Sort first half in ascending and second half in descending order | Set 2
- Sort Matrix in alternating ascending and descending order rowwise
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sort first k values in ascending order and remaining n-k values in descending order
- Sort given array to descending-lowest-ascending form
- Rearrange array such that difference of adjacent elements is in descending order
- Number of pairs such that path between pairs has the two vertices A and B
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Sort prime numbers of an array in descending order
- Different ways to sort an array in descending order in C#
- Rank of all elements in a Stream in descending order when they arrive
- Sort an array of string of dates in ascending order
- Sort an array of Roman Numerals in ascending order
- Sort an Array of dates in ascending order using Custom Comparator
- Sort the given IP addresses in ascending order
- Sort an array in descending order based on the sum of its occurrence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.