{"id":"9f33a328-ff7a-4740-8f51-26d358c4be2a","name":"Asteroid Collision","description":"1. You are given an array asteroids of integers representing asteroids in a row.\r\n2. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left).\r\n3. Each asteroid moves at the same speed.\r\n4. You need to find out the state of the asteroids after all collisions.\r\n5. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 2 &lt;= asteroids.length &lt;= 10^4\r\n2. -1000 &lt;= asteroids[i] &lt;= 1000\r\n3. asteroids[i] != 0","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> asteroidCollision(vector<int>& asteroids) {\n\n}\n\n\n\n\nint main()\n{\n int n;\n cin >> n;\n vector<int>arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n vector<int>result = asteroidCollision(arr);\n cout << result.size() << endl;\n for (int i = 0; i < result.size(); i++) {\n cout << result[i] << endl;\n\n }\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int[] asteroidCollision(int[] asteroids) {\r\n // complete this function\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n for (int i = 0; i < n; i++)arr[i] = Integer.parseInt(read.readLine());\r\n int result[] = asteroidCollision(arr);\r\n\r\n System.out.println(result.length);\r\n for (int e : result) {\r\n System.out.println(e);\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n3\r\n2\r\n-1\r\n3\r\n-3\r\n3\r\n-4\r\n","sampleOutput":"1\r\n-4\r\n","questionVideo":"https://www.youtube.com/embed/Y82zCeJft-Q","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"5fb2135f-3ff3-4faa-a1d1-6d24526c01d7","name":"Asteroid Collision","slug":"asteroid-collision","type":1}],"next":{"id":"d67b617e-4633-4d3d-845b-41dbe913e2f6","name":"Asteroid Collision Medium MCQ","type":0,"slug":"asteroid-collision-medium-mcq"},"prev":{"id":"2b66272e-8bda-4a0f-83b7-801c220a885e","name":"132 Pattern","type":3,"slug":"132-pattern"}}}

Asteroid Collision

1. You are given an array asteroids of integers representing asteroids in a row. 2. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). 3. Each asteroid moves at the same speed. 4. You need to find out the state of the asteroids after all collisions. 5. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

{"id":"9f33a328-ff7a-4740-8f51-26d358c4be2a","name":"Asteroid Collision","description":"1. You are given an array asteroids of integers representing asteroids in a row.\r\n2. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left).\r\n3. Each asteroid moves at the same speed.\r\n4. You need to find out the state of the asteroids after all collisions.\r\n5. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 2 &lt;= asteroids.length &lt;= 10^4\r\n2. -1000 &lt;= asteroids[i] &lt;= 1000\r\n3. asteroids[i] != 0","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> asteroidCollision(vector<int>& asteroids) {\n\n}\n\n\n\n\nint main()\n{\n int n;\n cin >> n;\n vector<int>arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n vector<int>result = asteroidCollision(arr);\n cout << result.size() << endl;\n for (int i = 0; i < result.size(); i++) {\n cout << result[i] << endl;\n\n }\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int[] asteroidCollision(int[] asteroids) {\r\n // complete this function\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n for (int i = 0; i < n; i++)arr[i] = Integer.parseInt(read.readLine());\r\n int result[] = asteroidCollision(arr);\r\n\r\n System.out.println(result.length);\r\n for (int e : result) {\r\n System.out.println(e);\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n3\r\n2\r\n-1\r\n3\r\n-3\r\n3\r\n-4\r\n","sampleOutput":"1\r\n-4\r\n","questionVideo":"https://www.youtube.com/embed/Y82zCeJft-Q","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"5fb2135f-3ff3-4faa-a1d1-6d24526c01d7","name":"Asteroid Collision","slug":"asteroid-collision","type":1}],"next":{"id":"d67b617e-4633-4d3d-845b-41dbe913e2f6","name":"Asteroid Collision Medium MCQ","type":0,"slug":"asteroid-collision-medium-mcq"},"prev":{"id":"2b66272e-8bda-4a0f-83b7-801c220a885e","name":"132 Pattern","type":3,"slug":"132-pattern"}}}
plane

Editor


Loading...

Asteroid Collision

medium

1. You are given an array asteroids of integers representing asteroids in a row. 2. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). 3. Each asteroid moves at the same speed. 4. You need to find out the state of the asteroids after all collisions. 5. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

Constraints

1. 2 <= asteroids.length <= 10^4 2. -1000 <= asteroids[i] <= 1000 3. asteroids[i] != 0

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

7 3 2 -1 3 -3 3 -4

Sample Output

1 -4

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode