×

# TAASEQ - editorial

Author: Tuấn Anh Trần Đặng
Tester: Kamil Dębowski
Editorialist: Tuấn Anh Trần Đặng

Easy

# PREREQUISITES:

Dynamic Programming

# Problem

Remove a single number in the sequence to make it an arithmetic progression.

# Solution

The number at position i can be removed if the sub-sequence on its left and right are both arithmetic progression and when when we concat that two parts they still make an arithmetic progression.

The first condition can be handled by precalculate f[i] = whether the prefix of i characters of the sequence is arithmetic progression and similarly g[] for the suffixes. Put all the corner cases aside the whole conditions can be represented as f[i] and g[i] and a[i - 2] - a[i - 1] = a[i - 1] - a[i + 1] = a[i + 1] - a[i + 2] (1).

### details

We will use dynamic programming (dp) to calculate f and g. Since they are similar let’s just discuss how to calculate f. Initialize the dp with f[0] = f[1] = f[2] = true. We’ll have that

• f[i] = (a[i - 1] - a[i] = a[i - 2] - a[i - 1] and f[i - 1]).

Before using the formula (1) some corner cases we need to consider is when N ≤ 3, i ≤ 2 and N - 2i.

Complexity is O(N).

# Author's/Tester's Solutions:

This question is marked "community wiki".

116613
accept rate: 0%

