-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMin using divide and conquer.cpp
More file actions
53 lines (52 loc) · 955 Bytes
/
MaxMin using divide and conquer.cpp
File metadata and controls
53 lines (52 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<vector>
using namespace std;
void in(int a[],int n){
for(int i=0;i<n;i++)
cin>>a[i];
}
void print(int a[],int n){
for(int i=0;i<n;i++)
cout<<a[i];
}
int getMax(int a,int b){
if(a>b)
return a;
return b;
}
int getMin(int a,int b){
if(a<b)
return a;
return b;
}
void show(vector<int> a,int n){
for(int y=0;y<n;y++)
cout<<a[y]<<" ";
cout<<endl;
}
vector<int> MaxMin(int a[],int i,int n){
// show(a,i,n);
if(i==n){
vector<int> h;
h.push_back(a[i]);
h.push_back(a[i]);
return h;
}
int mid=(i+n)/2;
vector<int> jj;
vector<int> m=MaxMin(a,i,mid);
vector<int> k=MaxMin(a,mid+1,n);
jj.push_back(getMax(m[0],k[0]));
jj.push_back(getMin(m[1],k[1]));
return jj ;
}
int main(void){
int n;
cout<<"Size of the array-";
cin>>n;
int a[n];
cout<<"ENTER ELEMENTS-";
in(a,n);
vector<int> o=MaxMin(a,0,n-1);
cout<<"Max "<<o[0]<<endl<<"Min "<<o[1];
}