5

Count the number of arrays formed by rearranging elements such that one adjacent...

 9 months ago
source link: https://www.geeksforgeeks.org/count-the-number-of-arrays-formed-by-rearranging-elements-such-that-one-adjacent-element-is-multiple-of-other/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Count the number of arrays formed by rearranging elements such that one adjacent element is multiple of other

Discuss
Courses
Practice

Given array A[] of size N (1 <= N <= 15), count all possible arrays formed by rearranging elements of A[] such that at least one of the following conditions is true for adjacent elements of that array:

  • A[i] % A[i + 1] == 0
  • A[i + 1] % A[i] == 0

The count can be a large, modulo answer with 109+ 7 before printing.

Examples:

Input: A[] = {2, 3, 6}
Output: 2
Explanation: {3, 6, 2} and {2, 6, 3} are two possible rearrangement of array A[] such that one of the required conditions satisfy for adjacent elements.

Input: A[] = {1, 4, 3}
Output: 2
Explanation: {3, 1, 4} and {4, 1, 3} are two possible rearrangement of array A[] such that one of the required conditions satisfy for adjacent elements.

Approach: Implement the idea below to solve the problem

Bitmask Dynamic Programming can be used to solve this problem. The main concept of DP in the problem will be:

DP[i][j] represents count of arrangements which satisfy above conditions where i represents subset in form of bitmask and j represents last element of this subset so to check divisibility conditions when new element will be added in subset i.

Transition:

dp[i | (1 << (k – 1))][k] = (dp[i | (1 << (k – 1))][k] + dp[i][j])

here k is kth element of array A[] being added at the end of the subset i whose last element is j’th of array A[]

Steps were taken to solve the problem:

  • Declaring DP[1 << N][N + 1] array.
  • Initializing the base case by iterating over 0 to N – 1 as i, then set dp[1 << i][i + 1] = 1
  • Iterating over all subsets from 1 to 2Nas i and for each iteration
    • Iterate from 1 to N as j if the j’th bit of subset i is not set then it means that jth element is not considered in the current submask, so we skip that iteration.
    • Else, Iterate from 1 to N as k and for each iteration and check if the k’th bit is not in the current submask and last element j and k satisfy required condition, then update dp table according to transitions: dp[i | (1 << (k – 1))][k] = (dp[i | (1 << (k – 1))][k] + dp[i][j])
  • After iterating over all the submasks, add dp[(1 << N) – 1][i] which represents count of arrangement of size N with last element i which satisfies the above conditions.
  • Return ans.

Code to implement the approach:

// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// mod
const int mod = 1e9 + 7;
// Function to Count the number of arrays formed
// by rearranging given array which satisfy following
// condition
int countOfArr(int A[], int N)
{
// DP array initalized with 0
vector<vector<int> > dp((1 << (N + 1)),
vector<int>(N + 2, 0));
// base case
for (int i = 0; i < N; i++)
dp[1 << i][i + 1] = 1;
// iterating over all subsets
for (int i = 1; i < (1 << N); i++) {
// if last element was picked was j from array A[]
for (int j = 1; j <= N; j++) {
// if j'th element not present in current subset
// i skip the iteration
if (((1 << (j - 1)) & i) == 0)
continue;
// tring to choose k'th element from array A[]
for (int k = 1; k <= N; k++) {
// if k'th element of A is not visited and
// last element j and current element k
// satisfy the required condition update dp
// according to transitions
if (((1 << (k - 1)) & i) == 0
and (A[j - 1] % A[k - 1] == 0
or A[k - 1] % A[j - 1] == 0)) {
dp[i | (1 << (k - 1))][k]
= (dp[i | (1 << (k - 1))][k]
+ dp[i][j])
% mod;
}
}
}
}
// variable to count all permutaions
int ans = 0;
// accumulating all arrangements of size N and last
// element i
for (int i = 1; i <= N; i++) {
ans = (ans + dp[(1 << N) - 1][i]) % mod;
}
// return the total count
return ans;
}
// Driver Code
int32_t main()
{
// Input
int N = 3;
int A[] = { 2, 3, 6 };
// Function Call
cout << countOfArr(A, N) << endl;
return 0;
}
Output
2

Time Complexity: O(N2* 2N), where N is the size of input array A[].
Auxiliary Space: O(N * 2N)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Recommended Problems
Frequently asked DSA Problems
Last Updated : 13 Dec, 2023
Like Article
Save Article

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK