Find minimum and maximum element in an array | Practice | GeeksforGeeks

Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.
 
Example 1:
Input:
N = 6
A[] = {3, 2, 1, 56, 10000, 167}
Output:
min = 1, max =  10000
 
Example 2:
Input:
N = 5
A[]  = {1, 345, 234, 21, 56789}
Output:
min = 1, max = 56789
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function getMinMax() which takes the array A[] and its size N as inputs and returns the minimum and maximum element of the array.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 105
1 <= Ai <=1012
0
shreyshah17120028 hours ago
def getMinMax( a, n):   temp=0   for x in a:       if x<temp:           temp=x   min=temp   max=0   for x in a:       if x <min:           min=x       elif x>max:           max=x       else:           pass   return min,max   
0
piyush4for11 hours ago
 pair<int,int> ans;   priority_queue<int> getMaximum;   priority_queue <int, vector<int>, greater<int>> getMinimum;      for(int i=0;i<n;i++){       getMaximum.push(a[i]);       getMinimum.push(a[i]);   }   ans.first = getMinimum.top();   ans.second = getMaximum.top();   return ans;
0
devanshdas14
This comment was deleted.
0
ifeelrook1 day ago
pair<long long, long long> getMinMax(long long a[], int n) {

    //Your code here
    long long max=a[0];
    long long min=a[0];
    pair<long long,long long> p;
    for(int i=1;i<n;i++){
        if(a[i]<min) min=a[i];
        if(a[i]>max) max=a[i];
    }
    p=make_pair(min,max);
    return p;
}
 
0
mukundrssharma2 days ago
class Compute {   static pair getMinMax(long a[], long n)     {       //Write your code here         long maxm=-2147483648;   long minm=2147483647;   for(int i=0;i<n;i++){       if(a[i]>maxm){           maxm=a[i];           }          }   for(int i=0;i<n;i++){       if(minm>a[i]){           minm=a[i];       }   } pair p= new pair(minm,maxm);   return p;   }    } 
0
harshscode3 days ago
 long long int ma=INT_MIN,mi=INT_MAX;
   
   for(int i=0;i<n;i++)
   {
       if(a[i]>ma)
       ma=a[i];
       
       if(a[i]<mi)
       mi=a[i];
   }
   
   return {mi,ma};
0
rahilarahman4 days ago
python two liner code:def getMinMax( a, n):      a.sort()   return a[0], a[n-1] 
0
chhetriarjun555554 days ago
struct pair minmax;
minmax.min=arr[0];
minmax.max= arr[0];
 
for(int i=1;i<n;i++){if (arr[i] > minmax.max)               minmax.max = arr[i];                   if (arr[i] < minmax.min)               minmax.min = arr[i];
}
return minmax;
 
+1
harshscode4 days ago
 long long mi=a[0],ma=a[0];   for(int i=0;i<n;i++)   {       if(a[i]>ma)       ma=a[i];              if(a[i]<mi)       mi=a[i];   }      return {mi,ma};
+1
hk63017359394 days ago
JAVA SOLUTION.............
class Compute {   static pair getMinMax(long a[], long n)     {       Arrays.sort(a);       pair o1 = new pair(a[0],a[a.length-1]);       return o1;   }}
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.