Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
Programmingoneonone

HackerEarth Even Odd Queries problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes