-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigBossProbelm.cpp
188 lines (146 loc) · 3.95 KB
/
BigBossProbelm.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Problem Statement
Big Boss is one of popular reality shows in India. Every year a new variant of the show comes with a new set of policies. Candidates from all over the India can participate in the show. Suppose there are N candidates who participated in the 2021 show. All the participants had to go through the selection process. The survival in the show is based on the votes obtained from the viewers. Your task is to find the sum and average of the first K participants who have achieved lowest votes from the viewers. The problem can be solved with the help of binary search tree.
Your goal is to implement the C++ program for the following-
Create a BST with the given data
Perform an in-order traversal to sort all the candidates votes.
Get the votes for the first K participants.
Display the sum and average of the first K candidates votes.
First line of input shows the number of participants followed by their cote count in new lines. The last line shows the c=value of K.
Sample Input:
7
54
51
49
52
75
74
85
3
Sample Output:
152
21.7
*/
//Method - 1 Insertion -> Inorder and pushing elements in a stack using STL -> Result
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int data){
this->data = data;
left = NULL;
right = NULL;
}
};
void inorder(Node* root, vector<int> & sorted){
if(root == NULL){
return;
}
inorder(root->left, sorted);
sorted.push_back(root->data);
inorder(root->right, sorted);
}
void insert(Node* root, int num){
if(num < root->data){
if(root->left == NULL){
Node* newNode = new Node(num);
root->left = newNode;
}
else{
insert(root->left, num);
}
}
else if(num > root->data){
if(root->right == NULL){
Node* newNode = new Node(num);
root->right = newNode;
}
else{
insert(root->right, num);
}
}
}
int main(){
int n;
vector<int> v;
while(cin >> n){
v.push_back(n);
}
int k = v[v.size()-1];
v.pop_back();
Node* root = new Node(v[0]);
for(int i=1; i<v.size(); i++){
insert(root, v[i]);
}
vector<int> sorted;
inorder(root, sorted);
int sum = 0;
for(int i=0; i<k; i++){
sum += sorted[i];
}
float avg =(float) sum/v.size();
cout << sum << endl;
cout << fixed << setprecision(1) << avg;
}
//Method - 2 Insertion -> Finding sum of first kthelement -> Result
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
struct Node * left;
struct Node * right;
};
Node * insert(Node * root, int data){
if(!root){
struct Node * node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}
if(root->data < data){
root ->right = insert(root -> right, data);
}
else{
root->left = insert(root->left, data);
}
return root;
}
int ksmallestElementSumRec(Node *root, int k, int &count)
{
if (root == NULL)
return 0;
if (count > k)
return 0;
int res = ksmallestElementSumRec(root->left, k, count);
if (count >= k)
return res;
res += root->data;
count++;
if (count >= k)
return res;
return res + ksmallestElementSumRec(root->right, k, count);
}
void ksmallestElementSum(struct Node *root, int k)
{
int count = 0;
float sum = ksmallestElementSumRec(root, k, count);
cout<<sum<<endl;
cout<<fixed << setprecision(1)<<sum/7;
}
int main()
{
int n; cin>>n;
Node * root = NULL;
for(int i = 0; i<n;i++){
int temp;cin>>temp;
root = insert(root, temp);
}
int t;cin>>t;
int arr[n];
ksmallestElementSum(root, 3);
return 0;
}