-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathques3.cpp
93 lines (85 loc) · 1.83 KB
/
ques3.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<iostream>
#include <vector>
using namespace std;
int print(int arr[],int n){
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
void getElement(vector<int> v)
{
int len = v.size();
cout<<"enter which element to find"<<endl;
int n;
vector<int>::iterator j;
cin>>n;
if(n>len)
cout<<"enter valid position"<<endl;
else{
// cout<<len<<" "<<n<<endl;
int &maxElement= v.at(len-1-(n-1));
int &minElement = v.at(n-1);
cout<<n<<"th "<<"minimum element is - "<<minElement<<endl;
cout<<n<<"th "<<"maximum element is - "<<maxElement<<endl;
}
}
void makeSet(int arr[],int n){
int a=0;
vector<int> v;
// print(arr,n);
for(int i=0;i<n;){
a=i;
for(int j=i+1;j<n;j++){
if(arr[i]==arr[j])
{a++;
// cout<<arr[i]<<arr[j]<<endl;
}
}
v.push_back(arr[i]);
i=a+1;
// cout<<a<<endl;
}
// std::vector<int>::iterator i;
// for(i=v.begin();i<v.end();i++){
// cout<<*i<<endl;
// }
getElement(v);
}
void sort(int arr[],int n){
for(int i=0;i<n;i++){
int max = arr[i];
int k=i;
for(int j=i;j<n;j++){
if (max<arr[j]){
// cout<<"max"<<max<<arr[j]<<endl;
max = arr[j];
k=j;
}
}
// cout<<arr[i]<<"swapped with"<<arr[k]<<endl;
int swap = arr[i];
arr[i] = max;
arr[k] = swap;
// print(arr,n);
}
// print(arr,n);
makeSet(arr,n);
}
void input(int n){
int arr[n];
cout<<"enter array"<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,n);
}
int main(){
cout<<"enter the length of array"<<endl;
int n;
cin>>n;
input(n);
return 0;
}