To determine if the forces acting on an object are in equilibrium, we
sum the components
of the
forces in the
x, y, and z directions
.
If the
sum of all components
in
each direction is
zero
,
the forces are in equilibrium, and the object is stationary.
import java.util.Scanner; public class CF { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int noOfForces = sc.nextInt(); int xForces =0, yForces=0, zForces=0; for(int i=0;i<noOfForces;i++){ xForces += sc.nextInt(); yForces += sc.nextInt(); zForces += sc.nextInt(); } if(xForces==0 && yForces==0 && zForces==0) System.out.println("YES"); else System.out.println("NO"); } }
The algorithm iterates through the input once, summing the components of the forces in each direction. This results in a linear time complexity.
The algorithm uses a constant amount of extra space to store the sums of the force components.