Program to check if a body is in equilibrium or not - GeeksforGeeks

27 Apr, 2021
Given a 2D array a[][] where each row consists of N vector coordinates of the form (xi, yi, zi) of an applied body. If the body is in equilibrium, print “YES“. Otherwise, print “NO“.
Examples:
Input: a[][] = {{4, 1, 7}, {-2, 4, -1}, {1, -5, -3}}Output: NO
Input: a[][] = {{3, -1, 7}, {-5, 2, -4}, {2, -1, -3}}Output: YES
Approach: Follow the steps below to solve the problem:
Apply vector addition on all the vector coordinates.
If the result of the vector addition is (0, 0, 0), the body is in equilibrium. Therefore, print “YES“.
Otherwise, print “NO“.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ Program to implement// the above approach#include <iostream>using namespace std; // Function to calculate vector additionvoid vectorAddition(int a[][3], int N){     // Sum1 to store sum of xi    // Sum2 to store sum of yi    // Sum3 to store sum of zi    int sum1 = 0, sum2 = 0, sum3 = 0;     for (int i = 0; i < N; i++) {        sum1 += a[i][0];        sum2 += a[i][1];        sum3 += a[i][2];    }     // If the sum1, sum2 and sum3    // are all equal to zero    if (sum1 == 0 && sum2 == 0        && sum3 == 0) {         // Body is in        // equilibrium        cout << "YES";    }    else {         // Body is not in        // equilibrium        cout << "NO";    }} // Driver Codeint main(){    int N = 3;     int a[N][3]        = { { 3, -1, 7 },            { -5, 2, -4 },            { 2, -1, -3 } };     vectorAddition(a, N);     return 0;}
// Java Program to implement// the above approachimport java.util.*;class GFG{ // Function to calculate// vector additionstatic void vectorAddition(int a[][],                           int N){  // Sum1 to store sum of xi  // Sum2 to store sum of yi  // Sum3 to store sum of zi  int sum1 = 0, sum2 = 0, sum3 = 0;   for (int i = 0; i < N; i++)  {    sum1 += a[i][0];    sum2 += a[i][1];    sum3 += a[i][2];  }   // If the sum1, sum2 and sum3  // are all equal to zero  if (sum1 == 0 && sum2 == 0 &&      sum3 == 0)  {    // Body is in    // equilibrium    System.out.print("YES");  }  else  {    // Body is not in    // equilibrium    System.out.print("NO");  }} // Driver Codepublic static void main(String[] args){  int N = 3;  int a[][] = {{3, -1, 7},               {-5, 2, -4},               {2, -1, -3}};  vectorAddition(a, N);}} // This code is contributed by shikhasingrajput
# Python3 program to implement# the above approach # Function to calculate vector additiondef vectorAddition(a, N):         # Sum1 to store sum of xi    # Sum2 to store sum of yi    # Sum3 to store sum of zi    sum1 = 0    sum2 = 0    sum3 = 0     for i in range(N):        sum1 += a[i][0]        sum2 += a[i][1]        sum3 += a[i][2]     # If the sum1, sum2 and sum3    # are all equal to zero    if (sum1 == 0 and sum2 == 0 and        sum3 == 0):         # Body is in        # equilibrium        print("YES")     else:         # Body is not in        # equilibrium        print("NO") # Driver Codeif __name__ == '__main__':         N = 3    a = [ [ 3, -1, 7 ],          [ -5, 2, -4 ],          [ 2, -1, -3 ] ]     vectorAddition(a, N) # This code is contributed by mohit kumar 29
// C# Program to implement// the above approachusing System;class GFG{ // Function to calculate// vector additionstatic void vectorAddition(int[,]a,                           int N){  // Sum1 to store sum of xi  // Sum2 to store sum of yi  // Sum3 to store sum of zi  int sum1 = 0, sum2 = 0, sum3 = 0;   for (int i = 0; i < N; i++)  {    sum1 += a[i, 0];    sum2 += a[i, 1];    sum3 += a[i, 2];  }   // If the sum1, sum2 and sum3  // are all equal to zero  if (sum1 == 0 && sum2 == 0 &&      sum3 == 0)  {    // Body is in    // equilibrium    Console.Write("YES");  }  else  {    // Body is not in    // equilibrium    Console.Write("NO");  }} // Driver Codepublic static void Main(String[] args){  int N = 3;  int[,]a = {{3, -1, 7},             {-5, 2, -4},             {2, -1, -3}};  vectorAddition(a, N);}} // This code is contributed by shikhasingrajput
<script> // Javascript program for the above approach // Function to calculate// vector additionfunction vectorAddition(a, N){  // Sum1 to store sum of xi  // Sum2 to store sum of yi  // Sum3 to store sum of zi  let sum1 = 0, sum2 = 0, sum3 = 0;    for (let i = 0; i < N; i++)  {    sum1 += a[i][0];    sum2 += a[i][1];    sum3 += a[i][2];  }    // If the sum1, sum2 and sum3  // are all equal to zero  if (sum1 == 0 && sum2 == 0 &&      sum3 == 0)  {    // Body is in    // equilibrium    document.write("YES");  }  else  {    // Body is not in    // equilibrium    document.write("NO");  }} // Driver Code   let N = 3;  let a = [[3, -1, 7],               [-5, 2, -4],               [2, -1, -3]];  vectorAddition(a, N);  </script> </script>
YES
Time Complexity: O(N)Auxiliary Space: O(1)
mohit kumar 29
shikhasingrajput
chinmoy1997pal
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java