Saturday, May 6, 2017

#codingexercise
Find count of all sets in which adjacent elements are such that one of them divides the other. We are given n as the size of the set required and elements can range in value from 1 to m. 
We know the adjacencies are possible only when the numbers are factors or multiples. Therefore the valid combinations must be as many as possible with each of the factors or multiples.
1 <= i <= m, dp[1, m] = 1. 
For 2 <= i <= n 
   for 1 <= j <= m and 
  dp[i,j] = 0 + 
                 dp-value of the previous row in the dp-matrix for column  equal to each of the factors of j  + 
                dp-value of the previous row in the dp-matrix for column equal to each of the multiples of j 
For m = 3 and n = 3 we get 
Dp-matrix as 
1                  1               1 
0+1+2   0+2+0     0+2+0 
0+3+4   0+5+0     0+5+0 
For factors of 1,2,3 as    1 and 1,2,  and 1,2,3 respectively 
And multiples of 1,2,3 as 2,3 and 0 and 0 respectively 
This gives a total of 17 values as the sum of the last row to indicate the number of set with elements satisfying the adjacency requirement.

No comments:

Post a Comment