×

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem.

A permutation is a term used mainly in mathematics and it finds usage in programming as well. It can be defined as the method of ordering each member of a provided set to form an arrangement. P(n, r) is used to represent a permutation coefficient. N refers to the total no. of elements and r represents an instance, which means it gives out the total number of permutations by considering r at a time.

For example:

P(6,2) – It means there are 6 elements as n is equal to 6 and 2 elements can be taken from it as r is equal to 2.

To know what the possible arrangements are or how these 2 numbers can be arranged, we need to take permutation into account.

Considering 6 elements 1, 2, 3, 4, 5, 6 and taking 2 elements at a time.

The possible arrangements become:

(1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6)

(2, 1)(2, 2)(2, 3) (2, 4) (2, 5) (2, 6)

(3, 1) (3, 2)(3, 3) (3, 4) (3, 5) (3, 6)

(4, 1) (4, 2) (4, 3)(4, 4)(4, 5) (4, 6)

(5, 1) (5, 2) (5, 3) (5, 4), (5, 5) (5, 6)

On counting the above-written pairs we receive 25. It denotes that there are 25 ways to arrange the numbers from 1 to 6 by considering 2 at a time.

Now, let us look at the mathematical way to solve the above problem P(n, r).

One can easily solve it using the given formula:

P(n, r) = n! / (n - r)! 

P(6, 2) = 6! / (6 - 2)! = 6! / 4! = (6 x 5 x 4 x 3 x 2 x 1) / (4 x 3 x 2 x 1) = 720 / 24 = 30

Now, let us understand the concept through a java program

Implementation

public class PermCoefficient  
{  


// a method to compute the factorial  
// of the number n,which is n!  
public int computeFactorial(int num)  
{  
    int res = 1;  


    // loop to compute the value of   
    // 1 x 2 x 3 x 4 x ... x n  
    for(int j = 1; j <=num; j++)  
    {  
        res = res * j;  
    }  


    return res;  
}  




public int computePermutation(int num, int r1)  
{  


// finding factorial till n  
int numerator = computeFactorial(num);  


// computing factorial (num - r1)!  
int denominator = computeFactorial(num - r1);  


/// computing P(num, r1) = num! / (num - r1)!  
return (numerator / denominator);  


}  


// main method  
public static void main(String argvs[])  
{  
int num = 8;  
int r1 = 5;  
// creation of an object of the class named PermCoefficient  
PermCoefficient obj = new PermCoefficient();  


// storing the outcome of the function computePermutation()  
int answer = obj.computePermutation(num, r1);  


// displaying the answer  
System.out.println("The value of (" + num + ", " + r1 + ") is: " + answer);  


num = 7;  
r1 = 3;  


System.out.println();  


// keeping the result of the method findPermutation()  in a variable
answer = obj.computePermutation(num, r1);  


// displaying the outcome 
System.out.println("The value of (" + num + ", " + r1 + ") is: " + answer);  


}  
}  

Output:

Permutation Coefficient in Java

Explanation: When n is 8 and r is 5, the possible number of outcomes would be 6720.Similarly, if the value of n is 7 and the value of r is 3 then, the possible number of arrangements would be 210.

The time complexity of this program is O(n + r). 

The second approach - Recursive

public class PermutationCoeff  
{  


// A method to compute the value of P(n, r), recursively  
public int computePermutationCoeff(int num, int r1)  
{  
// dealing with the base cases  
if(r1 == 0)  
{  
return 1;  
}  


else if(r1 == num)  
{  
// computing the value of num!  
int res = 1;  
for(int j = 1; j <= num; j++)  
{  
res = res * j;  
}  


return res;  
}  
else  
{  
// finding the value of P(num, r1) with the help of the recursion formula  
return computePermutationCoeff(num - 1, r1) + r1 * computePermutationCoeff(num - 1, r1 - 1);  
}  


}  


// main method  
public static void main(String argvs[])  
{  
int num = 15;  
int r1 = 3;  


// creation of an object of the class named PermutationCoeff  
PermutationCoeff obj = new PermutationCoeff();  


// calling the function computePermutationCoeff()  
int res = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + res);  


// updating the value of num & r1  
num = 9;  
r1 = 4;  


System.out.println();  


// calling the function computePermutationCoeff()  
res = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + res);  


}  
}  

