Generate all possible permutations of a Number divisible by N
Last Updated :
12 Jun, 2021
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 215
Explanation:
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++14
#include <bits/stdc++.h>
using namespace std;
void swap_( char & a, char & b)
{
char temp;
temp = a;
a = b;
b = temp;
}
void permute( char * str, int l, int r, int n)
{
int i;
if (l == r) {
int j = atoi (str);
if (j % n == 0)
cout << str << endl;
return ;
}
for (i = l; i < r; i++) {
swap_(str[l], str[i]);
permute(str, l + 1, r, n);
swap_(str[l], str[i]);
}
}
int main()
{
char str[100] = "125" ;
int n = 5;
int len = strlen (str);
if (len > 0)
permute(str, 0, len, n);
return 0;
}
|
Java
class GFG{
static void swap_( char []a, int l, int i)
{
char temp;
temp = a[l];
a[l] = a[i];
a[i] = temp;
}
static void permute( char [] str, int l,
int r, int n)
{
int i;
if (l == r)
{
int j = Integer.valueOf(String.valueOf(str));
if (j % n == 0 )
System.out.print(String.valueOf(str) + "\n" );
return ;
}
for (i = l; i < r; i++)
{
swap_(str, l, i);
permute(str, l + 1 , r, n);
swap_(str, l, i);
}
}
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);
}
}
|
Python3
def permute(st, l, r, n):
if (l = = r):
p = ''.join(st)
j = int (p)
if (j % n = = 0 ):
print (p)
return
for i in range (l, r):
st[l], st[i] = st[i], st[l]
permute(st, l + 1 , r, n)
st[l], st[i] = st[i] ,st[l]
if __name__ = = "__main__" :
st = "125"
n = 5
length = len (st)
if (length > 0 ):
p = list (st)
permute(p, 0 , length, n);
|
C#
using System;
class GFG{
static void swap_( char []a, int l,
int i)
{
char temp;
temp = a[l];
a[l] = a[i];
a[i] = temp;
}
static void permute( char [] str, int l,
int r, int n)
{
int i;
if (l == r)
{
int j = Int32.Parse( new string (str));
if (j % n == 0)
Console.Write( new string (str) + "\n" );
return ;
}
for (i = l; i < r; i++)
{
swap_(str, l, i);
permute(str, l + 1, r, n);
swap_(str, l, i);
}
}
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);
}
}
|
Javascript
<script>
function swap_(a, l, i)
{
let temp;
temp = a[l];
a[l] = a[i];
a[i] = temp;
}
function permute(str, l, r, n)
{
let i;
if (l == r)
{
let j = parseInt((str).join( "" ));
if (j % n == 0)
document.write((str).join( "" ) + "<br>" );
return ;
}
for (i = l; i < r; i++)
{
swap_(str, l, i);
permute(str, l + 1, r, n);
swap_(str, l, i);
}
}
let str = "125" ;
let n = 5;
let len = str.length;
if (len > 0)
permute(str.split( "" ), 0, len, n);
</script>
|
Time Complexity: O(N!)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...