# Count of arrays in which all adjacent elements are such that one of them divide the another

Given two positive integer **n** and **n**. The task is to find the number of arrays of size n that can be formed such that :

- Each element is in range [1, m]
- All adjacent element are such that one of them divide the another i.e element A
_{i}divides A_{i + 1}or A_{i + 1}divides A_{i + 2}.

Examples:

Input : n = 3, m = 3. Output : 17 {1,1,1}, {1,1,2}, {1,1,3}, {1,2,1}, {1,2,2}, {1,3,1}, {1,3,3}, {2,1,1}, {2,1,2}, {2,1,3}, {2,2,1}, {2,2,2}, {3,1,1}, {3,1,2}, {3,1,3}, {3,3,1}, {3,3,3} are possible arrays. Input : n = 1, m = 10. Output : 10

We try to find number of possible values at each index of the array. First, at index 0 all values are possible from 1 to m. Now observe for each index, we can reach either to its multiple or its factor. So, precompute that and store it for all the elements. Hence for each position i, ending with integer x, we can go to next position i + 1, with the array ending in integer with multiple of x or factor of x. Also, multiple of x or factor of x must be less than m.

So, we define an 2D array dp[i][j], which is number of possible array (divisible adjacent element) of size i with j as its first index element.

1 <= i <= m, dp[1][m] = 1. for 1 <= j <= m and 2 <= i <= n dp[i][j] = dp[i-1][j] + number of factor of previous element less than m + number of multiples of previous element less than m.

Below is the C++ implementation of this approach:

`// C++ program to count number of arrays ` `// of size n such that every element is ` `// in range [1, m] and adjacen are ` `// divisible ` `#include <bits/stdc++.h> ` `#define MAX 1000 ` `using` `namespace` `std; ` ` ` `int` `numofArray(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `dp[MAX][MAX]; ` ` ` ` ` `// For storing factors. ` ` ` `vector<` `int` `> di[MAX]; ` ` ` ` ` `// For storing multiples. ` ` ` `vector<` `int` `> mu[MAX]; ` ` ` ` ` `memset` `(dp, 0, ` `sizeof` `dp); ` ` ` `memset` `(di, 0, ` `sizeof` `di); ` ` ` `memset` `(mu, 0, ` `sizeof` `mu); ` ` ` ` ` `// calculating the factors and multiples ` ` ` `// of elements [1...m]. ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 2*i; j <= m; j += i) ` ` ` `{ ` ` ` `di[j].push_back(i); ` ` ` `mu[i].push_back(j); ` ` ` `} ` ` ` `di[i].push_back(i); ` ` ` `} ` ` ` ` ` `// Initalising for size 1 array for ` ` ` `// each i <= m. ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `dp[1][i] = 1; ` ` ` ` ` `// Calculating the number of array possible ` ` ` `// of size i and starting with j. ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 1; j <= m; j++) ` ` ` `{ ` ` ` `dp[i][j] = 0; ` ` ` ` ` `// For all previous possible values. ` ` ` `// Adding number of factors. ` ` ` `for` `(` `auto` `x:di[j]) ` ` ` `dp[i][j] += dp[i-1][x]; ` ` ` ` ` `// Adding number of multiple. ` ` ` `for` `(` `auto` `x:mu[j]) ` ` ` `dp[i][j] += dp[i-1][x]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Calculating the total count of array ` ` ` `// which start from [1...m]. ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `{ ` ` ` `ans += dp[n][i]; ` ` ` `di[i].clear(); ` ` ` `mu[i].clear(); ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 3, m = 3; ` ` ` `cout << numofArray(n, m) << ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

17

**Time Complexity: **O(N*M).

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count array elements that divide the sum of all other elements
- Count elements which divide all numbers in range L-R
- Count sub-arrays which have elements less than or equal to X
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Divide array into two sub-arrays such that their averages are equal
- Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach )
- Maximum sum such that no two elements are adjacent
- Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )
- Distinct adjacent elements in an array
- Sort 1 to N by swapping adjacent elements
- Maximum set bit sum in array without considering adjacent elements
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Count of strings where adjacent characters are of difference one
- Maximum sum in circular array such that no two elements are adjacent
- Count ways to divide circle using N non-intersecting chords