- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 Articles
- Program to get final string after shifting characters with given number of positions in Python
- Final state of the string after modification in Python
- C++ program to find first collision point of two series
- 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 cost to reach final index of any of given two lists in Python
- Program to find the final ranking of teams in order from highest to lowest rank 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 Final Prices With a Special Discount in a Shop in Python
- Program to find maximum binary string after change in python
- Program to find maximize score after n operations in Python
- C++ code to find final number after min max removal game
- Program to find number of items left after selling n items in python