top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Find smallest and largest element in an array in constant time complexity.

+1 vote
607 views

explain algorithm or program needed.

posted Jun 28, 2016 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

This is the most efficient function to find minimum and maximum value of a array, although comparisons in if conditions will take constant time but it will be compared n times because of the for loop, thus complexity is O(n), there is no way to find directly the minimum and maximum value in an array.

void find MinAndMax(int A[ ], int n){
     int min=328664,max=-328664
      for(int i=0;i<n;i++){
          if(A[i]>max)
             max=A[i];
         if(A[i]<min)
            min=A[i];
         printf("minimum=%d and maximum =%d",min,max);
      }
}
answer Jun 30, 2016 by Shahsikant Dwivedi
Thanks shashikant :D
Similar Questions
+3 votes

In an "N" element integer sorted array, a particular elements is repeating "(N/2)+1" times. How much time it take to find the repeating element.

+1 vote

Given 3 sorted array of size x, y, z. what is the minimum time taken to find the kth smallest element in the merged sorted array.

0 votes

Given an unordered array A of size n and integer x. What is the best complexity to find two elements in A whose sum is x?
Share the algo, its complexity and if possible C code.

...