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
ishankvarshney13in 11 hours
struct pair getMinMax(long long int arr[], long long int n) {      long long min=INT_MAX;   long long max=INT_MIN;   for(int i=0;i<n;i++)   {       if(min>arr[i])       min=arr[i];       if(max<arr[i])       max=arr[i];   }   return {min,max};}
please tell me what is the error?
+1
harshscodein 8 hours
long long max=INT_MIN;
    long long min=INT_MAX;
    for(int i=0;i<n;i++)
    {
        max=max<a[i]?a[i]:max;
        min=min>a[i]?a[i]:min;
    }
    return {min,max};
0
anu149264in 1 minute
JAVA SOLUTION.........
class Compute {   static pair getMinMax(long arr[], long n)     {       //Write your code here       long largest=Integer.MIN_VALUE;       long smallest=Integer.MAX_VALUE;                     for(int i=0;i<n;i++){           if(arr[i] >largest){               largest=arr[i];           }           if(arr[i] <smallest){               smallest=arr[i];           }       }       pair npair=new pair(smallest,largest);       return npair;   }}
0
govindgautam33310 hours ago
pair<long long, long long> getMinMax(long long a[], int n) {      pair<long long,long long>ans(INT_MAX,INT_MIN);
   for(long long i=0;i<n;i++)   {       if(a[i]<ans.first)       ans.first=a[i];       if(a[i]>ans.second)        ans.second=a[i];   }   return ans;}
0
5f2wpb3plfsg1ukzdkg59penrcckudo1r3su1sbr17 hours ago
pair<int,int> num;num.first=a[0];num.second=a[0];
   for(int i=0;i<n;i++)   {       if(a[i]<num.first)       {           num.first=a[i];                  }       else if(a[i]>num.second)       {           num.second=a[i];       }          }   return num;
0
madhuranjansharma48
This comment was deleted.
+1
hjadoun92 days ago
pair<long long, long long> getMinMax(long long a[], int n) {   pair<long long, long long> minmax;   int i;   if(n%2==0)   {       if(a[1]<a[0])       {          minmax.first=a[1];          minmax.second=a[0];       }       else       {           minmax.first=a[0];          minmax.second=a[1];       }       i=2;          }   else   {     minmax.first=a[0];     minmax.second=a[0];     i=1;     }   while(i<n-1)   {       if(a[i]<a[i+1])       {           if(a[i]<minmax.first)           minmax.first=a[i];           if(a[i+1]>minmax.second)           minmax.second=a[i+1];       }       else       {            if(a[i]>minmax.second)           minmax.second=a[i];           if(a[i+1]<minmax.first)           minmax.first=a[i+1];       }       i+=2;   }   return minmax;}
0
visionsameer392 days ago
python
  def getMinMax( a, n):   return min(a),max(a)
0
anmolcs192 days ago
//JAVA SOLUTION
 
 static pair getMinMax(long a[], long n)     {       long max = a[0];       long min = a[0];              for(int i=0;i<n;i++){           if(a[i]> max){           max = a[i];                      } else if(a[i]<min){               min = a[i];           }   }   return new pair(min,max);    }} 
0
mayurcosmos0073 days ago
class Compute 
{
   static pair getMinMax(long a[], long n)  
   {
       long min = a[0], max = a[0];
       
       for(int i=0; i<n; i++){
           
           if(a[i] < min) min = a[i];
           if( a[i] > max) max = a[i];
       }
       
       pair p = new pair(min, max);
       
       return p;
   }
}
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.