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


Given two integers named ‘one’ and ‘another’. The goal is to find the number of possible arrays such that −

  • The elements in the array are in range between 1 and ‘another’.

  • All elements of array are such that arr[i] divides arr[i+1] or arr[i+1] divides arr[i+2]....and so on.

  • The length of the array is ‘one’.

For Example

Input

one = 3, another = 2

Output

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

Explanation

The arrays will be:
[ 1,1,1 ], [ 1,1,2 ], [ 1,2,1 ], [ 1,2,2 ], [ 2,1,1 ], [ 2,1,2 ], [ 2,2,1 ], [ 2,2,2 ]

Input

one = 2, another = 3

Output

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

Explanation

The arrays will be:
[ 1,1 ], [ 2,2 ], [ 3,3 ], [ 1,2 ], [ 1,3 ], [ 2,2 ], [ 3,3 ]

Approach used in the below program is as follows

We know that the first element of each array can be any number in the range [1,another]. The next element will always be multiple of previous or factor of previous so that the divisibility condition holds. Also the factor or multiple should be less than ‘another’. We will use dynamic programming to store computations. For this we use array arr[][].arr[1][another] will be 1 as they would be single element arrays. arr[i][j]=arr[i−1][j]+factors of previous element+multiples of previous element (less than another ).

  • Take integers one and another as input.

  • Function adjacent_elements(int first, int second) returns the count of arrays in which all adjacent elements are such that one of them divides the other.

  • Take initial count as 0 and array arr[size][size].

  • Initialize all counts as 0 using memset.

  • Take two vectors − vec for storing factors and vec_2 for storing multiples.

  • Traverse using two for loops from i=t to i=second and j=2*i to j=second. Increment j by i.

  • Add i to vec[j] and j to vec_2[i]. After j loop also add i to vec[i].

  • Set arr[1][i] using for loop.

  • Traverse array again and now traverse vec and vec_2 and add factors and multiples to arr[i][j].

  • At the end add all arr[first][i] to count and clear vec[i] and vec_2[i].

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
#define size 1000
int adjacent_elements(int first, int second){
   int count = 0;
   int arr[size][size];
   memset(arr, 0, sizeof arr);
   vector<int> vec[size], vec_2[size];
   memset(vec, 0, sizeof vec);
   memset(vec_2, 0, sizeof vec_2);
   for (int i = 1; i <= second; i++){
      for (int j = 2*i; j <= second; j += i){
         vec[j].push_back(i);
         vec_2[i].push_back(j);
      }
      vec[i].push_back(i);
   }
   for (int i = 1; i <= second; i++){
      arr[1][i] = 1;
   }
   for (int i = 2; i <= first; i++){
      for (int j = 1; j <= second; j++){
         arr[i][j] = 0;
         for (auto it: vec[j]){
            arr[i][j] += arr[i−1][it];
         }
         for (auto it : vec_2[j]){
            arr[i][j] += arr[i−1][it];
         }
      }
   }
   for (int i = 1; i <= second; i++){
      count = count + arr[first][i];
      vec[i].clear();
      vec_2[i].clear();
   }
   return count;
}
int main(){
   int one = 2, another = 2;
   cout<<"Count of arrays in which all adjacent elements are such that one of them divide the another are: "<<adjacent_elements(one, another);
   return 0;
}

Output

If we run the above code it will generate the following output −

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

Updated on: 05-Jan-2021

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements