C++: Young Physicist

Thought Process

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.

#include <bits/stdc++.h>
using namespace std;

int main(){

    int xForces=0,yForces=0,zForces=0;
    int noOfForces, x,y,z;

    cin >> noOfForces;
    for(int i=0;i<noOfForces;i++){

        cin >> x;
        xForces += x;

        cin >> y;
        yForces += y;

        cin >> z;
        zForces += z;
    }
    if(xForces==0 && yForces==0 && zForces==0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;

    return 0;
}

Code Complexity

Time Complexity: O(n)

The algorithm iterates through the input once, summing the components of the forces in each direction. This results in a linear time complexity.

Space Complexity: O(1)

The algorithm uses a constant amount of extra space to store the sums of the force components.

Code copied!