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 Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to implement Priority Queue
A Quick Guide to Using Keycloak with Spring Boot
Getting Started with Custom Deserialization in Jackson
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Implement Warshall Algorithm
Spring Security 5 for Reactive Applications
Write/Read cookies using HTTP and Read a file from the internet
Map to String Conversion in Java
Introduction to Spring Cloud Stream
Lập trình đa luồng với CompletableFuture trong Java 8
So sánh HashMap và HashSet trong Java
Từ khóa throw và throws trong Java
A Guide to JPA with Spring
Request Method Not Supported (405) in Spring
Guide to Java 8 groupingBy Collector
Java 9 Stream API Improvements
Java Program to Implement Depth-limited Search
Receive email using IMAP
Java Program to Find the Connected Components of an UnDirected Graph
Spring Boot with Multiple SQL Import Files
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Spring Security Registration – Resend Verification Email
Java Program to Perform Uniform Binary Search
Java Program to Implement PriorityBlockingQueue API
Java Program to Implement Gale Shapley Algorithm
Simple Single Sign-On with Spring Security OAuth2
Retrieve User Information in Spring Security
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Chương trình Java đầu tiên
JWT – Token-based Authentication trong Jersey 2.x
Multi Dimensional ArrayList in Java