Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • 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

LEARN EVERYTHING ABOUT PROGRAMMING

HackerEarth Help Ashu problem solution

YASH PAL, 31 July 2024
In this HackerEarth Help Ashu problem solution Ashu and Shanu are best buddies. One day Shanu gives Ashu a problem to test his intelligence.He gives him an array of N natural numbers and asks him to solve the following queries:-
  1. Query 0:- modify the element present at index i to x.
  2. Query 1:- count the number of even numbers in range l to r inclusive.
  3. Query 2:- count the number of odd numbers in range l to r inclusive.
HackerEarth Help Ashu problem solution

HackerEarth Help Ashu problem solution.

#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
int BIT[MAX];
void upd(int idx,int val){
for(int i=idx;i<MAX;i+=i&(-i))
BIT[i]+=val;
}
int query(int val){
int ret=0;
while(val){
ret+=BIT[val];
val-=val&(-val);
}
return ret;
}
int a[MAX];
int main(){
int n,i;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]%2==0)
upd(i,1);
}
int q,l,r,ch;
cin>>q;
while(q--){
cin>>ch>>l>>r;
if(ch==0){
if(r%2==1&&a[l]%2==0)
upd(l,-1);
if(r%2==0&&a[l]%2==1)
upd(l,1);
a[l]=r;
}
else if(ch==1){
int cnt=query(r)-query(l-1);
cout<<cnt<<endl;
}
else{
int cnt=query(r)-query(l-1);
cout<<(r-l+1)-cnt<<endl;
}
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

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