Aman Arithmetic progression

Concepts Used

Sorting

Difficulty Level

Medium

Problem Statement (Simplified):

Print the total number of cases in which the current array sequence becomes Arithmetic Progression by adding a number at any place, also print the numbers.

  • Arithmetic Progression: Sequence increasing or decreasing with a constant K (common difference) is known as Arithmetic Progression.

See original problem statement here


Input:
1
3
4 2 6

Output:
2
0 8

Explanation:
In the given array,
If we introduce 0, the array becomes [0, 2, 4, 6] which is an Arithmetic Progression with common difference 2.
If we introduce 8, the array becomes [2, 4, 6, 8] which is an Arithmetic Progression with common difference 2.
So, we print 2, because there are two such numbers, also we print 0 and 8 because they're required numbers.

Solving Approach :

Bruteforce Approach:
1) We sort the array and introduce numbers one by one according to data structures in c++.. After adding one number, we check if the current formed sequence is Arithmetic Progression or not. If yes, increase the count and print the note the added number.
2) After the whole array is traversed for adding the number, we print the count and print all the numbers noted in a row.
3) This approach takes O(n*log(n)) for sorting array using merge sort, also O(n+1) to add numbers. This takes O(n) for checking if the current sequence is Arithmetic Progression or not for every addition. Hence total time complexity of this approach is O(n*log(n) + n^2). This approach takes longer times to process a large number of queries, so we move to a new efficient approach.

Efficient Approach:

1) As we know Arithmetic Progression is always increasing or decreasing, so we sort the array so that it can be arranged in an increasing sequence.
2) If the array contains only a single element, it is already an Arithmetic Progression, so no further elements are required to make it Arithmetic Progression.
3) If the array contains only 2 elements, there can be three possibilities :
> A number can be added in front, where the common difference between the number and the first element is the same as the difference between array elements.
>
A number can be added in last, where the common difference between the number and last element is the same as the difference between array elements.
> A number can be added in middle, that would be the average of two elements in the array, where the sum of both elements must be even.
3) If the array contains more than 2 elements, the following cases, can arise :
>
CASE-1: If the current array is already an Arithmetic Progression, we can add numbers in the last and first position as we discussed in point 2. Here no middle element is added.
> * CASE-2: If the array is not an Arithmetic Progression, we calculate all possible common differences, and see for these cases :
>>1) If there are more than 2 common differences, there is no way to convert the current array into Arithmetic Progression by adding a number.
>>2) If there are two common difference, where both appears multiple times in sequence (multiple elements have them as common difference), then there is no possible way to convert the current array into an Arithmetic Progression.
>>3) If there appears a common difference which is present only once, and other common difference appears the rest of the times. Let’s say multiple elements have a as common difference and there exist two elements whose difference is b. We can convert current sequence into an arithmetic progression only and only if b is double of a (2b = a), hence a number can be added between elements having common difference b, and number would be A[k]+a or A[k+1]-a where A[k+1]-A[k] = b.

Example

ADD IMAGES FROM FOLDER

Solutions

[TABS_R id=1307]
[forminator_quiz id=”1332″]

Space Complexity : O(1)

Previous post Copying Hero
Next post Ball Firing Game

Leave a Reply

Your email address will not be published. Required fields are marked *