Open in App
Not now

# Count of non-decreasing Arrays arr3[] such that arr1[i] <= arr3[i] <= arr2[i]

• Last Updated : 16 Dec, 2021

Given two arrays arr1[] and arr2[] having N integers in non-decreasing order, the task is to find the count of non-decreasing arrays arr3[] of length N such that arr1[i] <= arr3[i] <= arr2[i] for all values of i in range [0, N).

Examples:

Input: arr1[] = {1, 1}, arr2[] = {2, 3}
Output: 5
Explanation: The 5 possible arrays that follow the required conditions are {1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}

Input: ranges[] = {{-12, 15}, {3, 9}, {-5, -2}, {20, 25}, {16, 20}}
Output: 247

Approach: The given problem can be solved using Dynamic Programming. Consider a 2D array dp[][] such that dp[i][j] represents the count of arrays of length i such that the ith element is j. Initialize all the elements of the dp array as 0 and dp[0][0] as 1. Upon observation, the DP relation of the above problem can be stated as follows:

dp[i][j] =

Therefore, using the above relation, calculate the value of dp[i][j] for each i in the range [0, N] and for each j in the range [0, M] where M represents the maximum integer in both the given arrays arr1[] and arr2[]. Hence, the value stored in dp[N][M] is the required answer.

Below is the implementation of the above approach:

## C++

 // C++ Program of the above approach #include  using namespace std;   // Function to find the count of // valid sorted arrays int arrCount(int arr1[], int arr2[], int N) {       // Maximum possible value     // of arr1 and arr2     int M = 1000;       // Stores the dp states     vector > dp(         N + 1,         vector<int>(M + 1, 0));       // Initial condition     dp[0][0] = 1;       // Loop to iterate over range [0, N]     for (int i = 0; i <= N; i++) {           // Loop to iterate over         // the range [0, M]         for (int j = 0; j < M; j++) {             dp[i][j + 1] += dp[i][j];         }           // If current index is not         // the final index         if (i != N) {               // Loop to iterate in the             // range [arr1[i], arr2[i]]             for (int j = arr1[i]; j <= arr2[i]; j++)                 dp[i + 1][j] += dp[i][j];         }     }       // Return Answer     return dp[N][M]; }   // Driver Code int main() {     int arr1[] = { 1, 1 };     int arr2[] = { 2, 3 };     int N = sizeof(arr1) / sizeof(int);       cout << arrCount(arr1, arr2, N);       return 0; }

## Java

 // Java Program of the above approach import java.util.*;   public class GFG{       // Function to find the count of // valid sorted arrays static int arrCount(int[] arr1, int[] arr2, int N) {           // Maximum possible value     // of arr1 and arr2     int M = 1000;       // Stores the dp states     int[][] dp = new int[N + 1][M + 1];       // Initial condition     dp[0][0] = 1;       // Loop to iterate over range [0, N]     for(int i = 0; i <= N; i++)      {                   // Loop to iterate over         // the range [0, M]         for(int j = 0; j < M; j++)         {             dp[i][j + 1] += dp[i][j];         }           // If current index is not         // the final index         if (i != N)          {                           // Loop to iterate in the             // range [arr1[i], arr2[i]]             for(int j = arr1[i]; j <= arr2[i]; j++)                 dp[i + 1][j] += dp[i][j];         }     }       // Return Answer     return dp[N][M]; }   // Driver Code public static void main(String args[]) {     int[] arr1 = { 1, 1 };     int[] arr2 = { 2, 3 };     int N = arr1.length;       System.out.println(arrCount(arr1, arr2, N)); } }   // This code is contributed by Samim Hossain Mondal.

## Python3

 # Python Program to implement # the above approach   # Function to find the count of # valid sorted arrays def arrCount(arr1, arr2, N):       # Maximum possible value     # of arr1 and arr2     M = 1000       # Stores the dp states     dp = [0] * (N + 1)     for i in range(len(dp)):         dp[i] = [0] * (M + 1)       # Initial condition     dp[0][0] = 1       # Loop to iterate over range [0, N]     for i in range(N + 1):           # Loop to iterate over         # the range [0, M]         for j in range(M):             dp[i][j + 1] += dp[i][j]           # If current index is not         # the final index         if (i != N):               # Loop to iterate in the             # range [arr1[i], arr2[i]]             for j in range(arr1[i], arr2[i] + 1):                 dp[i + 1][j] += dp[i][j]       # Return Answer     return dp[N][M]   # Driver Code arr1 = [1, 1] arr2 = [2, 3] N = len(arr1)   print(arrCount(arr1, arr2, N))   # This code is contributed by Saurabh Jaiswal

## C#

 // C# Program of the above approach using System;   class GFG{       // Function to find the count of // valid sorted arrays static int arrCount(int[] arr1, int[] arr2, int N) {           // Maximum possible value     // of arr1 and arr2     int M = 1000;       // Stores the dp states     int[,] dp = new int[N + 1, M + 1];       // Initial condition     dp[0, 0] = 1;       // Loop to iterate over range [0, N]     for(int i = 0; i <= N; i++)      {                   // Loop to iterate over         // the range [0, M]         for(int j = 0; j < M; j++)         {             dp[i, j + 1] += dp[i, j];         }           // If current index is not         // the final index         if (i != N)          {                           // Loop to iterate in the             // range [arr1[i], arr2[i]]             for(int j = arr1[i]; j <= arr2[i]; j++)                 dp[i + 1, j] += dp[i, j];         }     }       // Return Answer     return dp[N, M]; }   // Driver Code public static void Main() {     int[] arr1 = { 1, 1 };     int[] arr2 = { 2, 3 };     int N = arr1.Length;       Console.WriteLine(arrCount(arr1, arr2, N)); } }   // This code is contributed by ukasp

## Javascript

 

Output

5

Time Complexity: O(N * M), where M represents the maximum value of the integers in the array arr1[] and arr2[].
Auxiliary Space: O(N * M)

My Personal Notes arrow_drop_up
Related Articles
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}