Count of ways to rearrange N digits and M alphabets keeping all alphabets together
Given two positive integers N and M representing the count of distinct digits and alphabets respectively in a string, the task to count the number of ways to rearrange the characters of the string such that all the alphabets are adjacent.
Examples:
Input: N = 2, M = 2
Output: 12
Explanation: Possible ways to rearrange characters of a string such that all alphabets are adjacent: { {N1N2M2M1, N2N1M2M1, N2N1M1M2, N1N2M1M2, M2M1N1N2, M1M2N2N1, M1M2N1N2, M2M1N2N1, N1M1M2N2, N2M1M2N1, N1M2M1N2, N2M2M1N1} }.
Input: N = 2, M = 4
Output: 144
Naive Approach: The simplest approach to solve this problem to make a string consisting of N distinct numeric characters and M distinct alphabets. Now, generate all possible permutations of the string and check if all the alphabets of the string are adjacent or not. If found to be true, then increment the count. Finally, print the count obtained.
Time Complexity: O((N + M)!)
Auxiliary Space: O(N + M)
Efficient Approach: The problem can be solved based on the following observations:
Since all alphabets are adjacent, therefore consider all the alphabets as a single character.
Therefore, the total count of ways to rearrange the string by considering all alphabets to a single character = ((N + 1)!) * (M!)
Follow the below steps to solve the problem:
- Calculate the factorial of N + 1 say, X and factorial of M say, Y.
- Finally, print the value of (X * Y).
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int fact( int n)
{
int ans = 1;
for ( int i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
int findComb( int N, int M)
{
int x = fact(N + 1);
int y = fact(M);
return (x * y);
}
int main()
{
int N = 2;
int M = 2;
cout<<findComb(N, M);
}
|
Java
import java.util.*;
class GFG{
static int fact( int n)
{
int ans = 1 ;
for ( int i = 2 ; i <= n; i++)
ans = ans * i;
return ans;
}
static int findComb( int N, int M)
{
int x = fact(N + 1 );
int y = fact(M);
return (x * y);
}
public static void main(String[] args)
{
int N = 2 ;
int M = 2 ;
System.out.print(findComb(N, M));
}
}
|
Python3
import math
def fact(a):
return math.factorial(a)
def findComb(N, M):
x = fact(N + 1 )
y = fact(M)
return (x * y)
if __name__ = = "__main__" :
N = 2
M = 2
print (findComb(N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int fact( int n)
{
int ans = 1;
for ( int i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
static int findComb( int N, int M)
{
int x = fact(N + 1);
int y = fact(M);
return (x * y);
}
public static void Main()
{
int N = 2;
int M = 2;
Console.Write(findComb(N, M));
}
}
|
Javascript
<script>
function fact(n)
{
var ans = 1;
for ( var i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
function findComb(N, M)
{
var x = fact(N + 1)
var y = fact(M)
return (x * y)
}
var N = 2
var M = 2
document.write(findComb(N, M))
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Approach 3 :
The approach used in this code is based on combinatorics and the factorial of N and M.
The function fact(n) calculates the factorial of a given number, which is the product of all integers from 1 to n. This function takes O(n) time as it is a simple for loop that iterates from 2 to n, multiplying the result by the current number in each iteration.
The function findComb(N, M) calculates the total number of ways to rearrange the characters such that all alphabets are adjacent, by multiplying the factorial of N+1 and M. The factorial of N+1 is used to account for the number of ways to arrange the digits around the alphabets, and the factorial of M is used to account for the number of ways to arrange the alphabets together.
C++
#include<bits/stdc++.h>
using namespace std;
int fact( int n)
{
int ans = 1;
for ( int i = 2; i <= n; i++)
ans = ans * i;
return ans;
}
int findComb( int N, int M)
{
int x = fact(N + 1);
int y = fact(M);
return (x * y);
}
int main()
{
int N = 2;
int M = 2;
cout<<findComb(N, M);
}
|
Java
public class Main {
static int fact( int n) {
int ans = 1 ;
for ( int i = 2 ; i <= n; i++) {
ans = ans * i;
}
return ans;
}
static int findComb( int N, int M) {
int x = fact(N + 1 );
int y = fact(M);
return (x * y);
}
public static void main(String[] args) {
int N = 2 ;
int M = 2 ;
System.out.println(findComb(N, M));
}
}
|
Python3
def fact(n):
ans = 1
for i in range ( 2 , n + 1 ):
ans = ans * i
return ans
def findComb(N, M):
x = fact(N + 1 )
y = fact(M)
return (x * y)
N = 2
M = 2
print (findComb(N, M))
|
C#
using System;
public class GFG {
static int fact( int n)
{
int ans = 1;
for ( int i = 2; i <= n; i++) {
ans = ans * i;
}
return ans;
}
static int findComb( int N, int M)
{
int x = fact(N + 1);
int y = fact(M);
return (x * y);
}
static public void Main()
{
int N = 2;
int M = 2;
Console.WriteLine(findComb(N, M));
}
}
|
Javascript
function fact(n) {
let ans = 1;
for (let i = 2; i <= n; i++) {
ans = ans * i;
}
return ans;
}
function findComb(N, M) {
let x = fact(N + 1);
let y = fact(M);
return (x * y);
}
let N = 2;
let M = 2;
console.log(findComb(N, M));
|
Time complexity : O(n) as it only involves one for loop in the fact(n) function and the rest of the calculations are constant time operations. This approach is simple and efficient as it only requires a single for loop to calculate the factorial of N and M and the rest of the calculations are simple arithmetic operations.
Auxiliary Space : O(1)
Last Updated :
01 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...