1. Astronomy Board Game In an astronomy board game, N planets in an imaginary universe do not follow the normal law of gravitation. All the planets are positioned in a row. The planetary system can be in a stable state only if the sum of the mass of all planets at even positions is equal to the sum of the mass of planets at the odd positions. Initially, the system is not stable, but a player can destroy one planet to make it stable. Find the planet that should be destroyed to make the system stable. If no such planet exists, then return -1. If there are multiple such planets, then destroy the planet with the smallest index and return the index of the destroyed planet. Example Let N=5 and planets = [2,4,6,3,4]. Destroying the fourth planet of mass 3 will result in planets= [2,4,6,4], and here, the sum of odd positioned planets is (2+6)=8, and the sum of even positioned planets is (4+4)=8, and both are equal now. Hence, we destroy the fourth planet. Function Description Complete the function getPlanet To Destroy in the editor below. getPlanetToDestroy has the following parameter(s): planets[planets[1]....planets[n]]: An array of integers Returns int: the index of the planet to be destroyed. Constraints • 2≤N≤2x105 • 1splanets[i] ≤ 10⁹
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images