- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Program to find final states of rockets after collision in python

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 −

## Example

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))

## Input

[3, 8, 5, -5]

## Output

[3, 8]

- Related Questions & Answers
- C++ program to find first collision point of two series
- Final state of the string after modification in Python
- Program to get final string after shifting characters with given number of positions 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
- C++ code to find final number after min max removal game
- Program to find cost to reach final index of any of given two lists in Python
- Program to find state of prison cells after k days in python
- Program to find mean of array after removing some elements in Python
- Program to find expected growth of virus after time t in Python
- Program to find maximum binary string after change in python
- Program to find maximize score after n operations 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 minimum length of string after deleting similar ends in Python