Given an integer, N, the task is to count the number of ways to generate an array, arr[] of consisting of N integers such that for every index i(1-based indexing), arr[i] is either a factor or a multiple of i, or both. The arr[] must be the permutations of all the numbers from the range [1, N].
Examples:
Input: N=2
Output: 2
Explanation:
Two possible arrangements are {1, 2} and {2, 1}
Input: N=3
Output: 3
Explanation:
The 6 possible arrangements are {1, 2, 3}, {2, 1, 3}, {3, 2, 1}, {3, 1, 2}, {2, 3, 1} and {1, 3, 2}.
Among them, the valid arrangements are {1, 2, 3}, {2, 1, 3} and {3, 2, 1}.
Approach: The problem can be solved using Backtracking technique and the concept of print all permutations using recursion. Follow the steps below to find the recurrence relation:
- Traverse the range [1, N].
- For the current index pos, if i % pos == 0 and i % pos == 0, then insert i into the arrangement and use the concept of Backtracking to find valid permutations.
- Remove i.
- Repeat the above steps for all values in the range [1, N] and finally, print the count of valid permutations.
Below is the implementation of the above approach:
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to find the count of// desired permutationsint findPermutation(unordered_set<int>& arr, int N){ int pos = arr.size() + 1; // Base case if (pos > N) return 1; int res = 0; for (int i = 1; i <= N; i++) { // If i has not been inserted if (arr.find(i) == arr.end()) { // Backtrack if (i % pos == 0 or pos % i == 0) { // Insert i arr.insert(i); // Recur to find valid permutations res += findPermutation(arr, N); // Remove i arr.erase(arr.find(i)); } } } // Return the final count return res;}// Driver Codeint main(){ int N = 5; unordered_set<int> arr; cout << findPermutation(arr, N); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to find the count of// desired permutationsstatic int findPermutation(Set<Integer>arr, int N){ int pos = arr.size() + 1; // Base case if (pos > N) return 1; int res = 0; for(int i = 1; i <= N; i++) { // If i has not been inserted if (! arr.contains(i)) { // Backtrack if (i % pos == 0 || pos % i == 0) { // Insert i arr.add(i); // Recur to find valid permutations res += findPermutation(arr, N); // Remove i arr.remove(i); } } } // Return the final count return res;}// Driver Codepublic static void main(String []args){ int N = 5; Set<Integer> arr = new HashSet<Integer>(); System.out.print(findPermutation(arr, N));}}// This code is contributed by chitranayal |
Python3
# Python3 program to implement# the above approach# Function to find the count of# desired permutationsdef findPermutation(arr, N): pos = len(arr) + 1 # Base case if(pos > N): return 1 res = 0 for i in range(1, N + 1): # If i has not been inserted if(i not in arr): # Backtrack if(i % pos == 0 or pos % i == 0): # Insert i arr.add(i) # Recur to find valid permutations res += findPermutation(arr, N) # Remove i arr.remove(i) # Return the final count return res# Driver CodeN = 5arr = set()# Function callprint(findPermutation(arr, N))# This code is contributed by Shivam Singh |
C#
// C# program to implement// the above approachusing System;using System.Collections.Generic;class GFG{ // Function to find the count of// desired permutationsstatic int findPermutation(HashSet<int>arr, int N){ int pos = arr.Count + 1; // Base case if (pos > N) return 1; int res = 0; for(int i = 1; i <= N; i++) { // If i has not been inserted if (! arr.Contains(i)) { // Backtrack if (i % pos == 0 || pos % i == 0) { // Insert i arr.Add(i); // Recur to find valid permutations res += findPermutation(arr, N); // Remove i arr.Remove(i); } } } // Return the readonly count return res;}// Driver Codepublic static void Main(String []args){ int N = 5; HashSet<int> arr = new HashSet<int>(); Console.Write(findPermutation(arr, N));}}// This code is contributed by gauravrajput1 |
10
Time Complexity: O(N×N!)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of pairs having each element equal to index of the other from an Array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Number of permutations such that sum of elements at odd index and even index are equal
- Maximum subsequence sum possible by multiplying each element by its index
- Form an array of distinct elements with each element as sum of an element from each array
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Generate array having differences between count of occurrences of every array element on its left and right
- Range Query on array whose each element is XOR of index value and previous element
- Count sequences of length K having each term divisible by its preceding term
- Count of right shifts for each array element to be in its sorted position
- Count array elements having at least one smaller element on its left and right side
- Minimum index i such that all the elements from index i to given index are equal
- Count of contiguous subarrays possible for every index by including the element at that index
- Generate Array whose difference of each element with its left yields the given Array
- Minimize count of given operations required to make two given strings permutations of each other
- Count of distinct permutations of length N having no similar adjacent characters
- Sum of product of each element with each element after it
- Node having maximum number of nodes less than its value in its subtree
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Count subarrays having each distinct element occuring at least twice
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

