Maximize the value of x + y + z such that ax + by + cz = n
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
x = 0, y = 2 and z = 1
Input: n = 50, a = 8, b = 10, c = 2
Output: 25
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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) int maxResult(int n, int a, int b, int c) { int maxVal = 0; // i represents possible values of a * x for (int i = 0; i <= n; i += a) // j represents possible values of b * y for (int j = 0; j <= n - i; j += b) { float z = (n - (i + j)) / c; // If z is an integer if (floor(z) == ceil(z)) { int x = i / a; int y = j / b; maxVal = max(maxVal, x + y + (int)z); } } return maxVal; } // Driver code int main() { int n = 10, a = 5, b = 3, c = 4; cout << maxResult(n, a, b, c); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) static int maxResult(int n, int a, int b, int c) { int maxVal = 0; // i represents possible values of a * x for (int i = 0; i <= n; i += a) // j represents possible values of b * y for (int j = 0; j <= n - i; j += b) { float z = (n - (i + j)) / c; // If z is an integer 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; } // Driver code public static void main(String args[]) { int n = 10, a = 5, b = 3, c = 4; System.out.println(maxResult(n, a, b, c)); } } // This code is contributed by // Surendra_Gangwar |
Python 3
# Python3 implementation of the approach from math import *; # Function to return the maximum value # of (x + y + z) such that (ax + by + cz = n) def maxResult(n, a, b, c) : maxVal = 0; # i represents possible values of a * x for i in range(0, n + 1, a) : # j represents possible values of b * y for j in range(0, n - i + 1, b) : z = (n - (i + j)) / c; # If z is an integer if (floor(z) == ceil(z)) : x = i // a; y = j // b; maxVal = max(maxVal, x + y + int(z)); return maxVal; # Driver code if __name__ == "__main__" : n = 10 a = 5 b = 3 c = 4 print(maxResult(n, a, b, c)); # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) static int maxResult(int n, int a, int b, int c) { int maxVal = 0; // i represents possible values of a * x for (int i = 0; i <= n; i += a) // j represents possible values of b * y for (int j = 0; j <= n - i; j += b) { float z = (n - (i + j)) / c; // If z is an integer 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; } // Driver code public static void Main(String []args) { int n = 10, a = 5, b = 3, c = 4; Console.WriteLine(maxResult(n, a, b, c)); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the approach // Function to return the maximum value of // (x + y + z) such that (ax + by + cz = n) function maxResult($n, $a, $b, $c) { $maxVal = 0; // i represents possible values of a * x for ($i = 0; $i <= $n; $i += $a) // j represents possible values of b * y for ($j = 0; $j <= $n - $i; $j += $b) { $z = ($n - ($i + $j)) / $c; // If z is an integer if (floor($z) == ceil($z)) { $x = (int)($i / $a); $y = (int)($j / $b); $maxVal = max($maxVal, $x + $y + (int)$z); } } return $maxVal; } // Driver code $n = 10; $a = 5; $b = 3; $c = 4; echo maxResult($n, $a, $b, $c); // This code is contributed by mits ?> |
3
Time Complexity: O(N2)
Recommended Posts:
- Maximize the value of the given expression
- Maximize the bitwise OR of an array
- Maximize the number of subarrays with XOR as zero
- Maximize a value for a semicircle of given radius
- Maximize the Expression | Bit Manipulation
- Burst Balloon to maximize coins
- Maximize the product of four factors of a Number
- Maximize the happiness of the groups on the Trip
- Maximize the number of sum pairs which are divisible by K
- Remove an element to maximize the GCD of the given array
- Maximize the number of segments of length p, q and r
- Maximize the total profit of all the persons
- Maximize volume of cuboid with given sum of sides
- Maximize the sum of products of the degrees between any two vertices of the tree
- Maximize profit when divisibility by two numbers have associated profits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



