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.
Assumption: a is not equal to zero.
Note: Computed value lies within integer range.

 Example 1 

 Input 

a: 1
b: 2
c: 1

 Output 

0

 Explanation 

Since, (a > 0) output is the minimum value which is at x = -1, output = 1 * (-1)² + 2 * (-1) + 1 = 0.

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: -2
b: -8
c: 10

 Output 

18

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 

a: 1
b: -4
c: 3

 Output 

-1

 Test Case #2 

 Input 

a: -1
b: 2
c: -1

 Output 

0

 Solution 

 Programming Language: Python 3 Language 

> 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

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Toggle Consonants Adjacent to Vowels

String Pattern - Inverted U

Top Scoring Batsman - TEST ODI 20-20

Desktop Products

Mango Distribution

Java - Method Overriding - Area

Microsoft