Loading Now

onesAndZeros3

This challenge is based on onesAndZeros2, instead of having only ones and zeros, now you will be given an array of different intergers and each time, you will still choose any three consecutive elements of the same value and remove them from the array until there is no such three elements left .What is the minimum length of the array that can remain after applying  the described operation several times?

NOTE: Try to solve this in O(n), any solutions having passed by finding and removing a triple each time is just lucky because they run in O(n ** 2) !

Example

  • For arr = [0,0,1,2,2,2,1,1,0], the output should be onesAndZeros3(arr) = 0.
    First remove three twos and then romve three ones, finally remove the last three zeros and we are left with an array of length 0.

Input/Output

  • [execution time limit] 0.2 second

  • [input] an array of integers arr

    Guaranteed constraints:
    1 ≤ arr.length ≤ 10^5.

  • [output] int
    The minimum length of the array that may remain after applying the described operations several times.

 

Post Comment

Contact