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:

Database Migrations with Flyway
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Generating Random Numbers in a Range in Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement Adjacency List
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement Attribute API
Returning Custom Status Codes from Spring Controllers
Java Program to Implement Insertion Sort
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Shuffling Collections In Java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Finding the Differences Between Two Lists in Java
Java Program to Implement the Bin Packing Algorithm
HttpClient Connection Management
Difference Between Wait and Sleep in Java
Spring Security Registration – Resend Verification Email
A Guide to the Java LinkedList
Registration – Activate a New Account by Email
Java Program to Implement Miller Rabin Primality Test Algorithm
Hướng dẫn Java Design Pattern – Singleton
Java Program to Find the Longest Path in a DAG
Spring Boot - Interceptor
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Filtering a Stream of Optionals in Java
Java Program to Represent Graph Using Incidence List
Mapping a Dynamic JSON Object with Jackson
Java – Rename or Move a File
Life Cycle of a Thread in Java
Ignore Null Fields with Jackson