Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Even Odd Queries problem solution

YASH PAL, 31 July 202411 February 2026
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("%lldn",n)
#define pfd(n) printf("%dn",n)
#define pfld(n) printf("%ldn",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;
}
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes