Number of M-length sorted arrays that can be formed using first N natural numbers
Given two numbers N and M, the task is to find the number of sorted arrays that can be formed of size M using first N natural numbers, if each number can be taken any number of times.
Examples:
Input: N = 4, M = 2
Output: 10
Explanation: All such possible arrays are {1, 1}, {1, 2}, {1, 2}, {1, 4}, {2, 2}, {2, 3}, {2, 4}, {3, 3}, {3, 4}, {4, 4}.Input: N = 2, M = 4
Output: 5
Explanation: All such possible arrays are {1, 1, 1, 1}, {1, 1, 1, 2}, {1, 1, 2, 2}, {1, 2, 2, 2}, {2, 2, 2, 2}.
Naive Approach: There are two choices for each number that it can be taken or can be left. Also, a number can be taken multiple times.
- Elements that are taken multiple times should be consecutive inthe array as the array should be sorted.
- If an element is left and has moved to another element then that element can not be taken again.
Recursive Approach:
The left branch is indicating that the element is taken and the right branch indicating that the element is left and the pointer moved to the next element.
Below is the implementation of the above approach:
C++
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Recursive Approach with optimization:
- Traverse through each element and try to find all possible arrays starting from that element.
- In the previous approach for the right branch, the element is left first and in the next step, shifted to the next element.
- In this approach, instead of leaving the element first and then moving to the next element, directly go to the next element, so there will be fewer function calls.
Below is the implementation of the above approach:
C++
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Dynamic Programming Approach: It can be observed that this problem has overlapping subproblems and optimal substructure property, i.e, it satisfies both properties of dynamic programming. So, the idea is to use a 2D table to memoize the results during the function calls.
Below is the implementation of the above approach:
C++
|
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Space Optimized Iterative Dynamic Programming Approach:
- As all elements are available as many times as needed, so there is no need to save values for previous rows, the values from the same row can be used.
- So a 1-D array can be used to save previous results.
- Create an array, dp of size M, where dp[i] stores the maximum number of sorted arrays of size i that can be formed from numbers in the range [1, N].
Below is the implementation of the above approach:
C++
|
Time Complexity: O(N*M)
Auxiliary Space: O(M)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment