The GOCC was held on July 9. There were 2 coding questions to attempt, with a 60-minute time limit for completion. Below is the first question of the challenge:
You have designed a robot. The robot can move only left or right based on the given instruction. The robot is initially at point 0 on the X-axis. You are given N pairs of instructions. Each pair of instruction contains an integer and a character ('L' or 'R'). For example consider an instruction (3,R), It means that robot has to move 3 units right from current position along X-axis. You are allowed to perform the following Operation at most once. =>You can remove any number of consecutive instructions but condition is that the number of left and right instructions should be equal For example, You can remove (2,L)(3,R) or (2,L)(3,R)(7,R)(1,L) But cant remove (1,L) or (2,R)(2,R)(4,L) You are required to find Maximum distance robot can go from initial position 0.
1<=T<=10 1<=N<=1e5 0<=Di<=1e6 (Integer in the input instruction)
Input: T=1,N=5,instructions{(2,L)(3,R)(4,L)(1,R)(5,L)}. Output: 8. Explanation: Remove first and second Instructions.
I tried to do it by using kadane's algo 2 times First time I considered the Di related to L as postive values and the Di related to Ri as negative values and the second time vice versa. I now realise it was a wrong approach as I couldn't maintain the condition that the number of consecutive L and R instructions which can be removed should be equal. How can we calculate the maximum negative value which can be removed from our total sum by considering all the substrings which have equal no of L and R instructions. I can do it by brute force by considering all the combinations of different substrings of even length and calculating the maximum negative value that can be generated. But I am not able to optimise it. Am I thinking correctly? Any help will be appreciated.