Question

Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the...

Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the maximum elements in an array A of n elements. Your algorithm calls itself only once within the algorithm and should return a pair (a, b) where a is the minimum element and b is the maximum element.

Algorithm Min-Max(A, n)

Input: an Array A of n elements

Output: a pair of (a, b) where a is the minimum element and b is the maximum element

Homework Answers

Answer #1

HERE IS THE CODE FOR YOU :

//Explanation has been done in the code as u already know the concept of basic searching

/*************************************************/

#include <bits/stdc++.h>
using namespace std;
// globally declaring a pair
pair<int,int> comb;

// function returning a pair

pair<int,int> func(int arr[],int n){
if(n==0) return comb;
// assigning new minimum values to the pair
comb.first=min(comb.first,arr[n-1]);
// assigning new maximum values to the pair
comb.second=max(comb.second,arr[n-1]);
return func(arr,n-1);
}

int main() {

int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];

// after all the iterations first will contain minimum element
// where as second will contain maximum element
comb=make_pair(INT_MAX,INT_MIN);
// recieving new pair in p
pair<int,int> p=func(arr,n);
// printing result
cout<<"MINIMUM ELEMENT "<<p.first<<endl;
cout<<"MAXIMUM ELEMENT "<<p.second<<endl;
return 0;
}

SOME OUTPUTS

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In java 1. Write a recursive algorithm to add all the elements of an array of...
In java 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C. 4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
3. (10 marks) Describe a recursive algorithm for finding the maximum element in a array A...
3. Describe a recursive algorithm for finding the maximum element in a array A of n elements. Analyze its time complexity using a recursion tree.
a) Give a recursive algorithm for finding the max of a finite set of integers, making...
a) Give a recursive algorithm for finding the max of a finite set of integers, making use of the fact that the max of n integers is the larger of the last integer in the list and the max of the first n-1 integers in the list. Procedure power(x,n): If (n=0): return 1 Else: return power(x,n-1) · x b) Use induction to prove your algorithm is correct
USING C++. The following divide-and-conquer algorithm is proposed for finding the simultaneous maximum and minimum: If...
USING C++. The following divide-and-conquer algorithm is proposed for finding the simultaneous maximum and minimum: If there is one item, it is the maximum and minimum, and if there are two items, then compare them, and in one comparison you can find the maximum and minimum. Otherwise, split the input into two halves, divided as evenly as possibly (if N is odd, one of the two halves will have one more element than the other). Recursively find the maximum and...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 2. Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. ALGORITHM MinDistance(A[0..n − 1])//Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its...
Python homework. Write your own versions of the Python built-in functions min and max. They should...
Python homework. Write your own versions of the Python built-in functions min and max. They should take a list as an argument and return the minimum or maximum element. Hint: Pick the first element as the minimum (maximum) and then loop through the elements to find a smaller (larger) element. Each time you find a smaller (larger) element, update your minimum (maximum). Ensure you are supplied valid input i.e list of only numbers
1- Write algorithm (in pseudo-code) for the following problem: sum of the series: 1 +2+3+…+N 2-...
1- Write algorithm (in pseudo-code) for the following problem: sum of the series: 1 +2+3+…+N 2- Convert an algorithm to a flowchart diagram. 3- Counting the primitive operations of the algorithm. 4- Determine algorithm complexity. 5- Write a complete C++ program for the previous algorithm where the program should contain the following:  Display messages.  Input any number;  Display the series.  Display the sum of the series.
Write a method that take in an array of integers, find the maximum and minimum, and...
Write a method that take in an array of integers, find the maximum and minimum, and return an array containing four elements in the following order. max integer, max integer index, min integer, min integer index
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100 is surjective. That is, supply a “Method” to go with Input: A function (array) f with f(i) ∈ Z100 for i = 0, 1, . . . , 99. Output: Boolean B. B=‘true’ if f is surjective, ‘false’ otherwise. Try to make your algorithm as efficient as possible. Do NOT include an implementation of your algorithm in a programming language. (b) How many comparisons...