Welcome back, dear reader. So, how is it going? In this article, we will discuss a very interesting and challenging problem called ARITHMETIC SLICES-2. So, what does this problem say?
Important Links : Problem Link, Solution Video Link
Welcome back, dear reader. So, how is it going? In this article, we will discuss a very interesting and challenging problem called ARITHMETIC SLICES-2. So, what does this problem say?
Important Links : Problem Link, Solution Video Link
We will be given an input array. We have to count the number of arithmetic slices in that array. An arithmetic slice is defined as a subsequence of an array which forms an AP (Arithmetic Progression) and the number of terms should be greater than or equal to 3.
Now, in the above example the answer is 3 and we have 3 such subsequences. Well, to remove your confusion regarding subsequences and subarrays, let us take another example.
So, as you can see, we have to consider subsequences forming an AP with at least 3 terms as an Arithmetic Slice. We recommend you refer to the ARITHMETIC SLICES-2 VIDEO to understand the question and clear your doubts about it if any. We also suggest you try to solve these problems on your own first and then refer to the solution to build your concepts.
Time Complexity: O(n2) where n is the number of elements in the array.
Space Complexity: O(n2)
Explanation:
Let us start solving this problem following the step wise procedure of dynamic programming.
We will make an array of hashmaps of the same size as that of the input array. In this array, every element is a hashmap of integer-integer type. The elements in the hashmap will contain a common difference vs count type of elements. What do we mean by this?
Each element in the hashmap will contain a common difference of an AP and the number of APs with that common difference till the ith index. So, what does each element in the array of hashmap represent? Have a look at this:
So, did you understand what each element represents? Each element in this array represents the number of APs with a particular common difference till the index that we are on currently. Note that the number of terms in the APs will be greater than or equal to 2.
Important: It is very important to note that in the question, we have been asked to count the number of APs with minimum 3 terms whereas in the array of hashmaps, each hashmap stores the number of APs with a particular common difference and these APs have minimum number of terms equal to 2.
So, can you guess the direction from where we will start solving this problem? See, the direction of solving always starts from the smaller problem and reaches the largest problem towards the end. In this question, we have to find the number of APs with minimum 3 elements. So, at index 0 and 1, there will not be any such AP as there are not 3 elements to complete the AP.
So, we will start solving from index 0 and reach till the end of the array of hashmaps.
Let us start solving our problem. We are at index=0 initially as shown in the image below:
We will solve our problem using this example and we are not going to show an array of hashmaps in the diagram. Rather, we will use a table to depict the solution steps.
At index 0, we do not have any AP with minimum 2 elements to be kept at maps[0]. So, let us move to index 1.
Here, arr[1]-arr[0]=2-4=-2. So, we have an AP with common differences -2 and 2 elements are present in it. Also, before this index, there was no AP with common difference=-2. So, in the hashmap maps[1], we will put {-2,1} depicting the common difference -2 and number of APs with this common difference till index 1 equals 1.
So, till here, the process was simple. Now, in this step, you have more chances to understand the procedure. We are now at index=3.
We have 2 variables i and j. The variable i is at the current index that we are at and the variable j will always start from 0 and reach till i-1 so that we can calculate the common difference of APs. Are you confused about this? Don-t worry, let's see what the procedure will be.
So, we are at index 3 i.e. i=3. Also, the variable j is kept at index 0. Now, we calculate arr[i]-arr[j]=3-4=-1 and this is a common difference for the AP. Now, we can-t find any AP with common difference -1 till the 0th index i.e. the jth index. So, there is only one AP with common difference -1 till index 3 and that is what we have kept at maps[3] as shown in the image above.
Note that the AP is also shown in the diagram just for the explanation purpose. In the array of hashmaps, we will store only the common difference vs number of APs of that common difference till the ith index at maps[i].
So, we will now move the variable j forward and it comes to index 1. The difference arr[2]-arr[1]=3-2=1 gives a common difference =1. Since there is no AP with common difference=1 till index 2, we will put 1,1 in maps[3] depicting that corresponding to the common difference 1, there is only 1 AP till index 3.
As the previous cases, the AP is shown with the pink color and it is shown just for the explanation purpose.
So, dear reader, we request you try to fill the hashmap maps[5] yourself using the procedure explained above. You should end up with the hashmap filled as shown below:
Till now, we have not got even one AP that contains a minimum of 3 elements. Let us now move to index 4. As usual, j will start from 0.
So, the common difference will be arr[i]-arr[j]=2-4=-2. There is no AP of common difference -2 till index 0. So, we will put -2,1 in the maps[4] and the AP is 4,2.
Now, j will move to index 1. The common difference here is arr[i]-arr[j]=2-2=0. There is no AP of common difference 0 till index 1.. So, we will put 0,1 in maps[4].
Now comes a very interesting point in this solution. The variable j is at index 2. The common difference is arr[4]-arr[2]=2-3=-1. There is one AP for this common difference already till index 3 i.e. {4,3}. So, adding 5 to this AP will make this of size 3 and it is a valid AP for our answer. So, we will add the number of APs till index 3 i.e. 1 into our answer. Why did we take the value from index 3?
This is because one element added to it makes it of size 3 which is a valid AP. But, the value that we will store at index 4 actually gives us the count of the number of APs with a common difference -1 of size at least 2. So, we want APs of size 3 only. Hence, we have taken the value from index 3 to the answer.
So, what will be the value at maps[4]? There are now 2 APs with a common difference -1. The first is the previous AP in which we have now added the value 2 as well i.e. {4,3,2} and the second one is the AP with jth and ith index values that is {3,2}. This is shown in the diagram given below:
Now, j will move to the next index i.e. index 3.So, the common difference will be arr[i]-arr[j]=2-5=-3. There is no AP with common difference -3 till index 3. So, we will put -3,1 into the maps[4].
So, dear reader. We hope that you understand what we are doing here. So, we are left with the last index. Try to fill this yourself and see if the answer changes or you get the answer=1 only. The completely filled hashmap array is given below:
So, dear reader, we recommend you refer to the solution video () to understand the complete procedure explained above in more depth. Let us now look at one exceptional case that we have to handle for this question.
Exception/Corner Case :
See, we have to solve this problem for integers. Let us say in the array at arr[j] we have stored the minimum value of integers and at arr[i] we have the max value of integers.
The largest Integer number in Java is 231 -1 and the value of the smallest Integer number is -231. So, the situation is shown in the image below:
So, when we will reach the index 2 and calculate the common difference, it will be:
As you can see, this value is out of range of integers. This is the common difference. This means that we will add 232-1 to 231-1 to get the next term of AP.
This means that the next term of the AP will also be out of the integer range. So, the point that we want to make here is that we need to take care of the fact that the common difference should not be equal to or out of the boundary values of Integer that is the common difference should not be:
So, the common difference should always be in the range of integers.
So, we have completed all the aspects that we had to discuss in this question. We recommend you refer to this video to understand the complete procedure explained above and also understand the code written below:
java; true";
So, dear reader, we hope that you understood the above code as well. We recommend you refer to the solution video to understand the dry run of the code shown above and the edge cases and more additional information. Let us now analyze the time and space complexity of the above code.
The time complexity of the above solution is O(n2) as you can see a nested loop in the code and we will traverse all the previous elements before the current element to calculate the common difference.
The space complexity of the above code is O(n2) as Hashmaps will contain all the key value pairs before the current element and this makes the space complexity O(n2).
So, we have completed the discussion of this problem. If you have any doubts regarding any concept, refer to the complete solution video to clear all your doubts.