This solution is implemented and provided by
=> ITI Dixit
=> Computer Science
=> Banasthali University (Jaipur)
# save this class inside a MaximumMinimumOfArray.java file #
// inside this code we are finding the maximum and minimum value inside an array, we are also finding second maximum and second minimum
import java.util.*;
public class MaximumMinimumOfArray
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//taking the sie of array as an input from the user
System.out.println("ENTER THE SIZE OF ARRAY : ");
int size=sc.nextInt();
//array declaration
int[] a=new int[size];
//taking elements of array as an input from the user
System.out.println("ENTER ARRAY ELEMENTS : ");
for (int i=0;i<size;i++)
a[i]=sc.nextInt();
//display of array elements
System.out.println("ARRAY ELEMENTS ARE : ");
for (int i=0;i<size;i++)
System.out.println(a[i]);
//variables to store max,second max,min and second min values
int max=a[0],min=a[0],sec_max=a[0],sec_min=a[0];
for(int i=0;i<size;i++)
{
//to find and store max and second max values
if(a[i]>max)
{
sec_max=max;
max=a[i];
}
else if(a[i]>sec_max)
sec_max=a[i];
//to find and store min and second min values
if(a[i]<min)
{
sec_min=min;
min=a[i];
}
else if(a[i]<sec_min)
sec_min=a[i];
}
//display of values
System.out.println("MAXIMUM VALUE = "+max);
System.out.println("MINIMUM VALUE = "+min);
System.out.println("SECOND MAXIMUM VALUE = "+sec_max);
System.out.println("SECOND MINIMUM VALUE = "+sec_min);
}
}
No comments:
Post a Comment