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.
Used set to remove duplicates and create order.
Put data back into vectors to retrieve indices for upper bound checks.
// Complete the triplets function below.longtriplets(vector<int>a,vector<int>b,vector<int>c){longresult=0;std::set<int>setA{a.begin(),a.end()};std::set<int>setB{b.begin(),b.end()};std::set<int>setC{c.begin(),c.end()};std::vector<int>orderedA{setA.begin(),setA.end()};std::vector<int>orderedC{setC.begin(),setC.end()};for(constauto&itemB:setB){autoidxA=std::upper_bound(orderedA.begin(),orderedA.end(),itemB);autoidxC=std::upper_bound(orderedC.begin(),orderedC.end(),itemB);autoaDiff=idxA-orderedA.begin();autocDiff=idxC-orderedC.begin();result+=aDiff*cDiff;}returnresult;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Triple sum
You are viewing a single comment's thread. Return to all comments →
Used set to remove duplicates and create order. Put data back into vectors to retrieve indices for upper bound checks.