Java Program to Implement Maximum Length Chain of Pairs

This Java program Implements Maximum Length Chain Of Pairs.Given n pairs of numbers. In every pair, the first number is always smaller than the second number. A pair (c, d) can follow another pair (a, b) if b < c. Chain of pairs can be formed in this fashion. Find the longest chain which can be formed from a given set of pairs. Here is the source code of the Java Program to Implement Maximum Length Chain Of Pairs.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

public class MaxLengthChainOfPairs
{
    public int maxChainLength(PairVal pair_arr[], int n)
    {
        int i, j, max = 0;
        int MaxChainLen[] = new int[n];
        for (i = 0; i < n; i++)
        {
            MaxChainLen[i] = 1;
        }
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < i; j++)
            {
                if (pair_arr[i].a > pair_arr[j].b && MaxChainLen[i] < MaxChainLen[j] + 1)
                    MaxChainLen[i] = MaxChainLen[j] + 1;
            }
        }
        for (i = 0; i < n; i++)
        {
            if (max < MaxChainLen[i])
                max = MaxChainLen[i];
        }
        return max;
    }
 
    public static void main(String... arg)
    {
        PairVal pair_arr[] = new PairVal[4];
        pair_arr[0] = new PairVal(5, 24);
        pair_arr[1] = new PairVal(15, 25);
        pair_arr[2] = new PairVal(27, 40);
        pair_arr[3] = new PairVal(50, 60);
        int n = 4;
        MaxLengthChainOfPairs maxLengthChainOfPairs = new MaxLengthChainOfPairs();
        System.out.println("the length of maximum size chain is " 
            + maxLengthChainOfPairs.maxChainLength(pair_arr, n));
    }
}
 
class PairVal
{
    int a;
    int b;
 
    PairVal(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
}
$ javac MaxLengthChainOfPairs.java
$ java MaxLengthChainOfPairs
the length of maximum size chain is 3

Related posts:

Java Program to Represent Graph Using Incidence Matrix
Java Concurrency Interview Questions and Answers
Build a REST API with Spring and Java Config
How to Return 404 with Spring WebFlux
Java InputStream to String
Dockerizing a Spring Boot Application
Java Program to Implement Fenwick Tree
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Introduction to Thread Pools in Java
Handling URL Encoded Form Data in Spring REST
HttpClient Timeout
Recommended Package Structure of a Spring Boot Project
Java Program to Construct an Expression Tree for an Infix Expression
Apache Commons Collections Bag
Java Program to Implement Variable length array
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Perform integer Partition for a Specific Case
Creating a Custom Starter with Spring Boot
Đồng bộ hóa các luồng trong Java
Java Program to Implement Self Balancing Binary Search Tree
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Static Content in Spring WebFlux
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Implement Depth-limited Search
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Spring Boot Configuration with Jasypt
Instance Profile Credentials using Spring Cloud
Java Program to Implement LinkedBlockingQueue API
Java Program to Check Whether a Given Point is in a Given Polygon
Java Program to Generate Random Numbers Using Probability Distribution Function
Spring Boot - Runners