Beginner Array Problems C

Largest Element in an Array in C

Find the maximum value in an integer array in C using one pass through the array.

  • TimeO(n)
  • SpaceO(1)
  • Hints3
  • Read3 min
Quick answer

To find the largest element in an array in C, set max = arr[0], then loop i from 1 to n - 1. If arr[i] > max, set max = arr[i]. After one pass, max holds the largest value. For {34, 78, 12, 95, 47} the answer is 95. It takes O(n) time and O(1) extra space, and it works for negative numbers because max starts from a real element, not from 0.

Arraysfor loopComparison and updateSingle-pass algorithm
1

Problem statement

Write a C program to find the largest element in an array. Go through the array only once.

For the array {34, 78, 12, 95, 47} the output is Largest element = 95.

Input

An integer n, then n integers.

Output

Largest element = <max>

Examples

Example 1
Inputarr = {34, 78, 12, 95, 47}
OutputLargest element = 95

95 is greater than every other element.

Example 2
Inputarr = {-8, -3, -15, -1}
OutputLargest element = -1

-1 is the closest to zero, so it is the largest. Starting max at 0 would give the wrong answer here.

Constraints

  • 1 <= n <= 100
  • Elements can be negative and can repeat
2

Think before you code

Reveal the questions to ask yourself first
  • What should max be before the loop starts?
  • Why is starting with max = 0 a problem?
  • Do you need to compare arr[0] with itself?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Start with max = arr[0]. It is a real element, so it works for negative arrays too.
2 Hint 2
Loop from i = 1, because arr[0] is already in max.
3 Hint 3
Use if (arr[i] > max) max = arr[i]; inside the loop.
4

Approach

Reveal the step-by-step approach

Single-pass method

  1. Assume the first element is the largest: max = arr[0].
  2. For each index i from 1 to n - 1, compare arr[i] with max.
  3. If arr[i] is bigger, it becomes the new max.
  4. After the loop, print max.

Each element is looked at once, so the work grows in a straight line with n. Sorting the array first would also give the answer, but sorting takes O(n log n) time, which is more work than needed.

Why not start with max = 0?

If every element is negative, no element is greater than 0, so max stays 0, and 0 is not even in the array. Starting from arr[0] avoids this.

5

Dry run

Walk through the example step by step

arr = {34, 78, 12, 95, 47}, max = arr[0] = 34:

i | arr[i] | arr[i] > max? | max after
--+--------+---------------+----------
1 | 78     | 78 > 34 yes   | 78
2 | 12     | 12 > 78 no    | 78
3 | 95     | 95 > 78 yes   | 95
4 | 47     | 47 > 95 no    | 95

The loop ends with max = 95.

6

Solution

Reveal the full C solution
solution.c
#include <stdio.h>

int main(void) {
    int arr[] = {34, 78, 12, 95, 47};
    int n = sizeof(arr) / sizeof(arr[0]);
    int max = arr[0];

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

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

Output:

Largest element = 95

Four comparisons are made for five elements. In general, an array of n elements needs n - 1 comparisons.

Find max in array with user input and its position

#include <stdio.h>

int main(void) {
    int n;
    printf("Enter number of elements: ");
    scanf("%d", &n);

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

    int max = arr[0], pos = 0;
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
            pos = i;
        }
    }
    printf("Largest = %d at position %d\n", max, pos + 1);
    return 0;
}
Enter number of elements: 6
Enter 6 numbers: 14 3 88 27 88 5
Largest = 88 at position 3

88 appears twice. Because the check is > and not >=, the second 88 does not replace the first, so the first position is reported. Change it to >= if you want the last position instead. The position is pos + 1 because users count from 1.

Largest and smallest element in array in C, and a function version

#include <stdio.h>

int find_max(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

int main(void) {
    int temps[] = {-8, -3, -15, -1};
    int a[] = {5, 9, 9, 2};
    int arr[] = {34, 78, 12, 95, 47};
    int min = arr[0], max = arr[0];

    for (int i = 1; i < 5; i++) {
        if (arr[i] > max) max = arr[i];
        if (arr[i] < min) min = arr[i];
    }

    printf("Max of all-negative array = %d\n", find_max(temps, 4));
    printf("Max with duplicates = %d\n", find_max(a, 4));
    printf("Smallest = %d, Largest = %d\n", min, max);
    return 0;
}
Max of all-negative array = -1
Max with duplicates = 9
Smallest = 12, Largest = 95

The smallest and largest can be found in the same loop with two if checks. The function needs n passed in, because inside a function the array is a pointer and sizeof cannot tell its length.

The Java version is on find the largest element in an array. The next step is find the second largest element, which is a common interview question.

TimeO(n)
SpaceO(1)
7

Review before the interview

Common mistakes

  • Starting max at 0, which gives 0 for an all-negative array.
  • Starting the loop at i = 0. It still works, but it makes one useless comparison.
  • Using i <= n, which reads past the end of the array.
  • Sorting the whole array just to read the last element. It works but costs O(n log n).
  • Printing max inside the loop instead of once after it.

Edge cases to test

  • n = 1: the only element is the largest, and the loop body never runs.
  • All elements equal: max is that value.
  • Duplicates of the largest value: max is still correct; > keeps the first position.
  • All negative numbers: works because max starts from arr[0].

Interview follow-ups

  • How would you find the second largest element in one pass?
  • How would you find the largest element using recursion?
  • How would you find the largest element in each row of a 2D array?

Keep practising

Building your programming basics?

CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.

Chat with us