Longest Arithmetic Progression Algorithm in Java

This is a Java Program to implement Longest Arithmetic Progression Algorithm. This program finds the length of longest arithmetic progression from an array of numbers.

Here is the source code of the Java Program to implement Longest Arithmetic Progression Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  **    Java Program to implement Longest Arithmetic Progression Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class  LongestArithmeticProgression **/
  8. public class  LongestArithmeticProgression
  9. {    
  10.     /** function lap **/
  11.     public int lap(int[] A)
  12.     {        
  13.         int n = A.length;
  14.         if (n == 1)
  15.         	return 1;
  16.         int[][] L = new int[n + 1][n + 1];
  17.         int maxLen = 2;
  18.         for (int j = n - 1; j >= 0; j--)
  19.         {
  20.             int i = j - 1, k = j + 1;
  21.             while (i >= 0 && k < n)
  22.             {
  23.                 if (A[i] + A[k] < 2 * A[j])
  24.                     k = k + 1;
  25.                 else if (A[i] + A[k] > 2 * A[j])
  26.                 {
  27.                     L[i][j] = 2;
  28.                     i = i - 1;
  29.                 }
  30.                 else 
  31.                 {
  32.                     L[i][j] = L[j][k] + 1;
  33.                     maxLen = Math.max(maxLen, L[i][j]);
  34.                     i = i - 1;
  35.                     k = k + 1;
  36.                 }
  37.             }
  38.             while (i >= 0)  
  39.             {
  40.                 L[i][j] = 2;
  41.                 i = i - 1;
  42.             }
  43.         }           
  44.         return maxLen;                    
  45.     }
  46.  
  47.     /** Main Function **/
  48.     public static void main(String[] args) 
  49.     {    
  50.         Scanner scan = new Scanner(System.in);
  51.         System.out.println("Longest Arithmetic Progression Algorithm Test\n");
  52.  
  53.         System.out.println("Enter number of elements");
  54.         int n = scan.nextInt();
  55.         int[] arr = new int[n];
  56.         System.out.println("\nEnter "+ n +" elements");
  57.         for (int i = 0; i < n; i++)
  58.             arr[i] = scan.nextInt();
  59.  
  60.         LongestArithmeticProgression obj = new LongestArithmeticProgression(); 
  61.         int result = obj.lap(arr);       
  62.  
  63.         /** print result **/ 
  64.  
  65.         System.out.println("\nLength of Longest Arithmetic Progression : "+ result);        
  66.     }
  67. }

Longest Arithmetic Progression Algorithm Test
 
Enter number of elements
10
 
Enter 10 elements
1 3 7 13 14 15 19 20 25 30
 
Length of Longest Arithmetic Progression : 5
 
 
 
 
Longest Arithmetic Progression Algorithm Test
 
Enter number of elements
10
 
Enter 10 elements
1 2 3 4 5 6 7 8 9 10
 
Length of Longest Arithmetic Progression : 10

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.