top of page
Search

Day 1 - Young Physicist | C++

  • Writer: Ayush Mahajan
    Ayush Mahajan
  • Aug 28, 2022
  • 1 min read

Updated: Sep 26, 2022


The journey starts from the simplest questions only. So starting with the first question of the list - Young Physicist. You should always try it out yourself first.


To solve this question remember that for a body to be in equilibrium, the sum for all x, y, and z vectors should be zero. You can code it this way:




#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int n;
    cin >> n;
    int x[n], y[n], z[n];
    for(int i = 0; i < n; i++){
        cin >> x[i] >> y[i] >> z[i];
    }
    // Calculating sum for x - axis;
    int x_sum = 0;
    for(int i = 0; i < n; i++){
        x_sum += x[i];
    }
    // Calculating sum for y - axis;
    int y_sum = 0;
    for(int i = 0; i < n; i++){
        y_sum += y[i];
    }
    // Calculating sum for z - axis;
    int z_sum = 0;
    for(int i = 0; i < n; i++){
        z_sum += z[i];
    }
    if(x_sum == 0 && y_sum == 0 && z_sum == 0){
        cout << "YES\n";
    }
    else {
        cout << "NO\n";
    }
}

Submission Link If you want to solve with less code -

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int n, x = 0, y = 0, z = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a,b,c;
        cin >> a >> b >> c;
        x+=a;
        y+=b;
        z+=c;
    }
    if(x == 0 && y == 0 && z == 0){
        cout << "YES";
    }
    else{
        cout << "NO";
    }
}


 
 
 

Comments


bottom of page