Find Maxima/Minima
Coding Problem Keys
Find Maxima/Minima
Problem Statement
Implement the following function:
def MaximaOrMinima(a, b, c):
Quadratic equation: A quadratic equation is any equation having the form, ax² + bx + c, where 'a' cannot be zero.
The function accepts coefficients of a quadratic equation, 'a', 'b' and 'c' as its argument.
Implement the function to find the maximum or minimum value of quadratic equation by substituting integer values x, where -100 <= x <= 100. Return the values as follow:
- If a > 0, return the minimum value of the equation.
- If a < 0, return the maximum value of the equation.
Example 1
Input
a: 1Output
Explanation
The custom input format for the above case:
1
2
1
(The first line represents 'a', the second line represents 'b', the third line represents 'c')
Example 2
Input
a: -2Output
The custom input format for the above case:
-2
-8
10
(The first line represents 'a', the second line represents 'b', the third line represents 'c')
Instructions
- This is a template based question, DO NOT write the "main" function.
- Your code is judged by an automated system, do not write any additional welcome/greeting messages.
- "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring.
- Additional score will be given for writing optimized code both in terms of memory and execution time.
Test Case #1
Input
Output
Test Case #2
Input
Output
Solution
Programming Language: Python 3 Language
# Published By PKJCODERS> Read-only code below # Input read from STDIN result = MaximaOrMinima(a, b, c) # Value in result is printed in STDOUT > Write your code below def MaximaOrMinima(a, b, c): # Write your code here maxi=0;mini=0 for i in range(-100,101): res=(a*(i*i))+(b*i)+c print(res) if(res<mini): mini=res if(res>maxi): maxi=res if(a>0): return mini elif(a<0): return maxi
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments