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.
Python3 soltuion. Time: O(nlogn). Similar to the previous comment but I implimted by own binary search algorithm and included an optimization to reduce searching once q is greater than all elements in p and r.
deftriplets(a,b,c):# Use binary search to find where q is, or should be in the arraydefbinary_search(arr,target):high,low=len(arr)-1,0mid=(high+low)//2sample=arr[mid]l_arr=len(arr)whilelow<=high:iftarget<sample:high=mid-1eliftarget>sample:low=mid+1else:# Add 1 since we want count of elements q is >=returnmid+1mid=(high+low)//2sample=arr[mid]# Add 1 since we want count of elements q is >=returnhigh+1# Will be 0 or len(arr)a,b,c=list(set(a)),list(set(b)),list(set(c))a.sort()b.sort()c.sort()a_len,c_len=len(a),len(c)max_pr=product=result=0forqinb:ifmax_pr:result+=max_prelse:p=binary_search(a,q)r=binary_search(c,q)ifpandr:product=p*rifp==a_lenandr==c_len:# Once q is > all elements in p and r, simply resuse# this max product.max_pr=productresult+=productreturnresult
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 →
Python3 soltuion. Time: O(nlogn). Similar to the previous comment but I implimted by own binary search algorithm and included an optimization to reduce searching once q is greater than all elements in p and r.