Grokking Oracle Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Concise Python Code

himanshu1495

Oct 28, 2023

class Solution:   def searchTriplets(self, arr):     triplets = []     # TODO: Write your code here     arr.sort()     n=len(arr)     for i in range(0,n-2):#go upto the third last element only       el=arr[i]       if i>0 and arr[i]==arr[i-1]:         continue       j=i+1       k=n-1       while(j<k):         #TODO: logic for skipping second element already chosen         el2=arr[j]         el3=arr[k]         if arr[j]+arr[k]==(0-el):           triplets.append([el,el2,el3])           j+=1           k-=1             while(j<k and arr[j]==el2):             j+=1           while(j<k and arr[k]==el3):             k-=1         elif arr[j]+arr[k]<(0-el):           j+=1           while(j<k and arr[j]==el2):             j+=1         else:           k-=1           while(j<k and arr[k]==el3):             k-=1     return triplets

1

1

Comments
Comments
S
saifs a year ago

concise lol

On this page