Maximize the value of x + y + z such that ax + by + cz = n
Last Updated :
23 Jun, 2022
Given integers n, a, b and c, the task is to find the maximum value of x + y + z such that ax + by + cz = n.
Examples:
Input:
n = 10
a = 5
b = 3
c = 4
Output:
3
Explanation:
x = 0, y = 2 and z = 1
Input:
n = 50
a = 8
b = 10
c = 2
Output:
25
Explanation:
x = 0, y = 0 and z = 25
Approach: Fix the values of x and y then the value of z can be calculated as z = (n – (ax + by)) / c. If current value of z is an integer then update the maximum value of x + y + z found so far.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxResult( int n, int a, int b, int c)
{
int maxVal = 0;
for ( int i = 0; i <= n; i += a)
{
for ( int j = 0; j <= n - i; j += b)
{
float z = ( float )(n - (i + j)) / ( float )(c);
if ( floor (z) == ceil (z))
{
int x = i / a;
int y = j / b;
maxVal = max(maxVal, x + y + ( int )z);
}
}
}
return maxVal;
}
int main()
{
int n = 10, a = 5, b = 3, c = 4;
cout << maxResult(n, a, b, c);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxResult( int n, int a, int b, int c)
{
int maxVal = 0 ;
for ( int i = 0 ; i <= n; i += a)
for ( int j = 0 ; j <= n - i; j += b) {
float z = ( float )(n - (i + j)) / ( float )c;
if (Math.floor(z) == Math.ceil(z)) {
int x = i / a;
int y = j / b;
maxVal
= Math.max(maxVal, x + y + ( int )z);
}
}
return maxVal;
}
public static void main(String args[])
{
int n = 10 , a = 5 , b = 3 , c = 4 ;
System.out.println(maxResult(n, a, b, c));
}
}
|
Python3
from math import *
def maxResult(n, a, b, c):
maxVal = 0
for i in range ( 0 , n + 1 , a):
for j in range ( 0 , n - i + 1 , b):
z = (n - (i + j)) / c
if (floor(z) = = ceil(z)):
x = i / / a
y = j / / b
maxVal = max (maxVal, x + y + int (z))
return maxVal
if __name__ = = "__main__" :
n = 10
a = 5
b = 3
c = 4
print (maxResult(n, a, b, c))
|
C#
using System;
class GFG {
static int maxResult( int n, int a, int b, int c)
{
int maxVal = 0;
for ( int i = 0; i <= n; i += a)
for ( int j = 0; j <= n - i; j += b) {
float z = ( float )(n - (i + j)) / ( float )c;
if (Math.Floor(z) == Math.Ceiling(z)) {
int x = i / a;
int y = j / b;
maxVal
= Math.Max(maxVal, x + y + ( int )z);
}
}
return maxVal;
}
public static void Main(String[] args)
{
int n = 10, a = 5, b = 3, c = 4;
Console.WriteLine(maxResult(n, a, b, c));
}
}
|
PHP
<?php
function maxResult( $n , $a , $b , $c )
{
$maxVal = 0;
for ( $i = 0; $i <= $n ; $i += $a )
for ( $j = 0; $j <= $n - $i ; $j += $b )
{
$z = ( $n - ( $i + $j )) / $c ;
if ( floor ( $z ) == ceil ( $z ))
{
$x = (int)( $i / $a );
$y = (int)( $j / $b );
$maxVal = max( $maxVal , $x + $y + (int) $z );
}
}
return $maxVal ;
}
$n = 10;
$a = 5;
$b = 3;
$c = 4;
echo maxResult( $n , $a , $b , $c );
?>
|
Javascript
<script>
function maxResult(n, a, b, c)
{
let maxVal = 0;
for (let i = 0; i <= n; i += a)
for (let j = 0; j <= n - i; j += b) {
let z = (n - (i + j)) / c;
if (Math.floor(z) == Math.ceil(z)) {
let x = i / a;
let y = j / b;
maxVal
= Math.max(maxVal, x + y + z);
}
}
return maxVal;
}
let n = 10, a = 5, b = 3, c = 4;
document.write(maxResult(n, a, b, c));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...