×

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

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.