Advertisement
Shekhar777

Java Loops and Introduction to Arrays - Maximum and Minimum in an Array

Oct 19th, 2020
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. Q14-Given an array A[ ] of size N containing positive integers, find maximum and minimum elements from the array.
  2. Ans-import java.io.*;
  3. import java.util.*;
  4. class Main {
  5.     public static void main (String[] args) {
  6.                      
  7.                       Scanner sc =new Scanner(System.in);
  8.                       int T=sc.nextInt();
  9.                       for(int j=0;j<T;j++){
  10.  
  11.                          
  12.                          int N=sc.nextInt();
  13.                          int array[]=new int[N];
  14.                          int max=0;
  15.                          for(int i=0;i<N;i++){
  16.                             array[i]=sc.nextInt();
  17.                          }
  18.                          for(int i=0;i<N;i++){
  19.  
  20.                            if(array[i]>max){
  21.  
  22.                               max=array[i];
  23.  
  24.                            }
  25.  
  26.                          }
  27.                          System.out.print(max);
  28.                          int min=array[0];
  29.                          for(int i=0;i<N;i++){
  30.  
  31.                             if(array[i]<min){
  32.  
  33.                                min=array[i];
  34.                            }
  35.                          }
  36.                          
  37.                          System.out.print(" "+min);
  38.                          System.out.println();
  39.                      
  40.  
  41.                       }
  42.                      
  43.                      
  44.                      
  45.                      
  46.      
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement