
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Number of solutions for the equation x + y + z <= n using C++
In this article, we will explain the approaches to find the number of solutions for the equation x+y+z<=n. In this problem, we have an equation with four variables, and the task is to find the solution for the given equation. So here is a simple example &miuns;
Input: X = 1, Y = 1, Z = 1, n = 1 Output: 4 Input: X = 1, Y = 2, Z = 3, n = 4 Output: 3
In this problem, we can simply go through all the values of (x, y), (y,z), (x,z) by isolating each variable and checking if it satisfies the equation or not.
Approach to Find the Solution
Now we will use the Brute Force approach to find the solution to the given problem.
Brute Force
In this program we are going to go through all the possible values of (x,y), (y,z) and (x,z) such that it satisfies the equation z <= n - x - y (here z is isolated) where 0 <= z <= Z (and same for other isolated variables).
Example
#include<bits/stdc++.h> using namespace std; int main(){ int X = 1, Y = 2, Z = 3, n = 4; // limits of x, y, z and given n. int answer = 0; // counter variable. for(int i = 0; i <= X; i++){ for(int j = 0; j <= Y; j++){ int temp = (n - i) - j; // temp = n - x - y. if(temp >= Z){ // if n - x - y >= z so we increment the answer. answer++; } } } for(int i = 0; i <= X; i++){ for(int j = 0; j <= Z; j++){ int temp = (n - i) - j; // temp = n - x - y. if(temp >= Y){ // if n - x - y >= z so we increment the answer. answer++; } } } for(int i = 0; i <= Z; i++){ for(int j = 0; j <= Y; j++){ int temp = (n - i) - j; // temp = n - x - y. if(temp >= X){ // if n - x - y >= z so we increment the answer. answer++; } } } cout << answer << "\n"; }
Output
17
Explanation of the Above Program
In this program, we are going to go through all the combinations of (x,y), (y, z), (x,z) using a nested for loop and check the equation if it satisfies the equation or not and if it satisfies then, we increment the answer.
Conclusion
In this article, we solve a problem finding the Number of solutions that satisfy the equation x + y + z<= n in O(X*Y) time complexity. We also learned the C++ program for this problem and the complete approach by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages.
- Related Articles
- Find the product of $(-3 x y z)(\frac{4}{9} x^{2} z)(-\frac{27}{2} x y^{2} z)$ and verify the result for ; $x=2, y=3$ and $z=-1$
- Find the values of x, y, z."\n
- In \( \Delta X Y Z, X Y=X Z \). A straight line cuts \( X Z \) at \( P, Y Z \) at \( Q \) and \( X Y \) produced at \( R \). If \( Y Q=Y R \) and \( Q P=Q Z \), find the angles of \( \Delta X Y Z \).
- Verify the property \( x \times(y+z)=(x \times y)+(x \times z) \) for the given values of \( x,\ y \) and \( z \).\( x=\frac{-5}{2}, y=\frac{1}{2} \) and \( z=-\frac{10}{7} \)>
- Find the number of solutions to the given equation in C++
- Find the values of x , y and z."\n
- Find four solutions of $x + y=10$
- If $x=1,\ y=2$ and $z=5$, find the value of $x^{2}+y^{2}+z^{2}$.
- In the following equation, find which variables $x, y, z$ etc. represent rational or irrational numbers:\( z^{2}=0.04 \)
- Subtract $3 x y+5 y z-7 z x$ from $5 x y-2 y z-2 z x+10 x y z$.
- In the following equation, find which variables $x, y, z$ etc. represent rational or irrational numbers:\( y^{2}=9 \)
- Subtract x²-y²+z² from x²-y²- z²
- Find the Number of Solutions of n = x + n x using C++
- If $2^x \times 3^y \times 5^z = 2160$, find $x, y$ and $z$. Hence, compute the value of $3^x \times 2^{-y} \times 5^{-z}$.
- In the following equation, find which variables $x, y, z$ etc. represent rational or irrational numbers:\( x^{2}=5 \)
