
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Count array elements that divide the sum of all other elements in C++
- Maximum sum such that no two elements are adjacent in C++
- Count elements that are divisible by at-least one element in another array in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Count of strings where adjacent characters are of difference one in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- Count all Quadruples from four arrays such that their XOR equals to ‘x’ in C++
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Remove all the elements from an ArrayList that are in another Collection in Java
- Count of all possible values of X such that A % X = B in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Frequency of elements of one array that appear in another array using JavaScript
