Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Battlefield (MEDIUM) problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth Battlefield (MEDIUM) problem solution The war goes on. The Knights and the Demons are fighting at their fullest but as expected, the evil is dominating the good ones. In order to defeat the Demons, the knights have to come together.
 
Initially, everyone is standing in a circular path at the top of the Demon tower. The warrior at index 1 is standing next to the warrior at index n and before the warrior at index 2. In order to win the fight, the knights have to come together.
 
The knights have a special power through which they can swap themself with anyone.
Help the Knights decide the minimum number of swaps they have to do so that all of them stand together.
 
HackerEarth Battlefield (MEDIUM) problem solution
 
 

HackerEarth Battlefield (MEDIUM) problem solution.

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define SZ 666666
#define si(n) scanf("%d",&n);
#define sl(n) scanf("%ld",&n);
#define pi(n) printf("%dn",n);
#define pl(n) printf("%ldn",n);
#define pf(n) printf("%fn",n);
#define FILL(a,b) memset(a,0,sizeof(b));
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,a,b) for(int i=1;i<=b;i++)
const int INF=1e9+5;
const int MOD=1000000007;


int main(){
int T;
scanf("%d",&T);
while(T--){
int N,k=0,countK=0;
string slide;

scanf("%d",&N);
cin>>slide;

slide+=slide;

for(int i=0;i<N;i++)if(slide[i]=='K')++k;

for(int i=0;i<k;i++){
if(slide[i]=='K')++countK;
}
int maxK = countK;

for(int i=k;i<(N+N);i++){
countK = countK + ((slide[i]=='K')?1:0) - ((slide[i-k]=='K')?1:0);
//printf("%dn",countK);
maxK = max(maxK,countK);
}

printf("%dn",(k-maxK));
}
return 0;
}
 

 

 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes