
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Maximum and minimum of an array using minimum number of comparisons in C
We are given with an array of integers. The task is to find the minimum and maximum element of the array in the minimum number of comparisons.
Input
Arr[] = { 1,2,4,5,-3,91 }
Output
Maximum element : 91 Minimum Element : -3
Explanation − Here to minimize the number of comparisons, we will initialize the maximum and minimum element with Arr[0]. And starting from the 2nd element compare each value with min and max and update accordingly.
Input
Arr[] = { 10,20,21,31,18,11 }
Output
Maximum element : 31 Minimum Element : 10
Explanation − Here also, to minimize the number of comparisons, we will initialize the maximum and minimum element with Arr[0]. And starting from the 2nd element compare each value with min and max and update accordingly.
Approach used in the below program is as follows
We take an integer array having numbers as Arr[]
The function getresult( int arr[],int n) is to find the maximum and minimum element present in the array in minimum no. of comparisons.
If there is only one element then we will initialize the variables max and min with arr[0] .
For more than one element, we will initialize max with arr[1] and min with arr[0].
Inside for loop start traversing from third element ( i=2 ) till last.
Now we will compare each value ( arr[i] ) with min and max. If it is less than min, update min with arr[i]. If it is more than max, then update max with arr[i].
In the end print the results stored in max and min variables.
Example
#include <stdio.h> #include <math.h> int getresult(int arr[], int n){ int min=0,max=0; /*If there is only one element then return it as min and max both*/ if (n == 1) { min=max=arr[0]; } /* If there are more than one elements, then initialize min and max*/ if (arr[0] > arr[1]){ max = arr[0]; min = arr[1]; } else{ max = arr[1]; min = arr[0]; } for (int i = 2; i<n; i++){ if (arr[i] > max) max = arr[i]; else if (arr[i] < min) min = arr[i]; } printf(" Minimum element: %d", min); printf(" Maximum element: %d", max); } /* Driver program to test above function */ int main(){ int arr[] = {200, 191, 112, -11, 330, 60}; int n = 6; getresult (arr, n); }
Output
If we run the above code it will generate the following output −
Minimum element: -11 Maximum element: 330
- Related Articles
- Middle of three using minimum comparisons in C++
- Rearrange an Array in Maximum Minimum Form using C++
- How to find the minimum and maximum element of an Array using STL in C++?
- C Program to Minimum and Maximum prime numbers in an array
- Find the Number of Subarrays whose Minimum and Maximum are Same using C++
- Product of maximum in first array and minimum in second in C
- C++ program to rearrange an array in maximum minimum form
- Minimum number of swaps required to sort an array in C++
- C# program to find maximum and minimum element in an array\n
- Program to find the minimum (or maximum) element of an array in C++
- Recursive Programs to find Minimum and Maximum elements of array in C++
- Function that returns the minimum and maximum value of an array in JavaScript
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum and Maximum number of pairs in m teams of n people in C++
- Minimum number of elements to be removed to make XOR maximum using C++.
