Sunday, October 6, 2024
Google search engine
HomeData Modelling & AIProgram to check if a body is in equilibrium or not

Program to check if a body is in equilibrium or not

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++




// C++ Program to implement
// the above approach
#include <iostream>
using namespace std;
 
// Function to calculate vector addition
void 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 Code
int main()
{
    int N = 3;
 
    int a[N][3]
        = { { 3, -1, 7 },
            { -5, 2, -4 },
            { 2, -1, -3 } };
 
    vectorAddition(a, N);
 
    return 0;
}


Java




// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to calculate
// vector addition
static 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 Code
public 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




# Python3 program to implement
# the above approach
 
# Function to calculate vector addition
def 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 Code
if __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#




// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to calculate
// vector addition
static 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 Code
public 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


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to calculate
// vector addition
function 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>


Output

YES

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments