Maximum and Minimum value of a quadratic function

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:

  1. 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 minimum value of a square function is zero.
  2. Case 2: If value of a is negative.
    • The minimum value would be equal to -Infinity.
    • Since a is negative, the task to 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 the 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the Maximum and Minimum
// values of the quadratic function
void PrintMaxMinValue(double a, double b, double c)
{
  
    // Calculate the value of second part
    double secondPart = c * 1.0 - (b * b / (4.0 * a));
  
    // Print the values
    if (a > 0) {
  
        // Open upward parabola function
        cout << "Maxvalue = "
             << "Infinity\n";
        cout << "Minvalue = " << secondPart;
    }
    else if (a < 0) {
  
        // Open downward parabola function
        cout << "Maxvalue = " << secondPart << "\n";
        cout << "Minvalue = "
             << "-Infinity";
    }
    else {
  
        // If a=0 then it is not a quadratic function
        cout << "Not a quadratic function\n";
    }
}
  
// Driver code
int main()
{
    double a = -1, b = 3, c = -2;
  
    PrintMaxMinValue(a, b, c);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
  
class GFG 
{
  
    // Function to print the Maximum and Minimum
    // values of the quadratic function
    static void PrintMaxMinValue(double a, double b, double c) 
    {
  
        // Calculate the value of second part
        double secondPart = c * 1.0 - (b * b / (4.0 * a));
  
        // Print the values
        if (a > 0
        {
  
            // Open upward parabola function
            System.out.print("Maxvalue = "
                    + "Infinity\n");
            System.out.print("Minvalue = " + secondPart);
        
        else if (a < 0)
        {
  
            // Open downward parabola function
            System.out.print("Maxvalue = " + secondPart + "\n");
            System.out.print("Minvalue = "
                    + "-Infinity");
        
        else
        {
  
            // If a=0 then it is not a quadratic function
            System.out.print("Not a quadratic function\n");
        }
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        double a = -1, b = 3, c = -2;
  
        PrintMaxMinValue(a, b, c);
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach 
  
# Function to print the Maximum and Minimum 
# values of the quadratic function 
def PrintMaxMinValue(a, b, c) :
  
    # Calculate the value of second part 
    secondPart = c * 1.0 - (b * b / (4.0 * a));
      
    # Print the values
    if (a > 0) :
          
        # Open upward parabola function
        print("Maxvalue =", "Infinity");
        print("Minvalue = ", secondPart);
          
    elif (a < 0) :
          
        # Open downward parabola function
        print("Maxvalue = ", secondPart);
        print("Minvalue =", "-Infinity");
          
    else :
          
        # If a=0 then it is not a quadratic function
        print("Not a quadratic function"); 
  
# Driver code 
if __name__ == "__main__"
    a = -1; b = 3; c = -2
  
    PrintMaxMinValue(a, b, c); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG
{
      
    // Function to print the Maximum and Minimum
    // values of the quadratic function
    static void PrintMaxMinValue(double a, double b, double c) 
    {
  
        // Calculate the value of second part
        double secondPart = c * 1.0 - (b * b / (4.0 * a));
  
        // Print the values
        if (a > 0) 
        {
  
            // Open upward parabola function
            Console.Write("Maxvalue = "
                    + "Infinity\n");
            Console.Write("Minvalue = " + secondPart);
        
        else if (a < 0)
        {
  
            // Open downward parabola function
            Console.Write("Maxvalue = " + secondPart + "\n");
            Console.Write("Minvalue = "
                    + "-Infinity");
        
        else
        {
  
            // If a=0 then it is not a quadratic function
            Console.Write("Not a quadratic function\n");
        }
    }
  
    // Driver code
    static public void Main ()
    {
        double a = -1, b = 3, c = -2;
        PrintMaxMinValue(a, b, c);
    }
}
  
// This code is contributed by ajit.

chevron_right


Output:

Maxvalue = 0.25
Minvalue = -Infinity


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Rajput-Ji, jit_t, AnkitRai01



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.