You are viewing a single comment's thread. Return to all comments →
This Javascript solution works except all 1's test cases. I don't know the reason why time limit exceptions happened in many cases though.
function countTriplets(arr, r) { let count = 0; for (let i=0; i<arr.length - 2; i++) { let base = arr[i]; let baseTwo = arr[i] * r; let baseThree = arr[i] * r * r let twoCount = 0; let threeCount = 0; twoCount = arr.slice(i+1).filter(elem => elem === baseTwo).length; threeCount = arr.slice(i+1).filter(elem => elem === baseThree).length; count = count + (twoCount * threeCount); } return count; }
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
This Javascript solution works except all 1's test cases. I don't know the reason why time limit exceptions happened in many cases though.