Skip to content

Commit f7efed7

Browse files
authored
Strongest Fighter
1 parent c2662e2 commit f7efed7

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Hacker Blocks/Strongest Fighter.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Hacker Blocks */
2+
/* Title - Strongest Fighter */
3+
/* Created By - Akash Modak */
4+
/* Date - 27/08/2020 */
5+
6+
// There is a group of MMA fighters standing together in a line. Given the value of their strengths, find the strength of the strongest fighter in continuous sub-groups of size k.
7+
8+
// Input Format
9+
// First line contains an integer N, the number of fighters Followed by N integers where i'th integer denotes the strength of i'th fighter. Next line contains the size of sub-group k
10+
11+
// Constraints
12+
// 1<=N<=10^7
13+
// 1<=k<=N
14+
// 1 <= Ai <= 100000
15+
16+
// Output Format
17+
// Space separated integers in a single line denoting strength of strongest fighters in the groups.
18+
19+
// Sample Input
20+
// 5
21+
// 1 3 1 4 5
22+
// 3
23+
// Sample Output
24+
// 3 4 5
25+
// Explanation
26+
// First sub-group: 1 3 1 --> Max strength is 3
27+
// Second sub-group: 3 1 4 --> Max strength is 4
28+
// Third sub-group: 1 4 5 --> Max strength is 5
29+
30+
#include<bits/stdc++.h>
31+
using namespace std;
32+
int main() {
33+
int n,k;
34+
cin>>n;
35+
int a[n];
36+
for(int i=0;i<n;i++)
37+
cin>>a[i];
38+
cin>>k;
39+
int i;
40+
deque<int> q;
41+
for(i=0;i<k;i++)
42+
{
43+
while(!q.empty() and a[i]>a[q.back()])
44+
q.pop_back();
45+
q.push_back(i);
46+
}
47+
for(;i<n;i++){
48+
cout<<a[q.front()]<<" ";
49+
while(!q.empty() and q.front()<=i-k)
50+
q.pop_front();
51+
52+
while(!q.empty() && a[i]>=a[q.back()])
53+
q.pop_back();
54+
q.push_back(i);
55+
}
56+
cout<<a[q.front()];
57+
return 0;
58+
}

0 commit comments

Comments
 (0)