Largest Element in an Array: flowchart, algorithm and program

This program reads n numbers into an array and prints the biggest one. It assumes the first element is the largest, then walks through the rest and updates the answer whenever it finds something bigger.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: Set i = 0.
  4. Step 4: If i < n, go to step 5; otherwise go to step 7.
  5. Step 5: Read arr[i].
  6. Step 6: Set i = i + 1. Go to step 4.
  7. Step 7: Set largest = arr[0], i = 1.
  8. Step 8: If i < n, go to step 9; otherwise go to step 12.
  9. Step 9: If arr[i] > largest, go to step 10; otherwise go to step 11.
  10. Step 10: Set largest = arr[i].
  11. Step 11: Set i = i + 1. Go to step 8.
  12. Step 12: Print largest.
  13. Step 13: Stop.

Pseudocode

BEGIN
    READ n
    i ← 0
    WHILE i < n DO
        READ arr[i]
        i ← i + 1
    END WHILE
    largest ← arr[0], i ← 1
    WHILE i < n DO
        IF arr[i] > largest THEN
            largest ← arr[i]
        END IF
        i ← i + 1
    END WHILE
    PRINT largest
END

Program in C, Python and Java

Largest Element in an Array program in C

#include <stdio.h>

int main() {
    int n, i, largest;
    int arr[100];

    printf("Enter n: ");
    scanf("%d", &n);

    printf("Enter %d numbers: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    largest = arr[0];
    for (i = 1; i < n; i++) {
        if (arr[i] > largest) {
            largest = arr[i];
        }
    }

    printf("Largest element = %d\n", largest);
    return 0;
}

Largest Element in an Array program in Python

n = int(input("Enter n: "))
arr = list(map(int, input("Enter " + str(n) + " numbers: ").split()))

largest = arr[0]
for i in range(1, n):
    if arr[i] > largest:
        largest = arr[i]

print("Largest element =", largest)

Largest Element in an Array program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        System.out.print("Enter " + n + " numbers: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int largest = arr[0];
        for (int i = 1; i < n; i++) {
            if (arr[i] > largest) {
                largest = arr[i];
            }
        }

        System.out.println("Largest element = " + largest);
    }
}

Sample input: n = 5, numbers = 12 45 7 89 23
Output: Largest element = 89

How it works

  • The first loop runs i from 0 to n-1 and stores each number you type into arr[i].
  • Set largest = arr[0], so we start with a real value from the array as our best guess.
  • The second loop starts at i = 1, because arr[0] is already in largest and needs no comparison.
  • Inside the loop, if arr[i] > largest, then largest = arr[i]. Otherwise largest stays the same.
  • After the loop ends, largest holds the maximum, and we print it. For 12 45 7 89 23 it goes 12 -> 45 -> 45 -> 89 -> 89, so the output is 89.

Dry run (trace table)

StepLineBoxniarrlargestOutput
13Start
28Read n5Enter n: Enter 5 numbers:
311i = 050
411i < n?50
512Read arr[i]50[12]
611i = i + 151[12]
711i < n?51[12]
812Read arr[i]51[12,45]
911i = i + 152[12,45]
1011i < n?52[12,45]
1112Read arr[i]52[12,45,7]
1211i = i + 153[12,45,7]
1311i < n?53[12,45,7]
1412Read arr[i]53[12,45,7,89]
1511i = i + 154[12,45,7,89]
1611i < n?54[12,45,7,89]
1712Read arr[i]54[12,45,7,89,23]
1811i = i + 155[12,45,7,89,23]
1911i < n?55[12,45,7,89,23]
2015largest = arr[0], i = 151[12,45,7,89,23]12

Time and space complexity

Time: O(n) · Space: O(n). Each loop runs n times doing constant work, and the array stores n elements.

Common mistakes

  • Initialising largest = 0. This breaks when every number is negative, because 0 would wrongly win. Start from arr[0] instead.
  • Starting the second loop at i = 0 or running it up to i <= n. The first is harmless but wasteful, and the second reads past the last element.
  • Using n = 0 or n > 100. With n = 0, arr[0] is uninitialised, and with n > 100 the array overflows. Check n before reading.

Viva questions

Q1. Why do you initialise largest = arr[0] instead of 0?

If all numbers are negative, starting at 0 would wrongly report 0 as the largest. Using the first element guarantees the starting value is a real element of the array.

Q2. Why does the second loop start from i = 1 and not i = 0?

arr[0] is already stored in largest, so comparing it with itself is pointless. We only need to compare the remaining elements.

Q3. What does the condition arr[i] > largest check, and what happens if two elements are equal to the maximum?

It checks whether the current element is bigger than the maximum seen so far, and if so updates largest. With equal values nothing changes, since the value is the same anyway.

Q4. What happens if the user enters n = 1?

The first loop reads one number, largest = arr[0], and the second loop does not run because i = 1 is not less than 1. That single element is printed as the largest.

Q5. What happens if n is 0 or greater than 100?

For n = 0 no numbers are read and arr[0] is uninitialised garbage, so the output is meaningless. For n > 100 the loop writes past the end of arr, which is undefined behaviour; the program should validate n between 1 and 100.

Q6. What is the time and space complexity of this program?

Time is O(n) because each element is read once and compared once in a single scan. Space is O(1) extra beyond the array, since only a few variables are used.

Q7. Do we really need the array? How could the program be changed to avoid storing all the numbers?

No. We could read the first number into largest, then read each next number and compare it immediately, updating largest if bigger. This needs no array and uses constant memory.

Q8. What would you change to find the smallest element, or the position of the largest element?

For the smallest, change the comparison to arr[i] < smallest. For the position, also keep an index variable, set it to i whenever you update largest, and print it.

Frequently asked questions

Why do we set largest = arr[0] instead of 0?

Because the largest value must be one of the array's own elements. If all numbers are negative, starting at 0 would give a wrong answer, but arr[0] always works.

What is the time complexity of finding the largest element?

O(n), since each element is looked at exactly once. It uses O(1) extra space because only the variable largest is kept besides the array.

How can I also find the position of the largest element?

Keep an extra variable, such as index = 0, and set index = i whenever you update largest. At the end, arr[index] is the maximum and index tells you where it is.

More programs

← All programs

Try AI Flowchart free →