In this HackerEarth Troubling Triplet problem solution, A Puzzle is a game, problem, or toy that tests a person's ingenuity. In a puzzle, one is required to put pieces together, in a logical way, in order to arrive at the correct solution of the puzzle. There are different types of puzzles for different ages.Puzzles are often devised as a form of entertainment but they can also arise from serious mathematical or logistical problems.

Ananya and Bhavya are on their way to Goa. On their trip they visit a lot of different places. But one place left Ananya awe struck. She saw a lot of round big stones laid in a straight line with a number encrypted on each of them. She called this number as the value of the stones. Since they both are coders they could not leave their geekiness away for too long. So Bhavya converted the present environment to a problem. She labelled all the stones from 1 onwards. She then asked Ananya to tell her the total number of triplets such that the label of 1st stone< label of 2nd stone < label of 3rd stone and the product of value of 1st sone,value of 2nd stone and value of 3rd stone is less than or equal to a given value K that is (value of 1st stone) * (value of 2nd stone) * (value of 3rd stone)<=K. Two triplets are considered different if the label value's of both the triplets are not same. Since Ananya was not in any mood to bang her head on the stones so she asks you to help her.


HackerEarth Troubling Triple problem solution


HackerEarth Troubling Triple problem solution.

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 1000000007
#define NIL 0
#define MAXN 100005
#define EPS 1e-5
#define INF (1<<28)
typedef unsigned long long int uint64;
typedef long long int int64;

int BIT[1000006];

void upd(int x){
for(int i=x;i<=1000000;i+=i&(-i))
BIT[i]++;
}

int64 query(int x){
int64 ret=0;
while(x){
ret+=BIT[x];
x-=(x)&(-x);
}
return ret;
}
int a[2005];
int main(){
int n,k,i;
cin>>n>>k;
for(i=0;i<n;i++)
cin>>a[i];
int64 ans=0;
for(i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int64 val=a[i];
int64 val2=a[j];
val*=val2;
if(val>k)
continue;
ans+=query(k/val);
}
upd(a[i]);
}
cout<<ans;
return 0; }

Second solution

#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>

using namespace std;

#define MAXN 2000
#define MAXK 1000001

vector<long long> ar;
vector<long long>::iterator up;

int main(){
int N, K;
long long t;
scanf("%d %d", &N, &K);
assert(N>0 and N<=MAXN);
assert(K>0 and K<=MAXK);
for(int i=0;i<N;i++){
scanf("%lld", &t);
ar.push_back(t);
assert(ar[i]>0 and ar[i]<=1000000);
}
sort(ar.begin(), ar.begin()+N);
long long ans = 0;
for(int i=0;i<N;i++)
for(int j=i+1;j<N;j++){
//binary search
long long f = (long long)K / ar[i];
f = f/ar[j];
up = upper_bound(ar.begin(), ar.end(), f);
if(up-ar.begin()>j)
ans = ans + (up - (ar.begin()+j) - 1);

}
printf("%lld\n", ans);
}