Skip to study material
← Question index

QUESTION 35 · Worked algorithms

Why initialise minimum and maximum from the first value?

2 marks

Answer

Start both minimum and maximum with the first input value of the non-empty list.

  • Compare each remaining value with both current results and update them when needed.
  • Starting with zero can give a wrong maximum for all-negative inputs or a wrong minimum for all-positive inputs.

QUESTION 36 · Worked algorithms

Develop an algorithm to find the minimum and maximum in a set.

5 marks

Answer in brief

The minimum and maximum of a non-empty set can be found by reading each value and updating the current result.

Algorithm

  1. Read n: The count must be at least 1.
  2. Read the first value: Set maximum to this value. Also set minimum to this value.
  3. Read each remaining value: If it is greater than maximum, replace maximum. If it is smaller than minimum, replace minimum.
  4. Display the result: After all values have been checked, output the minimum and maximum.

Explanation

The starting value must come from the input. Suppose all values are negative. Starting maximum at zero would give a value that is not even in the input. Starting from the first actual value avoids that mistake. After each new value is checked, the stored result is correct for all values read so far.

Example

For the inputs −8, 3, 3 and −2, maximum starts at −8. Reading 3 changes it to 3. The next 3 and −2 do not change it. Minimum starts at −8 and stays −8. Thus the final minimum is −8 and the maximum is 3.

Boundary cases

With only one input, that value is the minimum and maximum. Equal values do not cause a problem. An empty input is rejected because there is no first value to use. The program accepts 1–100 integers. Each input must fit int. It reads values one at a time, so it does not need an array.

Time and space complexity

Each of n values is read once, with a fixed number of comparisons for each new value. Time is O(n). The program stores only a few counters and result variables, so extra space is O(1). In the flowchart, max and min mean choosing the larger or smaller value; they are mathematical operations, not C library calls used by this program.

Reference table

↔ Scroll horizontally on a small screen
Boundary checks
CaseExpected behaviour
One valueIt is the minimum and maximum.
All values negativeInitialisation from input still works.
Repeated equal valuesThe repeated value is retained.
Count zeroReject because the set must be non-empty.

Diagram or flowchart

Read the first value, initialise the candidate values, scan each remaining input and output the result.
Scan a set for its minimum and maximum · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    int n, value;
    if (scanf("%d", &n) != 1 || n < 1 || n > 100) {
        puts("Unsupported count"); return 1;
    }
    if (scanf("%d", &value) != 1) { puts("Invalid value"); return 1; }
    int maximum = value;
    int minimum = value;
    for (int i = 1; i < n; ++i) {
        if (scanf("%d", &value) != 1) { puts("Invalid value"); return 1; }
        if (value > maximum) { maximum = value; }
        if (value < minimum) { minimum = value; }
    }
    printf("Minimum = %d, maximum = %d\n", minimum, maximum);
    return 0;
}

Sample input and output

Input

4
-8 3 3 -2

Output

Minimum = -8, maximum = 3

Key points

  • Initialise the minimum and maximum from the first actual input, then update each candidate as the remaining values are read.
  • Do not initialise a candidate to zero unless the input domain justifies it.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • September 2023 · Q11(a) · 6 marks

    Write an algorithm to find minimum and maximum values in a given set.

    Paper P09 · page 2
View all matching exam references →