Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions CodeChef Problems/POPGATES/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
**Link to problem:** https://www.codechef.com/problems/POPGATES

**Description:**

You are Dastan, the great Prince of Persia!

After searching long for the mysterious 'Sands of Time', you have finally arrived at the gates of the city that hosts the ancient temple of the gods. However, the gate is locked and it can only be opened with a secret code, which you need to obtain by solving the following puzzle:

There is a table in front of you, with N coins placed in a row and numbered 1 through N from left to right. For each coin, you know whether it is initially showing heads or tails. You have to perform exactly K operations. In one operation, you should remove the rightmost coin present on the table, and if this coin was showing heads right before it was removed, then you should also flip all the remaining coins. (If a coin was showing heads, then after it is flipped, it is showing tails, and vice versa.)

The code needed to enter the temple is the number of coins which, after these K operations are performed, have not been removed and are showing heads. Can you find this number? The fate of Persia lies in your hands…



**Input:**

- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two space-separated integers N and K.
- The second line contains N space-separated characters. For each valid i, the i-th of these characters is 'H' if the i-th coin is initially showing heads or 'T' if it is showing tails.



**Output:**

For each test case, print a single line containing one integer ― the number of coins that are showing heads after K operations.



**Constraints:**

- 1≤T≤200
- 1≤K<N≤100



**Subtasks:**

```Subtask #1``` (100 points):** original constraints

**Example Input:**

3

5 3

H T T H T

7 4

H H T T T H H

6 1

T H T H T T



**Example Output:**

1

2

2


54 changes: 54 additions & 0 deletions CodeChef Problems/POPGATES/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//author : Aritra Banerjee

#include <stdio.h>
#include<string.h>
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N,K,i,j,x;
scanf("%d %d",&N,&K);
char a[N];
for(i=0;i<N;i++)
{
scanf("%s",&a[i]);
}
for(j=1;j<=K;j++)
{
for(i=N-1;i>=N-K;i--)
{
if(a[i]=='T')
{
a[i]=a[i+1];
}
else if(a[i]=='H')
{
for(x=0;x<i;x++)
{
if(a[x]=='H')
{
a[x]='T';
}
else if(a[x]=='T')
{
a[x]='H';
}
}
}
a[i]=a[i+1];
}
}
int count=0;
for(j=0;j<N-K;j++)
{
if(a[j]=='H')
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}