Number of solutions for the equation x + y + z <= n
Last Updated :
26 Aug, 2022
Given four numbers x, y, z, n. The task is to find the number of solutions for the equation x + y + z <= n, such that 0 <= x <= X, 0 <= y <= Y, 0 <= z <= Z.
Examples:
Input: x = 1, y = 1, z = 1, n = 1
Output: 4
Input: x = 1, y = 2, z = 3, n = 4
Output: 20
Approach: Let’s iterate explicitly over all possible values of x and y (using nested loop). For one such fixed values of x and y, the problem reduces to how many values of z are there such that z <= n – x – y and 0 <= z <= Z.
Below is the required implementation to find the number of solutions:
C++
#include <bits/stdc++.h>
using namespace std;
int NumberOfSolutions( int x, int y, int z, int n)
{
int ans = 0;
for ( int i = 0; i <= x; i++) {
for ( int j = 0; j <= y; j++) {
int temp = n - i - j;
if (temp >= 0) {
temp = min(temp, z);
ans += temp + 1;
}
}
}
return ans;
}
int main()
{
int x = 1, y = 2, z = 3, n = 4;
cout << NumberOfSolutions(x, y, z, n);
return 0;
}
|
C
#include <stdio.h>
int min( int a, int b)
{
int min = a;
if (min > b)
min = b;
return min;
}
int NumberOfSolutions( int x, int y, int z, int n)
{
int ans = 0;
for ( int i = 0; i <= x; i++) {
for ( int j = 0; j <= y; j++) {
int temp = n - i - j;
if (temp >= 0) {
temp = min(temp, z);
ans += temp + 1;
}
}
}
return ans;
}
int main()
{
int x = 1, y = 2, z = 3, n = 4;
printf ( "%d" ,NumberOfSolutions(x, y, z, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int NumberOfSolutions( int x, int y, int z, int n)
{
int ans = 0 ;
for ( int i = 0 ; i <= x; i++) {
for ( int j = 0 ; j <= y; j++) {
int temp = n - i - j;
if (temp >= 0 ) {
temp = Math.min(temp, z);
ans += temp + 1 ;
}
}
}
return ans;
}
public static void main (String[] args) {
int x = 1 , y = 2 , z = 3 , n = 4 ;
System.out.println( NumberOfSolutions(x, y, z, n));
}
}
|
Python 3
def NumberOfSolutions(x, y, z, n) :
ans = 0
for i in range (x + 1 ) :
for j in range (y + 1 ) :
temp = n - i - j
if temp > = 0 :
temp = min (temp, z)
ans + = temp + 1
return ans
if __name__ = = "__main__" :
x, y, z, n = 1 , 2 , 3 , 4
print (NumberOfSolutions(x, y, z, n))
|
C#
using System;
public class GFG{
static int NumberOfSolutions( int x, int y, int z, int n)
{
int ans = 0;
for ( int i = 0; i <= x; i++) {
for ( int j = 0; j <= y; j++) {
int temp = n - i - j;
if (temp >= 0) {
temp = Math.Min(temp, z);
ans += temp + 1;
}
}
}
return ans;
}
static public void Main (){
int x = 1, y = 2, z = 3, n = 4;
Console.WriteLine( NumberOfSolutions(x, y, z, n));
}
}
|
PHP
<?php
function NumberOfSolutions( $x , $y , $z , $n )
{
$ans = 0;
for ( $i = 0; $i <= $x ; $i ++)
{
for ( $j = 0; $j <= $y ; $j ++)
{
$temp = $n - $i - $j ;
if ( $temp >= 0)
{
$temp = min( $temp , $z );
$ans += $temp + 1;
}
}
}
return $ans ;
}
$x = 1; $y = 2;
$z = 3; $n = 4;
echo NumberOfSolutions( $x , $y , $z , $n );
?>
|
Javascript
<script>
function NumberOfSolutions(x, y, z, n)
{
var ans = 0;
for ( var i = 0; i <= x; i++)
{
for ( var j = 0; j <= y; j++)
{
var temp = n - i - j;
if (temp >= 0)
{
temp = Math.min(temp, z);
ans += temp + 1;
}
}
}
return ans;
}
var x = 1, y = 2, z = 3, n = 4;
document.write(NumberOfSolutions(x, y, z, n));
</script>
|
Time Complexity: O(x * y)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...