- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

Now we will use the Brute Force approach to find the solution to the given problem.

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).

#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"; }

17

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.

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 Questions & Answers
- Find the Number of Solutions of n = x + n x using C++
- Find number of solutions of a linear equation of n variables in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Maximize the value of x + y + z such that ax + by + cz = n in C++
- Program to find number of solutions in Quadratic Equation in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- Count of values of x <= n for which (n XOR x) = (n – x) in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find larger of x^y and y^x in C++
- Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++
- Number of non-negative integral solutions of sum equation in C++
- Find the Number of Sextuplets that Satisfy an Equation using C++
- Count of pairs (x, y) in an array such that x < y in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- C/C++ Program for Number of solutions to the Modular Equations?

Advertisements