File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* ************
2
+ Problem statement:
3
+ SILVER AND GOLD
4
+
5
+ There are N coins kept in a line.
6
+ Each coin has two sides - one is colored gold
7
+ and the other silver. You can flip two adjacent
8
+ coins any number of times. You need to make
9
+ the gold-colored side of every coin facing up.
10
+ You are given the initial status of coins
11
+ in a binary string s where 1 represents the
12
+ gold side facing up and 0 represents the silver
13
+ side facing up. If it is possible to make the
14
+ gold-colored side of every coin facing up,
15
+ return Yes, otherwise return No
16
+
17
+ Input:
18
+ N = 8, s = "11001100"
19
+ Output:
20
+ Yes
21
+ Explanation:
22
+ Flipping 3rd and 4th coin together and 7th
23
+ and 8th coin together will do the task.
24
+ ********* */
25
+ public class SilverAndGold {
26
+ //Silver and Gold Problem
27
+ public static String flipCoins (int N ,String s ) {
28
+ // Code here
29
+ int count0 , count1 ;
30
+ count0 = count1 = 0 ;
31
+ for (int i = 0 ;i < N ;i ++){
32
+ int result = Character .compare ('0' ,s .charAt (i ));
33
+ if (result == 0 ){
34
+ count0 ++;
35
+ }
36
+ else {
37
+ count1 ++;
38
+ }
39
+ }
40
+ if (count0 %2 == 0 ){
41
+ return "Yes" ;
42
+ }
43
+ else {
44
+ return "No" ;
45
+ }
46
+ }
47
+ public static void main (String args []){
48
+
49
+ System .out .println (flipCoins (8 ,"10010100" ));//test case 1
50
+ System .out .println (flipCoins (8 ,"11001100" ));//test case 2
51
+ System .out .println (flipCoins (2 ,"00" ));//test case 2
52
+
53
+
54
+
55
+
56
+ }
57
+ }
58
+
59
+
60
+
61
+
You can’t perform that action at this time.
0 commit comments