We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>usingnamespacestd;// Complete the countTriplets function below.typedeflonglongll;intmain(){cin.tie(NULL);ios_base::sync_with_stdio(false);longn,r;cin>>n>>r;map<int,long>mp2,mp3;//mp2 to hold count of needed values after this one to complete //2nd part of triplet//mp3 to hold count of needed values to complete tripletintval;longlongres=0;while(n--){cin>>val;if(mp3.count(val))//This value completes mp3[val] tripletsres+=mp3[val];if(mp2.count(val))//This value is valid as 2° part of mp2[val] tripletsmp3[val*r]+=mp2[val];mp2[val*r]++;//"Push-up" this value as possible triplet start}cout<<res<<endl;return0;}
Count Triplets
You are viewing a single comment's thread. Return to all comments →