0% found this document useful (0 votes)
8 views3 pages

MaxMin Algorithm Implementation

Uploaded by

sammeg.demanna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

MaxMin Algorithm Implementation

Uploaded by

sammeg.demanna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MAX MIN ALGORITHM

#include <iostream>

using namespace std;

void MaxMin(int a[], int i, int j, int &max, int &min) {

int mid, max1, min1;

// If there is only one element

if (i == j) {

max = min = a[i];

// If there are two elements

else if (i == j - 1) {

if (a[i] < a[j]) {

max = a[j];

min = a[i];

} else {

max = a[i];

min = a[j];

// If there are more than two elements

else {

mid = (i + j) / 2;

MaxMin(a, i, mid, max, min); // Recursively find max and min in left half

MaxMin(a, mid + 1, j, max1, min1); // Recursively find max and min in right half

if (max1 > max)

max = max1;

if (min1 < min)


min = min1;

int main() {

int n;

cout << "Enter number of elements: ";

cin >> n;

int a[n];

cout << "Enter elements:\n";

for (int i = 0; i < n; i++)

cin >> a[i];

int max, min;

MaxMin(a, 0, n - 1, max, min);

cout << "Maximum element: " << max << endl;

cout << "Minimum element: " << min << endl;

return 0;

OUTPUT

You might also like