Given a numerical string **S**, the task is to print all the permutations of the string which are divisible by **N**.

**Examples:**

Input:N = 5, S = “125”Output:125 215Explanation:

All possible permutations are S are {125, 152, 215, 251, 521, 512}.

Out of these 6 permutations, only 2 {125, 215} are divisible by N (= 5).

Input:N = 7, S = “4321”Output:4312 4123 3241

**Approach:** The idea is to generate all possible permutations and for each permutation, check if it is divisible by **N** or not. For each permutation found to be divisible by **N, **print them.

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 Swap two ` `// characters ` `void` `swap_(` `char` `& a, ` `char` `& b) ` `{ ` ` ` `char` `temp; ` ` ` `temp = a; ` ` ` `a = b; ` ` ` `b = temp; ` `} ` ` ` `// Function to generate all permutations ` `// and print the ones that are ` `// divisible by the N ` `void` `permute(` `char` `* str, ` `int` `l, ` `int` `r, ` `int` `n) ` `{ ` ` ` `int` `i; ` ` ` ` ` `if` `(l == r) { ` ` ` ` ` `// Convert string to integer ` ` ` `int` `j = ` `atoi` `(str); ` ` ` ` ` `// Check for divisibility ` ` ` `// and print it ` ` ` `if` `(j % n == 0) ` ` ` `cout << str << endl; ` ` ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print all the permutations ` ` ` `for` `(i = l; i < r; i++) { ` ` ` ` ` `// Swap characters ` ` ` `swap_(str[l], str[i]); ` ` ` ` ` `// Permute remaining ` ` ` `// characters ` ` ` `permute(str, l + 1, r, n); ` ` ` ` ` `// Revoke the swaps ` ` ` `swap_(str[l], str[i]); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `char` `str[100] = ` `"125"` `; ` ` ` `int` `n = 5; ` ` ` `int` `len = ` `strlen` `(str); ` ` ` ` ` `if` `(len > 0) ` ` ` `permute(str, 0, len, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `class` `GFG{ ` ` ` `// Function to Swap two ` `// characters ` `static` `void` `swap_(` `char` `[]a, ` `int` `l, ` `int` `i) ` `{ ` ` ` `char` `temp; ` ` ` `temp = a[l]; ` ` ` `a[l] = a[i]; ` ` ` `a[i] = temp; ` `} ` ` ` `// Function to generate all permutations ` `// and print the ones that are ` `// divisible by the N ` `static` `void` `permute(` `char` `[] str, ` `int` `l, ` ` ` `int` `r, ` `int` `n) ` `{ ` ` ` `int` `i; ` ` ` ` ` `if` `(l == r) ` ` ` `{ ` ` ` ` ` `// Convert String to integer ` ` ` `int` `j = Integer.valueOf(String.valueOf(str)); ` ` ` ` ` `// Check for divisibility ` ` ` `// and print it ` ` ` `if` `(j % n == ` `0` `) ` ` ` `System.out.print(String.valueOf(str) + ` `"\n"` `); ` ` ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print all the permutations ` ` ` `for` `(i = l; i < r; i++) ` ` ` `{ ` ` ` ` ` `// Swap characters ` ` ` `swap_(str, l, i); ` ` ` ` ` `// Permute remaining ` ` ` `// characters ` ` ` `permute(str, l + ` `1` `, r, n); ` ` ` ` ` `// Revoke the swaps ` ` ` `swap_(str, l, i); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"125"` `; ` ` ` `int` `n = ` `5` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `if` `(len > ` `0` `) ` ` ` `permute(str.toCharArray(), ` `0` `, len, n); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

125 215

**Time Complexity: **O(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:

- Generate all cyclic permutations of a number
- Generate all permutations of a string that follow given constraints
- Smallest number containing all possible N length permutations using digits 0 to D
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Generate permutations with only adjacent swaps allowed
- Iterative program to generate distinct Permutations of a String
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Program to generate all possible valid IP addresses from given string
- Program to generate all possible valid IP addresses from given string | Set 2
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Possible to make a divisible by 3 number using all digits in an array
- Smallest N digit number divisible by all possible prime digits
- Print the two possible permutations from a given sequence
- Possible permutations at a railway track
- Count of distinct permutations of every possible length of given string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Print all permutations of a number N greater than itself
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]

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.