'Silver and Gold GFG problem (https://practice.geeksforgeeks.org/contest/job-a-thon-for-internship/problems) it is the link of above problem
I have write code and it producing correct output but it can not fill all requirements please optimize my code or give me some suggestion.
It is my code...
// it is my code it produces correct output but not complete requirement.
public String flipCoins(int N,String s) {
char ch[] = new char[N];
for(int i=0;i<N;i++){
ch[i] = s.charAt(i);
}
for(int i=1;i<N;i++){
if(ch[i] == '1' || ch[i-1] == '1'){
ch[i] = '1';
ch[i-1] = '1';
}else if(ch[i] == '0' || ch[i-1] == '0'){
ch[i] = '1';
ch[i-1] = '1';
}else if(ch[i] == '0' || ch[i-1] == '1'){
ch[i] = '1';
ch[i-1] = '0';
}else if(ch[i] == '1' || ch[i-1] == '0'){
ch[i] = '0';
ch[i-1]='1';
}
}
int count = 0;
for(char c: ch){
if(c == '1')
count++;
}
if(count == N)
return "Yes";
else
return "No";
}
Solution 1:[1]
What I learned from the question is that you can flip any number of time, so the question boils down to find whether there are even nos. of zeroes or not beacuse you can swap 00 pair to make 11 else it would always be 10 or 01
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kai - Kazuya Ito |
