In this HackerEarth Even Odd Queries problem solution You are given an array Arr of size N, containing integers. You have to answer Q queries where each query is of the form :

K L R :- If K = 0, then you have to find the probability of choosing an even number from the segment [L,R] (both Inclusive) in the array Arr.

K L R :- If K = 1, then you have to find the probability of choosing an odd number from the segment [L,R] (both Inclusive) in the array Arr.

For each query print two integers p and q which represent the probability p/q. Both p and q are reduced to the minimal form. If p is 0 or p is equal to q print p/q alone.


HackerEarth Even Odd Queries problem solution


HackerEarth Even Odd Queries problem solution.

#include <bits/stdc++.h>
#define sflld(n) scanf("%lld",&n)
#define sfulld(n) scanf("%llu",&n)
#define sfd(n) scanf("%d",&n)
#define sfld(n) scanf("%ld",&n)
#define sfs(n) scanf("%s",&n)
#define ll long long
#define s(t) int t; while(t--)
#define ull unsigned long long int
#define pflld(n) printf("%lld\n",n)
#define pfd(n) printf("%d\n",n)
#define pfld(n) printf("%ld\n",n)
#define lt 2*idx
#define rt 2*idx+1
#define f(i,k,n) for(i=k;i<n;i++)
#define MAXN 100005

using namespace std;
int ct[MAXN][2];
int main()
{
    int t;
    sfd(t);
    while(t--)
    {
        int n,q,i;
        sfd(n);
        sfd(q);
        f(i,1,n+1)
        {
            int x;
            sfd(x);
            ct[i][x%2]=ct[i-1][x%2]+1;
            ct[i][1-x%2]=ct[i-1][1-x%2];
        }
        while(q--)
        {
            int l,r,k;
            sfd(k);
            sfd(l);
            sfd(r);
            int q=r-l+1;
            int p=ct[r][k]-ct[l-1][k];

            if(p==0||p==q)
                cout<<p/q<<endl;
            else
            {
                int g=__gcd(p,q);
                p=p/g;
                q=q/g;
                cout<<p<<" "<<q<<endl;
            }

        }
    }
    return 0;
}