Java Program of Sum of the maximum and the minimum element

Question:

Anjali gets n numbers in an array. Write a Java program to print the sum of the maximum and the minimum element in the array. If the size of an array is 0 or less print “Invalid Array Size”.

Sample Input 1:

Enter the size of an array:

5

Enter the elements:

45

23

48

90

89

Sample Output 1:

113

Sample Input 2:

Enter the size of an array:

0
Sample Output 2:

Invalid Array Size

Sum.java

```import java.util.*;
public class Sum
{
int max(int[] arr)
{
int max=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
return max;
}

int min(int[] arr)
{
int min=arr[0];
for(int i=0;i<arr.length;i++)
{
if(arr[i]<min)
{
min=arr[i];
}
}
return min;
}

public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
Sum obj=new Sum();

System.out.println("Enter the size of an array:");
int n=sc.nextInt();

if(n>0)
{
int[] arr=new int[n];

System.out.println("Enter the elements:");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}

System.out.println(obj.max(arr)+obj.min(arr));
}
else
{
System.out.println("Invalid Array Size");
}

}
}```