Write a Java program to find Largest Element in an array.

Write a program to read an integer array of odd length, compare the first, middle, and the last elements in the array and return the largest. If there is only one element in the array return the same element.

Note: Assume the maximum length of the array is 20.

Input and Output Format

  • Input consists of n+1 integers. The first integer input corresponds to n, the number of elements in the array. The next ‘n’ integers correspond to the elements in the array.
  • The output consists of a single Integer.

Refer sample output for formatting specifications

Sample Input 1:

5

2

3

8

4

5

Sample Output 1:

8

Program to Find Largest Element in the array in Java

Following are the steps to search the largest element in an array:

  •  Input number of elements (n) from the user.
  • Add elements to an array of size n.
  • Pass the array to the display() method.
  • Inside the method, declare and initialize the max value to 0. Also, declare the variables x,y, and z.
  • Initialize the first element of array to x, the last element to z, and second the last element to y.
  • Now, check for the below-mentioned condition:
    • If the value in x is greater than y and z, then initialize the max value to x.
    • Otherwise, compare the value of y with x and z, then initialize the max value to y.
    • And, compare the value of z with x and y, then initialize the max value to z.
  • At last, return the value in a max variable.
import java.util.*;
public class Main {
  static int n =0;
public static void main(String[] args)  {
  Scanner sc = new Scanner(System.in);
  n = sc.nextInt();
  int a[] = new int[20];
  for(int i=0;i<n;i++){
    a[i] = sc.nextInt();
  }
int n1 = display(a);
System.out.println(n1);
}
public static int display(int []a)

{
                int max=0;
                int x,y,z;
                x=a[0];
               
                y=a[n/2];
                z=a[n-1];
               
                if(x>y&&x>z)
                                max=x;
                else if(y>x&&y>z)
                                max=y;
                else if(z>x&&z>y)
                                max=z;
                return max;
}

}

Output

Largest Element without using Array

Following are the steps to find the largest element without using Array:

  • Input size of the element from the user.
  • Pass the size to the display() method.
  • Inside the method, declare and initialize the largest and num variable to 0.
  • Iterate through using while loop till size(n) is greater than 0 and decrement value of n for each iteration.
  • Inside the loop, for each iteration user will input a number and this number is checked if it is greater than the value in the largest variable then transfer the value to the largest variable.
  • At last, print the value of largest variable.
package com.demo3;

import java.util.Scanner;

public class LargestWithoutArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Size");
        int n = scanner.nextInt();
        display(n);
    }

    private static void display(int n) {
        int largest = 0,num;
        Scanner scanner = new Scanner(System.in);
        while(n > 0){
            num = scanner.nextInt();
            if(num  > largest){
                largest = num;
            }
            n--;
        }
        System.out.println("Largest Element is "+largest);
    }
}

Output

Thus, in this way, we learn how to find the largest element in an array.