- Related Questions & Answers
- C++ program to find first collision point of two series
- Final state of the string after modification in Python
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find minimum number of buses required to reach final target in python
- Program to find state of prison cells after k days in python
- Program to find cost to reach final index of any of given two lists in Python
- Program to find number of items left after selling n items in python
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find the final ranking of teams in order from highest to lowest rank in python
- Program to find longest number of 1s after swapping one pair of bits in Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find string after removing consecutive duplicate characters in Python
- Program to find most occurring number after k increments in python
- Program to find minimum number colors remain after merging in Python
- Program to find maximum difference of adjacent values after deleting k numbers in python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums and that is representing rocket's size and direction. Positive integer indicates right, and negative number represents left. And the absolute value of the number represents the rocket's size. Now when two rockets collide, the smaller one will be destroyed and the larger one will continue on its journey unchanged. When they are the same size rockets and they collide, they'll both destroy. When two rockets are moving in the same direction, they will never collide (assuming rockets speed are same). We have to find the state of the rockets after all collisions.

So, if the input is like nums = [3, 8, 5, -5], then the output will be [3, 8], as 5 and -5 will be destroyed, remaining will survive.

To solve this, we will follow these steps −

- ls := a new list with one element nums[0]
- for i in range 1 to size of nums - 1, do
- if nums[i] >= 0, then
- insert nums[i] at the end of ls

- otherwise,
- insert nums[i] at the end of ls
- j := size of ls - 2
- while j >= 0 and ls[j] >= 0, do
- if |last element of ls| > ls[j], then
- delete jth element from ls

- otherwise when |last element of ls| is same as ls[j], then
- delete jth element from ls
- delete last element from ls
- come out from the loop

- otherwise,
- delete -last element from ls
- come out from the loop

- j := j - 1

- if |last element of ls| > ls[j], then

- if nums[i] >= 0, then
- return ls

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums): ls = [nums[0]] for i in range(1, len(nums)): if nums[i] >= 0: ls.append(nums[i]) else: ls.append(nums[i]) j = len(ls) - 2 while j >= 0 and ls[j] >= 0: if abs(ls[-1]) > ls[j]: ls.pop(j) elif abs(ls[-1]) == ls[j]: ls.pop(j) ls.pop(-1) break else: ls.pop(-1) break j -= 1 return ls ob = Solution() nums = [3, 8, 5, -5] print(ob.solve(nums))

[3, 8, 5, -5]

[3, 8]

Advertisements