Output:

Permutation Coefficient in Java

Explanation: In this approach, we tried to solve the problem recursively.

Third approach: Iterative1

public class PermutationCoeff3  
{  


// A function to find the result of P(num, r1)  
public int computePermutationCoeff(int num, int r1)  
{  
int Fn2 = 1;  
int Fr2 = 1;  


// Calculaing num! and (num - r1)!  
for (int j = 1; j <= num; j++)  
{  
Fn2 = Fn2 * j;  
if (j == num - r1)  
{  
Fr2 = Fn2;  
}  
}  
int res = Fn2 / Fr2;  
return res;  
}  


// main method  
public static void main(String argvs[])  
{  
int num = 15;  
int r1 = 3;  


// creation of an object of the class named PermutationCoeff3  
PermutationCoeff3 obj = new PermutationCoeff3();  


// calling the funcion computePermutationCoeff()  
int res1 = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + res1);  


// updating the value of num and r1  
num = 9;  
r1 = 2;  


System.out.println();  


// calling the function computePermutationCoeff()  
res1 = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + res1);  


}  
}

Output:

Permutation Coefficient in Java

Explanation: In this method, a nested for loop and a two-dimensional array is used. The time complexity here is O(n * r). The space complexity is also the same i.e   O(n * r). However, we can reduce these complexities by optimizing our program.

4th approach:  iterative2

public class PermutationCoeff2  
{  


// A method that calculates the value of P(num, r1)  
public int computePermutationCoeff(int num, int r1)  
{  
int a[] = new int[num + 2];  


// Computing the value of the Permutation Coefficient  
//in abottom-up manner  
for (int i = 0; i <= num; i++)  
{  


// dealing with the Base Case  
if (i == 0)  
{  
a[i] = 1;  
}  


// Compute the value using the previously  
// stored values  
else  
{  
a[i] = a[i - 1] * i;  
}  


}  


// calculating num! / (num - r)!  
int res = a[num] / a[num - r1];  


return res;  
}  


// main method  
public static void main(String argvs[])  
{  
int num = 15;  
int r1 = 3;  


// creation of an object of the class PermutationCoeff2  
PermutationCoeff2 obj = new PermutationCoeff2();  


// calling the method  named computePermutationCoeff()  
int result = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + result);  


// upadating the value of num & r1  
num = 9;  
r1 = 2;  
System.out.println();  


// calling the method named computePermutationCoeff()  
result = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + result);  


}  
}  

Output:

Permutation Coefficient in Java

Explanation: In this case, the space and time complexity have been reduced to O(n).

5th approach:  iterative3

public class PermutationCoeff3  
{  
// A method to calculate the value of P(num, r1)  
public int computePermutationCoeff(int num, int r1)  
{  
int Fn1 = 1;  
int Fr1 = 1;      
// Calculating num! and (num - r1)!  
for (int j = 1; j <= num; j++)  
{  
Fn1 = Fn1 * j;  
if (j == num - r1)  
{  
Fr1 = Fn1;  
}  
}  
int ans = Fn1 / Fr1;  
return ans;  
}  
// main method  
public static void main(String argvs[])  
{  
int num = 15;  
int r1 = 3;  


// creation an object of the class named PermutationCoeff3  
PermutationCoeff3 obj = new PermutationCoeff3();  


// calling the function named computePermutationCoeff()  
int result = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + result);  


// upadating the value of n & r  
num = 9;  
r1 = 4;  


System.out.println();  


// calling the function named computePermutationCoeff()  
result = obj.computePermutationCoeff(num, r1);  
System.out.println("The Value of P(" + num + ", "+ r1 +")" + " = " + result);  


}  
}  

Output:

Permutation Coefficient in Java

Explanation: In this iterative approach, the time and space complexity is reduced to O(1). So, this is the most optimized approach to solve the given problem.


Related Topics

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Java Architecture

Java architecture is a combination of three parts they are JVM, JRE and JDK. These components will help in the functioning of the java programs. The process of code interpretation...

6 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.