Showing posts with label Maximum and Minimum in an Array. Show all posts
Showing posts with label Maximum and Minimum in an Array. Show all posts

Wednesday, June 29, 2022

Maximum and Minimum in an Array

 Maximum and Minimum in an Array

Problem Statement
Given an array A[ ] of size N containing positive integers, find maximum and minimum elements from the array.

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int testcase = sc.nextInt();
        while (testcase>0){
            int n =sc.nextInt();
            int arr[]=new int[n];
        for (int i=0;i<n;i++){
             arr[i]=sc.nextInt();
        }
        int mini = arr[0];
        int maxi = arr[0];
        for(int i=1;i<n;i++){
            if(mini>arr[i]){
                mini = arr[i];
            }
            if (maxi<arr[i]){
                maxi=arr[i];
           }
        }
           System.out.println(maxi+" "+mini);
     testcase--;
    
}
    }
}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...