The News Hub

# Reduce the instructions following the given conditions – GeeksforGeeks

Given an array dir_list[] that represents direction where the valid directions are “UP”, “Left”, “Right” and “Down”. Your goal is to reduce the instructions by eliminating those that contradict one another, or those consecutive instructions that are opposite to one another. If the list becomes empty, print -1.

Note: UP and Down are opposite to each other and Left and Right are also opposite to each other.

Examples:

Input: dir_list = [“UP”, “Left”, “Right”, “Down”, “Right”, “UP”]
Output: [‘Right’, ‘UP’]
Explanation: Direction at index 1 and 2 cancel out each other. After their removal, dir_list become [“UP”, “Down”, “Right”, “UP”] Direction at index 0 and 1 also cancel out each other. After their removal, dir_list become [“Right”, “UP”]

Input: dir_list = [“UP”, “Down”]
Output: []
Explanation: UP and Down are consecutively opposite to each other so, they are cancel out each other leaving an empty list.

Approach: This can be solved using a stack.

The idea is to eliminate the opposite directions by comparing top element of stack and last element of list.

Follow the below steps to implement the idea:

• First check if the List is empty or has only a single element, then, return the list itself.
• Create a stack, append the last element of the list to the stack, and pop it from the list.
• While the list is not empty
• If the stack is empty, pop the last element of the list and append it to the stack
• Set a = top element of the stack and pop the element and b = last element of the list and pop the element
• Check if a and b are not opposed to each other, append a and b into the stack respectively.
• After executing the loop, reverse the stack and return it.

Below is the implementation of the above approach.

## Python3

 ` `  ` `  ` `  `def` `isOpposite(a, b):` ` `  `    ``if` `(a ``=``=` `"UP"` `and` `b ``=``=` `"Down"``) ``or` `(a ``=``=` `"Down"` `and` `b ``=``=` `"UP"``):` `        ``return` `1` ` `  `    ``if` `(a ``=``=` `"Left"` `and` `b ``=``=` `"Right"``) ``or` `(a ``=``=` `"Right"` `and` `b ``=``=` `"Left"``):` `        ``return` `1` ` `  `    ``else``:` `        ``return` `0` ` `  ` `  ` `  `def` `reduceDir(l):` ` `  `    ` `    ` `    ``if` `len``(l) ``=``=` `0` `or` `len``(l) ``=``=` `1``:` `        ``return` `l` ` `  `    ` `    ` `    ` `    ``stack ``=` `[]` `    ``stack.append(l.pop())` ` `  `    ` `    ``while` `len``(l) !``=` `0``:` ` `  `        ` `        ``if` `len``(stack) ``=``=` `0``:` `            ``stack.append(l.pop())` `            ``continue` ` `  `        ` `        ``a ``=` `stack.pop()` `        ``b ``=` `l.pop()` ` `  `        ` `        ` `        ``if` `isOpposite(a, b) !``=` `1``:` ` `  `            ` `            ` `            ` `            ` `            ``stack.append(a)` `            ``stack.append(b)` ` `  `    ` `    ``stack.reverse()` ` `  `    ` `    ``return` `stack` ` `  ` `  ` `  `if` `__name__ ``=``=` `"__main__"``:` ` `  `    ``dir_list ``=` `[``"UP"``, ``"Left"``, ``"Right"``, ``"Down"``, ``"Right"``, ``"UP"``]` ` `  `    ``res_list ``=` `reduceDir(dir_list)` ` `  `    ``if``(``len``(res_list) ``=``=` `0``):` `        ``print``(``-``1``)` `    ``else``:` `        ``print``(res_list)`

Time Complexity: O(N)
Auxiliary Space: O(N)