Skip to content
Programming101
Programming101

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
Programming101
Programming101

Learn everything about programming

HackerEarth Cocktail Range Query problem solution

YASH PAL, 31 July 2024
In this HackerEarth Cocktail Range Query problem solution Vikas0706 is an awesome bar tender. He has never failed is satisfying his customers. One fine evening, after getting frustrated and tired a programmer came to his bar. He thought to remove all his frustration on Vikas0706.
There are N different types of drink kept in a line on the table. Every drink can be of different quantity (in ml) . Now programmer asked Vikas0706 to make a cocktail of quantity k ml using only 2 drinks. Also he has to completely consume the drink he selects. Vikas0706 is really confused which two drink to select. Now programmer didn’t stop here. He further told him to select drinks only from a range l to r (both l and r inclusive) . Vikas0706 is in a big trouble now. You are his last hope. Tell him how many different type of cocktails he can make in the specified range satisfying the conditions of the programmer.
HackerEarth Cocktail Range Query problem solution

HackerEarth Cocktail Range Query problem solution.

#include<bits/stdc++.h>
#define endl 'n'
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef std::vector<int> vi;
typedef std::vector<long long> vl;
#define all(v) v.begin(),v.end()
#define MAX 1000005
#define MOD 1000000007
#define FAST_IN_OUT ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
vector<int> has[2002];

ll search(int l,int r,int x){

ll k1= upper_bound(all(has[x]),r)-has[x].begin();
ll k2=lower_bound(all(has[x]),l)-has[x].begin();
return (k1-k2)>0?(k1-k2):0;
}

int main(){
FAST_IN_OUT

int n,q;
cin>>n>>q;
int *a=new int[MAX];
for (int i = 0; i < n; ++i)
{
cin>>a[i];
has[a[i]].pb(i);
}

while(q--) {
int l,r,x;
cin>>l>>r>>x;
ll ans=0;
l--;
r--;
for (int i = 1; i <=x ; ++i){
ll aa=0;
if(i==(x-i)){
ll v= search(l,r,i);
aa += v*(v-1);
}else{
aa=search(l,r,i)*search(l,r,x-i);
}
ans+=aa;
}
cout<<ans/2<<endl;
}
}
coding problems

Post navigation

Previous post
Next post
  • 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
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes