Python: 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.

def main():
    n = int(input())
    x = y = z = 0

    for _ in range(n):
        a, b, c = map(int, input().split())
        x += a
        y += b
        z += c

    print("YES" if x == y == z == 0 else "NO")

if __name__ == "__main__":
    main()

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!