Skip to content

Commit 2b16f7a

Browse files
authored
CodeChef Solution for MagicalDoors
CodeChef Starters 13 Division 3 link : https://www.codechef.com/START13C/problems/MAGDOORS
1 parent 4986f7c commit 2b16f7a

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

CodeChef/MagicalDoors.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Question;
2+
3+
Chef wants to cross a hallway of N doors. These N doors are represented as a string. Each door is initially either open or close, represented by 1 or 0 respectively. Chef is required to go through all the doors one by one in the order that they appear, starting from the leftmost door and moving only rightwards at each step.
4+
5+
To make the journey easier, Chef has a magic wand, using which Chef can flip the status of all the doors at once. Determine the minimum number of times Chef has to use this wand to cross the hallway of doors.
6+
7+
For example, the doors are 10011. Chef will start from the left and enter the first door. After passing through that door, the magic wand is waved. This flips the string to 01100. Now Chef passes through the next two doors in one go. Again, just before reaching the 4th door, the magic wand is waved. Now that the string is in its original state, Chef passes through the last two doors as well. The minimum number of times the magic wand needed to be used here was 2.
8+
Input Format
9+
First line will contain T, number of testcases. Then the testcases follow.
10+
Each testcase contains of a single string S, representing the doors as given in the problem.
11+
Output Format
12+
For each test case, print a single line containing one integer denoting the minimum number of times the magic wand needs to be used.
13+
14+
Sample Input
15+
3
16+
111
17+
010
18+
10011
19+
20+
Sample Output
21+
0
22+
3
23+
2
24+
25+
*/
26+
27+
//code
28+
/* package codechef; // don't place package name! */
29+
import java.util.*;
30+
import java.lang.*;
31+
import java.io.*;
32+
/* Name of the class has to be "Main" only if the class is public. */
33+
class MagicalDoors
34+
{
35+
public static void main (String[] args) throws java.lang.Exception
36+
{
37+
Scanner in = new Scanner(System.in);
38+
int t = in.nextInt();
39+
while(t>0)
40+
{
41+
42+
StringBuilder str = new StringBuilder(in.next());
43+
long res = 0;
44+
if(str.charAt(0) == '0')
45+
res++;
46+
for(int i = 1; i<str.length(); i++)
47+
{
48+
if(str.charAt(i) != str.charAt(i-1))
49+
res++;
50+
}
51+
System.out.println(res);
52+
t--;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)