What makes the first code faster than the second? I tried those 2 codes one gave me a Time limit that exceeded 1000 ms and the other works on 140ms at a long test input of CodeForces test
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int n, m, a, l, r, c(0), d(0);
scanf("%d%d", &n, &m);
while (n--)
{
scanf("%d", &a);
if (a == 1)
{
c += 1;
}
else
{
d += 1;
}
}
int up = min(c, d) * 2;
while (m--)
{
scanf("%d%d", &l, &r);
printf((r - l) % 2 == 1 && r - l < up ? "1\n" : "0\n");
}
return 0;
}
the slow one is:
#include <bits/stdc++.h>
int main()
{
int n, ones{}, _ones{}, l, r, q, temp;
std::cin >> n >> q;
while(n--)
{
std::cin >> temp;
if(temp == 1)
ones++;
else
_ones++;
}
int common = std::min(ones, _ones) * 2;
while(q--)
{
std::cin >> l >> r;
std::cout << ((r - l) % 2 == 1 && r - l < common ? "1\n" : "0\n");
}
}