Given a quadratic function ax2 + bx + c. Find the maximum and minimum value of the function possible when x is varied for all real values possible.
Examples:
Input: a = 1, b = -4, c = 4
Output:
Maxvalue = Infinity
Minvalue = 0
Quadratic function given is x2 -4x + 4
At x = 2, value of the function is equal to zero.
Input: a = -1, b = 3, c = -2
Output:
Maxvalue = 0.25
Minvalue = -Infinity
Approach:
Q(x)=ax2 + bx + c.
=a(x + b/(2a))2 + c-b2/(4a).
first part second part
The function is broken into two parts.
The first part is a perfect square function. There can be two cases:
- Case 1: If value of a is positive.
- The maximum value would be equal to Infinity.
- The minimum value of the function will come when the first part is equal to zero because the minimum value of a square function is zero.
- Case 2: If value of a is negative.
- The minimum value would be equal to -Infinity.
- Since a is negative, the task to maximize the negative square function. Again maximum value of a negative square function would be equal to zero as it would be a negative value for any other value of x.
The second part is a constant value for a given quadratic function and hence cannot change for any value of x. Hence, it will be added in both cases. Hence, the answer to the problem is:
If a > 0,
Maxvalue = Infinity
Minvalue = c - b2 / (4a)
If a < 0,
Maxvalue = c - b2 / (4a)
Minvalue = -Infinity
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void PrintMaxMinValue( double a, double b, double c)
{
double secondPart = c * 1.0 - (b * b / (4.0 * a));
if (a > 0) {
cout << "Maxvalue = "
<< "Infinity\n" ;
cout << "Minvalue = " << secondPart;
}
else if (a < 0) {
cout << "Maxvalue = " << secondPart << "\n" ;
cout << "Minvalue = "
<< "-Infinity" ;
}
else {
cout << "Not a quadratic function\n" ;
}
}
int main()
{
double a = -1, b = 3, c = -2;
PrintMaxMinValue(a, b, c);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void PrintMaxMinValue( double a, double b, double c)
{
double secondPart = c * 1.0 - (b * b / ( 4.0 * a));
if (a > 0 )
{
System.out.print( "Maxvalue = "
+ "Infinity\n" );
System.out.print( "Minvalue = " + secondPart);
}
else if (a < 0 )
{
System.out.print( "Maxvalue = " + secondPart + "\n" );
System.out.print( "Minvalue = "
+ "-Infinity" );
}
else
{
System.out.print( "Not a quadratic function\n" );
}
}
public static void main(String[] args)
{
double a = - 1 , b = 3 , c = - 2 ;
PrintMaxMinValue(a, b, c);
}
}
|
Python3
def PrintMaxMinValue(a, b, c) :
secondPart = c * 1.0 - (b * b / ( 4.0 * a));
if (a > 0 ) :
print ( "Maxvalue =" , "Infinity" );
print ( "Minvalue = " , secondPart);
elif (a < 0 ) :
print ( "Maxvalue = " , secondPart);
print ( "Minvalue =" , "-Infinity" );
else :
print ( "Not a quadratic function" );
if __name__ = = "__main__" :
a = - 1 ; b = 3 ; c = - 2 ;
PrintMaxMinValue(a, b, c);
|
C#
using System;
class GFG
{
static void PrintMaxMinValue( double a, double b, double c)
{
double secondPart = c * 1.0 - (b * b / (4.0 * a));
if (a > 0)
{
Console.Write( "Maxvalue = "
+ "Infinity\n" );
Console.Write( "Minvalue = " + secondPart);
}
else if (a < 0)
{
Console.Write( "Maxvalue = " + secondPart + "\n" );
Console.Write( "Minvalue = "
+ "-Infinity" );
}
else
{
Console.Write( "Not a quadratic function\n" );
}
}
static public void Main ()
{
double a = -1, b = 3, c = -2;
PrintMaxMinValue(a, b, c);
}
}
|
Javascript
<script>
function PrintMaxMinValue(a, b, c)
{
var secondPart = c * 1.0 -
(b * b / (4.0 * a));
if (a > 0)
{
document.write( "Maxvalue = " +
"Infinity" + "<br>" );
document.write( "Minvalue = " +
secondPart);
}
else if (a < 0)
{
document.write( "Maxvalue = " +
secondPart + "<br>" );
document.write( "Minvalue = " +
"-Infinity" );
}
else
{
document.write( "Not a quadratic function\n" );
}
}
var a = -1, b = 3, c = -2;
PrintMaxMinValue(a, b, c);
</script>
|
Output: Maxvalue = 0.25
Minvalue = -Infinity
Time Complexity: O(1)
Auxiliary Space: O(1)