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:
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Implement Queue using Linked List
Comparing Objects in Java
Java Program to Perform Insertion in a 2 Dimension K-D Tree
HttpClient Timeout
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Convert String to Byte Array and Reverse in Java
What is Thread-Safety and How to Achieve it?
Introduction to Spring Method Security
Simplify the DAO with Spring and Java Generics
So sánh HashMap và Hashtable trong Java
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Error Handling for REST with Spring
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Find All Pairs Shortest Path
Java InputStream to String
Using JWT with Spring Security OAuth
Batch Processing with Spring Cloud Data Flow
Java – Reader to String
Add Multiple Items to an Java ArrayList
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Collections Interview Questions
Debugging Reactive Streams in Java
Convert Character Array to String in Java
Spring Boot - File Handling
An Intro to Spring Cloud Zookeeper
Setting the Java Version in Maven
Custom Exception trong Java
A Comparison Between Spring and Spring Boot