Maximum and Minimum Of Array Elements | Practice | GeeksforGeeks

Given an array A[ ], find maximum and minimum elements from the array.
Input:
The first line of input contains an integer T, denoting the number of testcases. The description of T testcases follows. The first line of each testcase contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
For each testcase in a new line, print the maximum and minimum element in a single line with space in between.
Constraints:
1 ≤ T ≤ 30
1 ≤ N ≤ 100
0 ≤A[i]<100
Example:
Input:
2
4
5 4 2 1
1
8
Output:
5 1
8 8
Explanation:
Testcase 1:
Maximum element is: 5 
Minimum element is: 1
0
codingbeast00122 weeks ago
#include<iostream>#include<limits.h>using namespace std;
int max_element(int a[], int size){   int maxi=INT_MIN;      for(int i=0;i<size;i++)   {       maxi=max(maxi,a[i]);   }      return maxi;   }
int min_element(int b[], int size){   int mini=INT_MAX;   for(int i=0;i<size;i++)   {       mini=min(mini,b[i]);   }     return mini;}
 
int main(){//codeint T;cin>>T;while(T--){    int N;    cin>>N;    int A[N];    for(int i=0;i<N;i++)    {        cin>>A[i];    }    cout<<max_element(A,N)<<" "<<min_element(A,N)<<endl; }   
   return 0;}
0
krishnakshirsagar413 months ago
#try this difficult one so much to learn in this code not only min max #function
n = int(input())
for i in range(n):   l = int(input())   lst1 = list(map(int, input().split()))   lst1.sort(reverse=True)   ans1=[]   ans1.append(lst1[0])   lst1.sort()   ans1.append(lst1[0])   print(*ans1)
-1
abhinavsinghbiz3 months ago
C Code,Run Time=0.0
#include <stdio.h>
int max(int arr[],int N){   int max=arr[0];   for(int i=1;i<N;i++){       if(arr[i]>max){           max=arr[i];       }   }   return max;}
int min(int arr[],int N){   int min=arr[0];   for(int i=1;i<N;i++){       if(arr[i]<min){           min=arr[i];       }   }   return min;}
int main() {   int T;   scanf("%d",&T);for(int i=0;i<T;i++){    int N;    scanf("%d",&N);    int arr[N];    for(int j=0;j<N;j++){        scanf("%d",&arr[j]);    }    printf("%d ",max(arr,N));    printf("%d\n",min(arr,N));    }return 0;}
0
mohanlovish70803 months ago
#please help me out... i don't know that why my code fails every time i run...
t = int(input())
for i in range(t):
    n = int(input())
    arr = input()
    arr_l = arr.split(" ")
    
    mx = max(arr_l)
    mn = min(arr_l)
    
    print(mx, end = " ")
    print(mn)
0
rutwikgosavi3 months ago
t = int(input())
for i in range(t):
    n = int(input())
    lst = list(map(int, input().split()))
    print(max(lst), min(lst))
0
rutwikgosavi
This comment was deleted.
0
rryadav142001
This comment was deleted.
0
bickyjha0553 months ago
#include "bits/stdc++.h"using namespace std;
int min(int arr[], int n){       int min_value = INT_MAX;    for(int i = 0; i<n; i++){        if(arr[i] < min_value){            min_value = arr[i]; // store the min value        }    }    return min_value;}int max(int arr[], int n){       int max_value = INT_MIN;    for(int i = 0; i<n; i++){        if(arr[i] > max_value){            max_value = arr[i]; // store the max value        }    }    return max_value;}
int main() {  int t;  cin >>t; while(t--){     int n;    cin>>n;    int arr[n];        for(int i = 0; i<n; i++){        cin >> arr[i];    }    //Direct using STL Function     int Max = INT_MIN;     int Min = INT_MAX;          for(int i =0; i< n; i++){          Max = max(Max, arr[i]);          Min = min(Min, arr[i]);     }    // using create your one funtion   //  int Max = max(arr,n);   //  int Min = min(arr,n);          cout << Max << " " << Min <<endl; }return 0;}
0
ujjwal013 months ago
#include <iostream>
#include<climits>
#include<algorithm>
using namespace std;

int main() {
	//code
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	   cin>>n;
	   int max=INT_MIN;
	   int min=INT_MAX;
	   int a[n];
	   for(int i=0;i<n;i++){
	       cin>>a[i];
	       max=std::max(max,a[i]);
	       min=std::min(min,a[i]);
	   }
	   cout<<max<<" "<<min<<endl;
	}
	return 0;
}
+1
adibose93 months ago
#SIMPLE AND UNDERSTANDABLE PYTHON CODE
#UPVOTING WOULD BE HIGHLY APPRICIABLE
 
t=int(input()) #input for test casel=list() #created and empty list to store multiple arraysfor i in range(t): #loop so that for every test case, we can input an array   n=int(input())    #size of array   lst=list(map(int, input().split()))  #creating array   l.append(lst) #adding array to the list   for arr in l: #looping through the arrays inside the list   print(max(arr),min(arr)) #printing out the max and min values
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.