thank you so much!
i got it right with scanner classπ
#include<bits/stdc++.h>
using namespace std;
int main( ) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,n,i,j;
cin>>t;
while(t--) {
cin>>n;
string a,b;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
cin>>b[j]; }
if(b[i]=='0') {
a[i]='1';}
else {a[i]='0'; }
cout<<a[i];}
cout<<endl; }
return 0; }
// Why I am getting a SIGABRT (here) ?
My logic is:
Count the frequency of 1 and 0 respectively.
Check for these conditions:
- Count of 1 == 0
- Count of 0 == 0
- Count of 1 < Count of 0
- Count of 0 < Count of 1
- Count of 1 == Count of 0
Respectively print β0β and β1β as required.
Here is my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tc;
cin>>tc;
while(tcβ)
{
int n;
cin>>n;
vector res;
string d;
int count_zero = 0;
int count_one = 0;
int max_count_one = INT_MIN;
int max_count_zero = INT_MIN;
for(int i = 0 ; i < n ; i++)
{
cin>>d;
res.push_back(d);
}
for(auto i: res)
{
string d = i;
for(int j = 0 ; j < d.length() ; j++)
{
if(d[j] == 1)
{
count_one++;
}
else{
count_zero++;
}
}
max_count_one = max(max_count_one, count_one);
max_count_zero = max(max_count_zero, count_zero);
count_one = 0;
count_zero = 0;
}
if(max_count_zero == 0 or max_count_one == d.length())
{
for(int i = 0 ; i < max_count_one ; i++)
{
cout<<β0β;
}
cout<<endl;
}
else if(max_count_one == 0 or max_count_zero == d.length())
{
for(int i = 0 ; i < max_count_zero ; i++)
{
cout<<β1β;
}
cout<<endl;
}
else if(max_count_zero < max_count_one)
{
for(int i = 0 ; i < (max_count_one + max_count_zero) ; i++)
{
cout<<β1β;
}
cout<<endl;
}
else if(max_count_one < max_count_zero){
for(int i = 0 ; i < (max_count_zero + max_count_one) ; i++)
{
cout<<β0β;
}
cout<<endl;
}
else if(max_count_one == max_count_zero)
{
for(int i = 0 ; i < (max_count_zero + max_count_one) ; i++)
{
cout<<β1β;
}
cout<<endl;
}
else{
}
}
}
Can you please tell the error? Why, I canβt submit this code?
Why am I getting WA? I have used the set approach. Sample and manually made testcases are giving correct answer on IDE.
import java.lang.reflect.Array;
import java.util.*;
public class CP {
public static void main(String[] args) {
try {
Scanner s = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
int t = s.nextInt();
while(t-- > 0){
int n = s.nextInt();
String st[] = new String[n];
for(int i = 0; i < n; i++){
st[i] = s.next();
}
Set<Integer> hs = new HashSet<>();
for(int i = 0; i < n; i++){
hs.add(Integer.parseInt(st[i] , 2));
}
int j = 0;
while(hs.contains(j)){
j++;
}
String ans = Integer.toBinaryString(j);
while (ans.length() != n){
ans = "0" +ans;
}
sb.append(ans+"\n");
}
System.out.println(sb);
}catch (Exception e){
System.out.println(e);
}
}
}
can anyone say why this didnβt work??
int n; cin>>n;
string s, x; // x+='0'; // for(int i=1; i<n-1; i++){ // x+='0'; // } // x+='1'; // int flag = 0; // int i=0; // while(n--){ // cin>>s; // if(s==x){ // flag ==1; // if(flag==1){ // if(x[i]=='0') x[i]='1'; // else x[i]='0'; // i++; // flag = 0; // } // } // // else continue; // } // cout<<x<<endl;