Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Battlefield (MEDIUM) problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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