Algorithms in C#  

πŸ” How to Find the Largest Element in an Array?

Arrays are one of the most basic data structures in programming. A common beginner-level question in Data Structures and Algorithms (DSA) is finding the largest element in an array. This problem helps you understand array traversal, comparisons, and how to work with loops effectively.

πŸ“Œ Problem Statement

Given an array of integers, find the largest (maximum) element from the array.

Example

Input: {10, 25, 7, 89, 56}
Output: 89

🧠 Approach / Logic

  1. Assume the first element is the largest.

  2. Traverse through the array using a loop.

  3. Compare each element with the current largest.

  4. If you find a bigger number, update the largest.

  5. Print the final largest value after traversal.

This works in O(n) time complexity since we scan the array once.

πŸ’» Code Examples

βœ… C Program

#include <stdio.h>

int main() {
    int n, i, max;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    int arr[n];
    
    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    max = arr[0];
    for (i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    
    printf("Largest element = %d\n", max);
    return 0;
}

βœ… C++ Program

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of elements: ";
    cin >> n;
    int arr[n];
    
    cout << "Enter " << n << " elements:\n";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    
    cout << "Largest element = " << max << endl;
    return 0;
}

βœ… Java Program

import java.util.Scanner;

public class LargestInArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter number of elements: ");
        int n = sc.nextInt();
        int arr[] = new int[n];
        
        System.out.println("Enter " + n + " elements:");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        
        int max = arr[0];
        for (int i = 1; i < n; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        
        System.out.println("Largest element = " + max);
        sc.close();
    }
}

πŸ“Š Time & Space Complexity

  • Time Complexity: O(n) β†’ We scan each element once.

  • Space Complexity: O(1) β†’ Only a variable is used to store the largest element.

πŸš€ Applications in Real Life

  • Leaderboard Ranking: Finding the top scorer in a game.

  • Finance: Finding the highest stock price in a given dataset.

  • Data Analytics: Identifying maximum sales in a month.

🎯 Key Takeaways

  • Always initialize the largest element with the first element of the array.

  • Don’t forget to check all elements with a loop.

  • This is a fundamental question that builds a foundation for sorting, searching, and optimization problems in DSA.