19.5k348496535

 4 I actually had a different type of solution for this. Either we can remove index 1 and see if the remaining sequence is in AP or not. If it is we get one of the possible answers. Now we will definitely make sure index 1 lies in the answer. Now we remove index 2 and see if the sequence is in AP or not. If it is then we get one of the possible answers. Now we definitely include both index 1 and 2 in our solution. This fixes the common difference "d" for our case. Now simply iterate through the array and check for fault positions. If no faulty positions are found, this means the given sequence was already in AP. In such a case find the minimum value in the array which can be removed. If some faulty position is found break at the first one, as we can make exactly one removal. Again after removing these positions check if we get a valid AP or not and update the answer. Answer is the minimum of all the above 4 cases. Here is my solution https://www.codechef.com/viewsolution/11559787 answered 19 Sep '16, 00:25 6★likecs 3.7k●19●78 accept rate: 9% @likecs i did the way u did but got wa did u got ac (19 Sep '16, 16:53) If did not get AC, I would not have discussed the approach in detail, rather asked for help :P. As, per your question, I got AC (19 Sep '16, 16:59) likecs6★
 4 The test cases for this problem are weak. Here is my accpeted solution : https://www.codechef.com/viewsolution/11562158 but it fails on the following test case: 1 7 2 3 6 8 13 15 18 The answer for the above test case is -1 but my accepted solution gives 2 (link text) answered 19 Sep '16, 00:32 477●1●8 accept rate: 15%
 2 Yes, Test cases are weak. I have found accepted solutions of people with wrong answers. Test Cases: 1 5 2 4 5 6 8 Given Ans: -1 Right Ans: 5 1 4 2 4 5 6 Given Ans: 5 Right Ans: 2 Some code are giving TLE. I am checking through my compile list Testcase: 14 7 2 3 6 8 13 15 18 3 3 5 7 3 7 5 3 3 1 7 12 3 12 7 1 5 2 6 7 8 11 3 2 7 12 4 2 5 7 11 4 3 6 7 11 6 2 4 6 15 8 10 5 2 4 5 6 8 4 2 4 5 6 2 1 1 4 4 5 6 8 Ans: -1 3 3 1 1 -1 2 -1 6 15 5 2 1 5 Everyone check your answer against this. I request to recheck all the submission. answered 19 Sep '16, 01:09 11●1 accept rate: 0%
 1 Can someone rewrite this editorial with a better explanation ? answered 06 Nov '16, 05:19 35●2 accept rate: 0%
 0 I used the same approach while the contest was running as this editorial says . but got WA, I also took care of some boundary cases. can anyone tell mistake in my code.? my solution ( I am storing common diff. of the A.P. starting from 0 and ends at pos i in pre[i] else -1 ) thanks in advance :) answered 19 Sep '16, 00:29 1 accept rate: 0% Your code fails for this test case 1 5 2 4 6 8 10 Your code gives 10 but actual answer is 2 ( we have to output the smallest number) (19 Sep '16, 00:36) thanks! it has been submitted! (19 Sep '16, 00:42)
 0 can anybody explain me why we need to initialise the dp with f[0]=f[1]=f[2]=true ? answered 19 Sep '16, 10:32 4★rdrsadhu 134●7 accept rate: 11% f[0] means "Is it an AP with first 0 elements?" - Yes it is. So, f[0] = true f[1] means "Is it an AP with first 1 elements?" - Yes, a single element is in AP so, f[1] = true; similarly, f[2] is true since any two numbers will always be in AP. And same is true if we took elements from backwards. Hope it helps. (20 Sep '16, 14:05) easy_2★
 0 Please anybody explain why we need to intialise the dp with f[0]=f[1]=f[2]= true and why we took g[n+1]=g[n]=g[n-1]=true. Please make it clear answered 19 Sep '16, 15:15 1 accept rate: 0% f[0] means "Is it a AP with 0 first elements?" - Yes it is. So, f[0] = true f[1] means "--------------- 1 --------------?" - Yes, a single element is in AP so, f[1] = true; similarly, f[2] is true since any two numbers will always in AP. And same is true if we took elements from backwards. Hope it helps. (20 Sep '16, 14:06) easy_2★
 0 I Think i did this in a different way - https://ideone.com/Zj8LHm answered 19 Sep '16, 15:46 4★avvinci 60●3 accept rate: 0%
 0 can someone point out mistake in this solution.I tried all the test cases mentioned above.I have considered three cases: when a1=1St term and a[2] is second term of AP(deque x); when a1=1st term and a[3] is second term of AP(deque y); when a[2]=1st term and a[3] is second term of AP(deque z); answered 19 Sep '16, 16:44 1 accept rate: 0%
 0 Its just a ad hoc for me.. in this only 4 cases will be made.. Firstly array can be increasing order or decreasing order, so Make algo keeping in mind of smallest one. 1. When N<4, then just take minimum of all them. 2. Leave 1st element and check difference from arr[3] to arr[N-1] by taking d=arr[2]-arr[1], If loop iterates through whole element then just take only ans=arr0; 3. Leave 2nd element and check difference from arr[3] to arr[N-1] by taking d=arr[2]-arr[0], If loop iterates through whole element then just take only ans=arr1; 4. This case is boundary or will cover all others like ,d=arr[1]-arr[0].. Loop iterate from arr[2] to arr[N-1] and if condition fail one time then we just store the position of that element and iterate through loop... Here if loop iterates to end without breaking path then we just take minimum of 1st element and last element and print ans otherwise we print the element of that alteration.. To know more Just see my code..https://www.codechef.com/viewsolution/11570665 answered 19 Sep '16, 22:47 2.8k●1●4●17 accept rate: 16%
 0 Can someone help me with my Java solution as well? I checked for corner cases, then stored the count of differences. If there was only one entry, then either first or last element is the answer. If more than three entries (if only only term is wrong then only 2 or 3 different kinds of differences can exist), then invalid solution. Else, I've found the difference which occurs maximum number of times and checked each pair of elements afterwards. Submission answered 19 Sep '16, 23:10 70●1●5 accept rate: 16%
 0 Can someone point out the mistake in my solution https://www.codechef.com/viewsolution/11575414 I have tried all test cases mentioned above, and it gives the expected answer for all.I divided the problem into three cases, if first element is to be removed, last element is to be removed or any other element except these two is to be removed to convert the sequence into AP. answered 20 Sep '16, 19:52 1 accept rate: 0%
 0 I was getting TLE when I used cin - cout and with the statement "ios::sync_with_stdio(0);". But then I simply changed these input output statements to scanf and printf: AC in 0.06 seconds. answered 26 Oct '16, 13:21 4★mjguru 1●1 accept rate: 0%
 0 can anyone help me with my python submission?i retried a few times and tested all the test cases i can think and find but it works correctly when i was using repl.it,however codechef just simply repeat 'Wrong answer',i cant get why link text answered 11 Nov '16, 16:26 1 accept rate: 0%
 0 You just have to store the difference of adjacent values and count them. If you remove y from x,y,z sequence then you have to check if count of [z-x] + 1 should be equal to N-2. This is the simplest approach. answered 20 Mar, 03:23 30●2 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,029
×3,469
×1,901
×42
×13

question asked: 18 Sep '16, 15:56

question was seen: 5,838 times

last updated: 13 Jul, 11:35