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++
#include <bits/stdc++.h>
using namespace std;
int arrCount( int arr1[], int arr2[], int N)
{
int M = 1000;
vector<vector< int > > dp(
N + 1,
vector< int >(M + 1, 0));
dp[0][0] = 1;
for ( int i = 0; i <= N; i++) {
for ( int j = 0; j < M; j++) {
dp[i][j + 1] += dp[i][j];
}
if (i != N) {
for ( int j = arr1[i]; j <= arr2[i]; j++)
dp[i + 1][j] += dp[i][j];
}
}
return dp[N][M];
}
int main()
{
int arr1[] = { 1, 1 };
int arr2[] = { 2, 3 };
int N = sizeof (arr1) / sizeof ( int );
cout << arrCount(arr1, arr2, N);
return 0;
}
|
Java
import java.util.*;
public class GFG{
static int arrCount( int [] arr1, int [] arr2, int N)
{
int M = 1000 ;
int [][] dp = new int [N + 1 ][M + 1 ];
dp[ 0 ][ 0 ] = 1 ;
for ( int i = 0 ; i <= N; i++)
{
for ( int j = 0 ; j < M; j++)
{
dp[i][j + 1 ] += dp[i][j];
}
if (i != N)
{
for ( int j = arr1[i]; j <= arr2[i]; j++)
dp[i + 1 ][j] += dp[i][j];
}
}
return dp[N][M];
}
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));
}
}
|
Python3
def arrCount(arr1, arr2, N):
M = 1000
dp = [ 0 ] * (N + 1 )
for i in range ( len (dp)):
dp[i] = [ 0 ] * (M + 1 )
dp[ 0 ][ 0 ] = 1
for i in range (N + 1 ):
for j in range (M):
dp[i][j + 1 ] + = dp[i][j]
if (i ! = N):
for j in range (arr1[i], arr2[i] + 1 ):
dp[i + 1 ][j] + = dp[i][j]
return dp[N][M]
arr1 = [ 1 , 1 ]
arr2 = [ 2 , 3 ]
N = len (arr1)
print (arrCount(arr1, arr2, N))
|
C#
using System;
class GFG{
static int arrCount( int [] arr1, int [] arr2, int N)
{
int M = 1000;
int [,] dp = new int [N + 1, M + 1];
dp[0, 0] = 1;
for ( int i = 0; i <= N; i++)
{
for ( int j = 0; j < M; j++)
{
dp[i, j + 1] += dp[i, j];
}
if (i != N)
{
for ( int j = arr1[i]; j <= arr2[i]; j++)
dp[i + 1, j] += dp[i, j];
}
}
return dp[N, M];
}
public static void Main()
{
int [] arr1 = { 1, 1 };
int [] arr2 = { 2, 3 };
int N = arr1.Length;
Console.WriteLine(arrCount(arr1, arr2, N));
}
}
|
Javascript
<script>
function arrCount(arr1, arr2, N) {
let M = 1000;
let dp = new Array(N + 1);
for (let i = 0; i < dp.length; i++) {
dp[i] = new Array(M + 1).fill(0);
}
dp[0][0] = 1;
for (let i = 0; i <= N; i++) {
for (let j = 0; j < M; j++) {
dp[i][j + 1] += dp[i][j];
}
if (i != N) {
for (let j = arr1[i]; j <= arr2[i]; j++)
dp[i + 1][j] += dp[i][j];
}
}
return dp[N][M];
}
let arr1 = [1, 1];
let arr2 = [2, 3];
let N = arr1.length;
document.write(arrCount(arr1, arr2, N));
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...