JPMorgan | Removal Game | Intern 2020

Given an array of integers and a positive integer k, we need to remove maximum consecutive triplets from the array which form A.P with common difference k
After removing one triplet the array shrinks, i.e the elements which were far apart due to removal now become adjacent
For example :

[1, 2, 3, 3, 4, 5, 4, 2, 3, 6], k = 1

Approach 1
Removing indexes 3, 4 and 5 (since they are consecutive triplets which form A.P with difference 1) changes the array into

[1, 2, 3, 4, 2, 3, 6]

now removing indexes 1, 2 and 3 since they are consecutive triplets which form A.P with difference 1, we get

[1, 2, 3, 6]

We can now remove indexes 0, 1 and 2 and we get

[6]

No more removal are possible after this
Since we were able to do 3 remove operations the answer is 3 for this approach

Approach 2
For this test-case if we had proceeded by removing indexes 0, 1, 2 in first turn then array would have become

[3, 4, 5, 4, 2, 3, 6]

Removing indexes 0, 1 and 2 again would have lead to

[4, 2, 3, 6]

We could do 2 removals for this approach

Answer
Since in approach 1 we could do 3 removals and we have to report maximum removals that can be done so answer is

3
Comments (3)