Find if a molecule can be formed from 3 atoms using their valence numbers in C++


As we know the valance number is the number that defines how-many bonds the atom must form with other atoms. We have the valance numbers of three atoms. We have to check whether they can make one molecule or not. Atoms can form multiple bonds with each other. So if the valance numbers are 2, 4, 2, then the output will be YES. As the bonds are like below −

1 – 2, 1 – 2, 2 – 3, 2 – 3.

Suppose the valance numbers are a, b and c. Consider c is largest. Then we have two cases in which they cannot form molecule −

  • a + b + c is odd. Since every bond decreases the valance number of two atoms by 1, then the sum will be even number
  • a + b < c, in this situation some unused connectors will be there.

Example

 Live Demo

#include<iostream>
using namespace std;
bool canMakeMolecule(int a, int b, int c) {
   if ((a + b + c) % 2 != 0 || a + b < c)
      return false;
   else
      return true;
}
int main() {
   int a = 2, b = 4, c = 2;
   if(canMakeMolecule(a, b, c)){
      cout << "They can form Molecule";
   } else {
      cout << "They can not form Molecule";
   }
}

Output

They can form Molecule

Updated on: 17-Dec-2019

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements