Max Element in java

Max Element in java

Given an array of integers, you need to find its maximum value element.

Input

The input contains N spaced integers. There is no size of the array given beforehand. You will have to use some other way of reading input values.

Constraints

  • 1 < = N < = 1000
  • 1 < = Arr[i] < = 10000

Output

Print the largest element present in the array.

Example

Sample Input

8 1 2 3 4 

Sample Output

8 

Sample Input

8 2 4 6 8

Sample Output

8

Solution of Max Element in java:–

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		//your code 
      Scanner sc = new Scanner(System.in);
      String str=sc.nextLine();
      String brr[] =str.split(" ");
      
      int arr[] = new int[brr.length];

      for(int i=0;i<brr.length;i++)
        {
          arr[i]=Integer.parseInt(brr[i]);
        }
      
      int max=arr[0];
      for(int i=0;i<brr.length;i++)
        {
          if(max<arr[i])
          {
            max=arr[i];
          }
        }
      System.out.println(max);
	}
}

Add a Comment

Your email address will not be published. Required fields are marked *