Site icon Alrich Roshan

Split houses HackerEarth Solution in C

split houses

Problem

You live in a village. The village can be represented as a line that contains n grids. Each grid can be denoted as a house that is marked as H or a blank space that is marked as ..

A person lives in each house. A person can move to a grid if it is adjacent to that person. Therefore, the grid must be present on the left and right side of that person.

Now, you are required to put some fences that can be marked as B on some blank spaces so that the village can be divided into several pieces. A person cannot walk past a fence but can walk through a house. 

You are required to divide the house based on the following rules:

Your task is to decide whether there is a possible solution. Print the possible solution.

Input format

Output format

The output must be printed in the following format:

Sample Input

5
H...H

Sample Output

YES
HBBBH

Time Limit: 1

Memory Limit: 256

Source Limit:

Explanation

Each person can reach 1 grid. Each person can reach his own houses only.

Note that HB.BH also works. Each person can reach only 1 grid.

But H..BH does not work. Because the first person can reach 3 grids but the second one can only reach 1.

H…H does not work either. The first person can reach the second person’s house which is bad.

So you need to print HBBBH because it has the most fences.

#include<stdio.h>
void main()
{
	int n,temp=0;
	scanf("%d",&n);
	char s[n];
	scanf("%s",s);
	for(int i=0;s[i]!='\0';i++)
	{
		if(s[i]=='H'&&s[i+1]=='H')
		{
			temp=1;
			break;
		}
		else if(s[i]=='.')
			s[i]='B';
	}
	if(temp==0)
	{
		printf("YES\n");
		printf("%s",s);
	}
	else
	printf("NO");
}
Exit mobile version