Make Chemical Formula A chemical formula is made using elements that are represented by…
- User
Make Chemical Formula A chemical formula is made using elements that are represented by some letters of the English alphabet (A- 2). Each letter has some mass. 'A' has a mass of 1. B' has a mass of 2 and so on, till 2, which has a mas of 26 The total mass of a chemical formula is calculated by adding the individual masses of each element present in the formula You are given an integer N which represents the total mass of a possible chemical formula Your tack is to find and return all the possible unique chemical formulae that can be formed using the given total mass mus. No element can be used more than twice. Note: The order of elements in the chemical formule does not mattes Input Specification: input1: An integer value N. representing the total mass of a possible chemical formula Output Specification: Return all the possible unique chemical formulae that can be formed using the given total mais where no element can be used more than twice. Example 1: input1:4 Output: 4 Explanation: Given the total mass 4. following are the possible unique chemical formular Formula "D" with the mass 4 Formula "CA" with the mass 3+1=4 Formula "88" with the mass 2+2=4 Formula "BAA" with the mass 2+1+1=4Output: 4 Explanation: Given the total mass 4 following are the possible unique chemical formular Formula "D" with the mass 4 Formula "CA" with the mass 3+1=4 Formula "88" with the mass 2+2=4 Formula "BAA" with the mass 2 + 1 + 1 = 4 Since there are 4 possible unique formulas therefore. 4 is returned as the output. Example 2: input1:7 Output:9 Explanation: Given the total mass 7. following are the possible unique chemical formular Formula 'G' with the mass 7. Formula "FA" with the man 6+17 Formula "Ell" with the mass 5-2-7. Formula "EAA" with the mass 5 + 1 + 1 = 7 Formula "DC" with the mass 4+37. Formula "DBA" with the mass 4 + 2 + 1 = 7 Formula "CCA" with the mass 3 + 3 + 1 = 3 Formula "CBS" with the mass 3 + 2 + 2 = 2 Formula "CBAA" with the mass 3 + 2 + 1 + 1 = 7 Since there are 9 possibile unique formulae 9 is returned as the output code in cpp
- Assistantcpp
#include <bits/stdc++.h> using namespace std; // Function to count the number of unique chemical formulas long long countFormulas(int N) { // Initialize a DP array where dp[j] represents the number of ways to achieve mass j // using elements with mass from 1 to 26, each used at most twice vector<long long> dp(N + 1, 0); dp[0] = 1; // Base case: one way to achieve mass 0 (using no elements) // Iterate through each possible element mass from 1 to 26 for(int m = 1; m <= 26; m++) { // Iterate through the masses in reverse to avoid using the same element more than twice for(int j = N; j >= m; j--) { // Add the number of ways to form the current mass by adding one instance of mass m dp[j] += dp[j - m]; // If possible, add the number of ways by adding a second instance of mass m if(j >= 2 * m) { dp[j] += dp[j - 2 * m]; } } } return dp[N]; } int main(){ int N; cin >> N; cout << countFormulas(N); }