DP35 Longest Equality Series Longest Arithmetic Progression @ geeksforgeeks

Source: Internet
Author: User

In an ordered array, we can find the arithmetic difference series and use three pointers to move the front and back pointers Based on the middle pointer.


Given a set of numbers, findLEngth ofLOngestARithmeticPRogression (LLAP) In it.

Examples:

set[] = {1, 7, 10, 15, 27, 29}output = 3The longest arithmetic progression is {1, 15, 29}set[] = {5, 10, 15, 20, 25, 30}output = 6The whole set is in AP

For simplicity, we have assumed that the given set is sorted. We can always add a pre-processing step to first sort the set and then apply the below algorithms.

ASimple solutionIs to one by one consider every pair as first two elements of AP and check for the remaining elements in sorted set. to consider all pairs as first two elements, we need to run a O (n ^ 2) nested loop. inside the nested loops, we need a third loop which linearly looks for the more elements inARithmeticPRogression (AP). This process takes O (n3) time.

We can solve this problem in O (n2) timeUsing Dynamic Programming. To get idea of the DP solution, let us first discuss solution of following simpler problem.

Given a sorted set, find if there exist three elements in Arithmetic Progression or not
Please note that, the answer is true if there are 3 or more elements in AP, otherwise false.
To find the three elements, we first fix an element as middle element and search for other two (one smaller and one greater ). we start from the second element and fix every element as middle element. for an element set [j] to be middle of AP, there must exist elements 'set [I] 'and 'set [k] 'such that set [I] + set [k] = 2 * set [j] where 0 <= I <j and j <k <= n-1.
How to efficiently find I and k for a given j?We can find I and k in linear time using following simple algorithm.
1)Initialize I as J-1 and k as j + 1
2)Do following while I> = 0 and j <= n-1
..........A)If set [I] + set [k] is equal to 2 * set [j], then we are done.
........B)If set [I] + set [k]> 2 * set [j], then decrement I (do I --).
........C)Else if set [I] + set [k] <2 * set [j], then increment k (do k ++ ).


How to extend the above solution for the original problem?
The above function returns a boolean value. the required output of original problem is Length of theLongest Arithmetic Progression (LLAP) which is an integer value. if the given set has two or more elements, then the value of LLAP is at least 2 (Why ?).
The idea is to create a 2D table L [n] [n]. an entry L [I] [j] in this table stores LLAP with set [I] and set [j] as first two elements of AP and j> I. the last column of the table is always 2 (Why-see the meaning of L [I] [j]). rest of the table is filled from bottom right to top left. to fill rest of the table, j (second element in AP) is first fixed. I and k are searched for a fixed j. if I and k are found such that I, j, k form an AP, then the value of L [I] [j] is set as L [j] [k] + 1. note that the value of L [j] [k] must have been filled before as the loop traverses from right to left columns.


Package DP; public class LongestArithmeticProgression {public static void main (String [] args) {int [] A = {1, 7, 10, 15, 27, 29}; System. out. println (arithmeticThree (A,. length); System. out. println (lengthOfLongestAP (A,. length); int [] B = {1, 7, 10, 15, 27, 28}; System. out. println (arithmeticThree (B, B. length); System. out. println (lengthOfLongestAP (B, B. length);} // determines whether there is A public static boolean arithmeticThree (int [] A, int n) {for (int j = 1; j
 
  
= 0 & k <= N-1) {if (A [I] + A [k] = 2 * A [j]) {// find return true ;} else if (A [I] + A [k]> 2 * A [j]) {// too large I --;} else {// too small k ++ ;}}} return false;} // Returns length of the longest AP subset in a given set // O (n ^ 2) time, spacepublic static int lengthOfLongestAP (int [] A, int n) {if (n <= 2) {return n;} // Create a table and initialize all values as 2. the value of // L [I] [j] stores LLAP with A [I] and A [j] as first two // elements of AP. only valid entries are the entries where j> iint [] [] L = new int [n] [n]; // L [I] [j]: take A [I] And A [j] as the maximum number of the first two equi series items int llap = 2; // The maximum number of equi series items // Fill entries in last column as 2. there will always be // two elements in AP with last number of set as second // element in APfor (int I = 0; I
  
   
= 1; j --) {int I = J-1, k = j + 1; // Search for I and k for jwhile (I> = 0 & k <= N-1) {if (A [I] + A [k] <2 * A [j]) {k ++ ;} else if (A [I] + A [k]> 2 * A [j]) {// Before changing I, set L [I] [j] as 2L [I] [j] = 2; // initialization, with at least 2i --;} else {// Found I and k for j, LLAP with I and j as first two // elements is equal to LLAP with j and k as first two // elements plus 1. L [j] [k] must have been filled // before as we run the loop from right sideL [I] [j] = L [j] [k] + 1; // because it includes two items: [I, j] and [j, k], one more item than the original [j, k] // Update overall LLAP, if neededllap = Math. max (llap, L [I] [j]); // Change I and k to fill more L [I] [j] values for current ji --; k ++; }}// If the loop was stopped due to k becoming more than/n-1, set the remaining entities in column j as 2 while (I> = 0) {L [I] [j] = 2; I -- ;}} return llap ;}}
  
 


Http://www.geeksforgeeks.org/length-of-the-longest-arithmatic-progression-in-a-sorted-array/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.