---
title: "Second largest element using pointer"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-second-largest"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-second-largest
site: BCT Engineering
---

# Second largest element using pointer

Computer Programming · Chapter 9: Pointers

- Asked in: 2074 Asoj, 4 marks
- Video: [Find out max and second max from an array in C language | C programming video tutorials series](https://www.youtube.com/watch?v=_3S1Hvb-ZGg) — Sanjay Gupta | Cloud & AI
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-second-largest
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

Write a program in C to find second largest elements from an array containing N elements using concept of pointer.

## Short answer

Read array via pointer. Track first largest and second (found flag). In one pass, if element > first, shift first to second; else if element < first and (not found or > second), update second. Print second largest if found, else indicate all equal. Example output: Largest = 89, second largest = 45.

## Answer

**Logic.** The array is read through a pointer `p` (`p + i` gives the address of element i, and `*(p + i)` its value). `first` starts as the first element, and the flag `found` shows whether a second, different value has been seen yet. For each remaining element `v`:
- if `v` is greater than `first`, the old `first` becomes `second` and `v` becomes `first`;
- otherwise, if `v` is less than `first` and (no `second` exists yet, or `v` is greater than `second`), `v` becomes `second`;
- a value equal to `first` is ignored, so a repeated largest value is not counted as the second largest.

This takes only one pass over the array. If all the elements are equal, there is no second largest.

```c
#include <stdio.h>

int main(void)
{
    int a[100], n, *p = a;
    printf("Enter N: ");
    if (scanf("%d", &n) != 1 || n < 2 || n > 100) {
        printf("N must be between 2 and 100\n");
        return 1;
    }
    printf("Enter %d elements: ", n);
    for (int i = 0; i < n; i++)
        scanf("%d", p + i);

    int first = *p, second = 0, found = 0;   /* found = 1 once a second distinct value exists */
    for (int i = 1; i < n; i++) {
        int v = *(p + i);
        if (v > first) {
            second = first;
            first = v;
            found = 1;
        } else if (v < first && (!found || v > second)) {
            second = v;
            found = 1;
        }
    }
    if (found)
        printf("Largest = %d, second largest = %d\n", first, second);
    else
        printf("All elements are equal: no second largest\n");
    return 0;
}
```

**Sample run** (12 45 7 89 45 23)

Input: `6 12 45 7 89 45 23`

```text
Enter N: Enter 6 elements: Largest = 89, second largest = 45
```

With the input `3 89 89 45` the output is `Largest = 89, second largest = 45`, because the repeated 89 is not counted twice.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When an element is greater than first, what happens?
   A. second takes the old first and first takes the new value
   B. Only first is changed
   C. second takes the new value
   D. The element is ignored
   Answer: A. The old largest becomes the second largest and the new element becomes the largest. (Key point: Update rule for new maximum)

2. Why does the else-if check *(p+i) != first?
   A. To avoid division by zero
   B. So a duplicate of the largest is not counted as second largest
   C. To make the loop faster
   D. To skip negative numbers
   Answer: B. Without it the largest value repeated would be reported as second largest. (Key point: Handling duplicates)

3. What is the second largest for 12 45 7 89 45 23?
   A. 89
   B. 45
   C. 23
   D. 12
   Answer: B. The largest is 89; the duplicate 45 counts once, so second largest is 45. (Key point: Result for 12 45 7 89 45 23)

4. How is the value of element i read through pointer p?
   A. p + i
   B. &(p + i)
   C. *(p + i)
   D. *p + i
   Answer: C. p + i is the address of element i and * gives its value. (Key point: Pointer access to element i)

## Related in Computer Programming

- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9
- [Pass array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-largest-smallest) — Computer Programming, chapter 7
- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7
- [Function returning largest of array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Alargest-from-array-function) — Computer Programming, chapter 7
- [Relation between array and pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-pointer-relation) — Computer Programming, chapter 9, asked 5×

## Related in other subjects

- [Stored program concept and processing cycle](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Astored-program-processing-cycle) — Microprocessor, chapter 1, asked 6×
- [Two-element array and end fire pattern](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Atwo-isotropic-endfire-pattern) — Propagation and Antenna, chapter 2
- [Simple program illustrating OOP concept](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-simple-program) — Object Oriented Programming, chapter 1
- [Two-element array: broadside and end fire](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Atwo-element-array-broadside-endfire) — Propagation and Antenna, chapter 2
