Longest Arithmetic Progression Sequence in Java10 Sept 2024 | 8 min read An array arr[] is given, and the task is to find the length of the longest sequence in the array such that the sequence forms the arithmetic progression. Example 1: Input: int arr[] = {30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140}; Output: 12 Explanation: The sequence of numbers 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140 form the arithmetic progression sequence where the common difference is 10, and the total numbers in the sequence is 12. Example 2: Input: int arr[] = {15, 7, 20, 9, 14, 15, 25, 30, 90, 100, 35, 40}; Output: 6 Explanation: The sequence of numbers 15, 20, 25, 30, 35, 40 forms the arithmetic progression sequence where the common difference is 5 and the total numbers in the sequence are 6. Let's discuss various approaches to solve the problem. Naive ApproachThe naive or simple approach is to consider each pair one by one and treat the elements of the pair as the first two elements (assume the first two elements are: a1 and a2) of the arithmetic progression. Using the first two elements, we can get a common difference (a2 - a1). Using this common difference, we can get the subsequent elements of the arithmetic progression. While finding the subsequent elements, we can also keep the count of elements (countEle) for that arithmetic progression. We will find this count (countEle) for each pair and then will do the comparison for finding the longest arithmetic sequence. Observe the following program. FileName: LongestArithmeticProgression.java Output: For the following array: 30 40 50 60 70 80 90 100 110 120 130 140 The length of the longest arithmetic progression sequence is: 12 For the following array: 15 7 20 9 14 15 25 30 90 100 35 40 The length of the longest arithmetic progression sequence is: 6 Complexity Analysis: In the above program, three for-loops have been nested. Therefore, the time complexity of the above program O(n3), where n is the total number of the elements present in the input array. The space complexity of the above program is constant, i.e., O(1). Since the O(n3) time complexity of the program is too high, it is required to reduce the time complexity using some optimization. The following approach shows the same. Dynamic Programming ApproachFileName: LongestArithmeticProgression1.java Output: For the following array: 30 40 50 60 70 80 90 100 110 120 130 140 The length of the longest arithmetic progression sequence is: 12 For the following array: 15 7 20 9 14 15 25 30 90 100 35 40 The length of the longest arithmetic progression sequence is: 6 Complexity Analysis: In the above program, two for-loops have been nested. Therefore, the time complexity of the above program O(n2), where n is the total number of the elements present in the input array. We have also used the two-dimensional array which makes the space complexity of the program O(n2). Even, we can do more optimization to reduce the space complexity. The following approach shows the same. Using Two Pointers ApproachIn this approach, we will fix the middle element of the arithmetic progression and will try to find out the previous and the next element of the progression. It will be done using two pointers. However, before doing that we also need to sort the array. It is because two pointers approach will not work if the given array is not sorted. FileName: LongestArithmeticProgression2.java Output: For the following array: 30 40 50 60 70 80 90 100 110 120 130 140 The length of the longest arithmetic progression sequence is: 12 For the following array: 15 7 20 9 14 15 25 30 90 100 35 40 The length of the longest arithmetic progression sequence is: 6 Complexity Analysis: The time complexity is the same as above. However, we have only used a single dimensional array for accomplishing the task. Therefore, we have reduced the space complexity to O(n), where n is the total number of the elements present in the input array. Next TopicTypes of Sockets in Java |
In Java, inheritance enables a class to adopt behaviors and functions from another class. The class from which the functionalities and behaviours are inherited is known as the base class or parent class or superclass. The receiver class is often known as a child class,...
4 min read
Determine the longest happy string that may be given the three integers a, b, and c. Return any of the longest happy strings if more than one exists. Return the empty string " if there isn't a string of that kind. A string that has...
9 min read
The fundamental building block of each programming language is an operator. Additionally, Java has a wide variety of operators that can be used for arithmetic, relational, logical, and other calculations and tasks. They are categorized according to the functions they provide. Assignment Operators: These operators can be used...
5 min read
Java is a versatile and widely used programming language known for its platform independence and object-oriented approach. One of the fundamental concepts in Java programming is the use of classes and objects. A crucial aspect of this is the concept of a "Driver Class". In this...
2 min read
The N-th Stair Problem, also known as the Stair Climbing Problem. It is a classic dynamic programming challenge. The problem generally asks: Considering a staircase, how many different ways can you climb to the top? If you can only climb one or two stairs at a...
7 min read
In this section, we are going to learn about stars numbers in Java. The stars number resembles the board of the Chinese checkers game. A star number is a hexagram. Here, a hexagram means a six-pointed star. Observe the following diagram. Mathematically, the number is represented by Sn...
9 min read
Before directly jumping to the topic 'Blocking Queue' let us first understand Queue in brief. Queue is an ordered list of objects where insertions take place at the rear end of the list and deletion of elements takes place from the front end. Therefore, it is...
14 min read
Java is an object-oriented programming language, which means that objects play a central role in its design. Fundamental things in Java that contain data and behaviours are called objects. For Java code to be efficient and modular, understanding objects is crucial. We will examine objects...
4 min read
The getImplementationVersion() method is a part of the Package class in Java. It is employed to obtain the package's implementation version that matches to the given class. If the version is not supplied or cannot be identified, the function returns null. It also returns a string...
3 min read
Merge sort is similar to the quick sort algorithm as it uses the divide and conquer approach to sort the elements. It is one of the most popular and efficient sorting algorithms. It divides the given list into two equal halves, calls itself for the two...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India