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

Valence number of an atom is defined as the exact number of bonds the atom must form with other atoms. Given valence number of 3 atoms, the task is to determine if they can form a molecule together or not. Atoms can form multiple bonds with each other.

**Examples:**

Input: 2 4 2 Output: YES The bonds are between the following atoms: 1 - 2 1 - 2 2 - 3 2 - 3 Input: 1 2 3 Output: NO

**Approach:** Let the valence numbers be a, b and c. Let c be the largest. We have 2 cases in which the molecule cannot be formed:

**a+b+c is odd:**Since every bond decreases the valence number of 2 atoms by 1, the sum of valence numbers should be an even number.**a+b < c:**In this case, c will be left unsatisfied even if every bond is formed with it.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function to check if it is possible` `void` `printPossible(` `int` `a, ` `int` `b, ` `int` `c)` `{` ` ` `if` `((a + b + c) % 2 != 0 || a + b < c)` ` ` `cout << ` `"NO"` `;` ` ` `else` ` ` `cout << ` `"YES"` `;` `}` ` ` `// Driver code` `int` `main()` `{` ` ` `int` `a = 2, b = 4, c = 2;` ` ` `printPossible(a, b, c);` ` ` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the above approach ` ` ` `import` `java.io.*;` ` ` `class` `GFG {` ` ` `// Function to check if it is possible ` `static` `void` `printPossible(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `if` `((a + b + c) % ` `2` `!= ` `0` `|| a + b < c) ` ` ` `System.out.println(` `"NO"` `); ` ` ` `else` ` ` `System.out.println(` `"YES"` `); ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) {` ` ` ` ` `int` `a = ` `2` `, b = ` `4` `, c = ` `2` `; ` ` ` `printPossible(a, b, c); ` ` ` `} ` `} ` ` ` `// This code is contributed by akt_mit` |

## Python3

`# Python 3 implementation of the` `# above approach ` ` ` `# Function to check if it is possible ` `def` `printPossible( a, b, c):` ` ` ` ` `if` `((a ` `+` `b ` `+` `c) ` `%` `2` `!` `=` `0` `or` `a ` `+` `b < c):` ` ` `print` `(` `"NO"` `)` ` ` `else` `:` ` ` `print` `(` `"YES"` `)` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `a ` `=` `2` ` ` `b ` `=` `4` ` ` `c ` `=` `2` ` ` `printPossible(a, b, c)` ` ` `# This code is contributed ` `# by ChitraNayal` |

## C#

`// C# implementation of the above approach` `using` `System;` ` ` `class` `GFG` `{` ` ` `// Function to check if it is possible` `static` `void` `printPossible(` `int` `a, ` `int` `b, ` `int` `c)` `{` ` ` `if` `((a + b + c) % 2 != 0 || a + b < c)` ` ` `Console.Write(` `"NO"` `);` ` ` `else` ` ` `Console.Write(` `"YES"` `);` `}` ` ` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `int` `a = 2, b = 4, c = 2;` ` ` `printPossible(a, b, c);` `}` `}` ` ` `// This code is contributed` `// by Akanksha Rai` |

## PHP

`<?php` `// PHP implementation of the above approach` ` ` `// Function to check if it is possible` `function` `printPossible(` `$a` `, ` `$b` `, ` `$c` `)` `{` ` ` `if` `((` `$a` `+ ` `$b` `+ ` `$c` `) % 2 != 0 || ` ` ` `$a` `+ ` `$b` `< ` `$c` `)` ` ` `echo` `(` `"NO"` `);` ` ` `else` ` ` `echo` `(` `"YES"` `);` `}` ` ` `// Driver code` `$a` `= 2;` `$b` `= 4;` `$c` `= 2;` `printPossible(` `$a` `, ` `$b` `, ` `$c` `);` ` ` `// This code is contributed ` `// by Shivi_Aggarwal` `?>` |

**Output:**

Yes

**Time complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.