Code Works but not accepting

// Take care of mod becoming -ve

#include<bits/stdc++.h>
#define fro(i,s,e) for(auto i=s;i<=e;++i)
#define int long long
#define ld long double
#define fr(i,n) for(auto i=0;i<n;i++)
#define ff first
#define ss second
#define mp make_pair
#define vi std::vector
#define mii map<int,int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int,int>
#define tc int t;cin>>t;while(t–)
#define FILE freopen(“input.txt”,“r”,stdin); freopen(“output.txt”,“w”,stdout)
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inp(a) long long a;cin>>a;
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type arr=new type[n];
#define endl “\n”
const int mod = 1000000007;
#define printclock cerr<<"Time : "<<1000
(ld)clock()/(ld)CLOCKS_PER_SEC<<“ms\n”;
using namespace std;
const int MAXN = 100005;
const int INF = 20000000000000;
#define fill(s,x) memset(s,x,sizeof(s));
inline int add(int a, int b) {return ((a % mod + b % mod) % mod);}
inline int sub(int a, int b) {return ((a % mod - b % mod) % mod);}
inline int mult(int a, int b, int md) {return ((a % md) * (b % md)) % md;}
// typedef tree<int, null_type, less, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by find(all(x),key)-x.begin()
// find_if(iterator_first,iterator_last,unary function) returns first iterator if any element corresponding to unary function exists and if no such element is found it retrurns the last iterator
/For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; it returns 25/
/search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1 based on pred function the iterator correspond
to the first element of sequence if it exists and if not then th elast iterator is obtained.
/
int binsearch(int
a, int key) {
int index = 0;
int n = sizeof(a) / sizeof(a[0]);
for (auto jump = n / 2; jump >= 1; jump++) {
while (index + jump < n and a[index + jump] <= key)index += jump;
}
if (a[index] == key)
return index;
return -1;
}
long long power(long long a, long long b, int md) {
long long ans = 1;
while (b > 0) {
if (b & 1) {ans = (ans * a) % md;}
a = (a * a) % md;
b >>= 1;
}
return ans;
}
int findDuplicate(vector& nums) {
int ans;
fr(i, nums.size()) {
if (nums[abs(nums[i]) - 1] > 0)
nums[abs(nums[i]) - 1] = -nums[abs(nums[i]) - 1];
else
ans = nums[i];
}
return abs(ans);
}
bool in_range(int i, int start, int end) {
return (i >= start && i <= end);
}
// int power[MAXN];
bool all_character_same(string s) {
return (s.find_first_not_of(s[0]) == string::npos);
}
int signum(int x) {
if (x != 0)
return x > 0 ? 1 : -1;
return 0;
}
// Maximum sub-array sum
void solve() {
int n;
cin >> n;
string s, s1, s2;
cin >> s;
for (int i = 0; i < 2 * n; i += 2) {
s1 += s[i];
}
for (auto i = 1; i < 2 * n; i += 2) {
s2 += s[i];
}
if (count(all(s1), ‘1’) == count(all(s2), ‘1’)) {
cout << 2 * n << “\n”;
return ;
}
vi miss(2, 0), scored(2, 0);
for (int i = 0; i < n; i++) {
if (s1[i]^s2[i]) {
if (s1[i] == ‘0’) {
scored[1]++;
miss[0]++;
}
else if (s2[i] == ‘0’) {
miss[1]++;
scored[0]++;
}
//Base Cases
if (scored[0] >= n - 1 && miss[1] >= 2) {
cout << 2 * (i + 1) << “\n”;
return;
}
else if (scored[1] >= n - 1 && miss[0] >= 2) {
cout << 2 * i + 1 << “\n”;
return;
}

        if (scored[0] - scored[1] >= (n - i - 1)) {
            cout << 2 * (i + 1) << "\n";
            return ;
        }
        else if (scored[1] - scored[0] >= (n - i - 1)) {
            cout << 2 * i + 1 << "\n";
            return ;
        }

    }
}

}

int32_t main() {
fio;
#ifndef ONLINE_JUDGE
FILE;
#endif
// indexed_set s;
// s.insert(2);
// s.insert(3);
// s.insert(7);
// s.insert(9);
// auto x = s.find_by_order(2);
// cout << *x << endl;

tc {
    solve();
}


printclock;
return 0;

}

Format your code and tell which question is it?

1 Like