given a list of integers return elements a b c such that a b c 0 args nums list of integers returns list of lists of integers where sumeachlist 0 examples findtripletswith0sum1 0 1 2 1 4 1 1 2 1 0 1 findtripletswith0sum findtripletswith0sum0 0 0 0 0 0 findtripletswith0sum1 2 3 0 1 2 3 3 0 3 3 1 2 2 1 3 2 0 2 1 0 1 function for finding the triplets with a given sum in the array using hashing given a list of integers return elements a b c such that a b c 0 args nums list of integers returns list of lists of integers where sumeachlist 0 examples findtripletswith0sumhashing1 0 1 2 1 4 1 0 1 1 1 2 findtripletswith0sumhashing findtripletswith0sumhashing0 0 0 0 0 0 findtripletswith0sumhashing1 2 3 0 1 2 3 1 0 1 3 1 2 2 0 2 2 1 3 3 0 3 time complexity on2 auxiliary space on initialize the final output array with blank set the initial element as arri to store second elements that can complement the final sum current sum needed for reaching the target sum traverse the subarray arri1 required value for the second element verify if the desired value exists in the set finding triplet elements combination include the current element in the set for subsequent complement verification return all the triplet combinations given a list of integers return elements a b c such that a b c 0 args nums list of integers returns list of lists of integers where sum each_list 0 examples find_triplets_with_0_sum 1 0 1 2 1 4 1 1 2 1 0 1 find_triplets_with_0_sum find_triplets_with_0_sum 0 0 0 0 0 0 find_triplets_with_0_sum 1 2 3 0 1 2 3 3 0 3 3 1 2 2 1 3 2 0 2 1 0 1 function for finding the triplets with a given sum in the array using hashing given a list of integers return elements a b c such that a b c 0 args nums list of integers returns list of lists of integers where sum each_list 0 examples find_triplets_with_0_sum_hashing 1 0 1 2 1 4 1 0 1 1 1 2 find_triplets_with_0_sum_hashing find_triplets_with_0_sum_hashing 0 0 0 0 0 0 find_triplets_with_0_sum_hashing 1 2 3 0 1 2 3 1 0 1 3 1 2 2 0 2 2 1 3 3 0 3 time complexity o n 2 auxiliary space o n initialize the final output array with blank set the initial element as arr i to store second elements that can complement the final sum current sum needed for reaching the target sum traverse the subarray arr i 1 required value for the second element verify if the desired value exists in the set finding triplet elements combination include the current element in the set for subsequent complement verification return all the triplet combinations
from itertools import combinations


def find_triplets_with_0_sum(nums: list[int]) -> list[list[int]]:
    
    return [
        list(x)
        for x in sorted({abc for abc in combinations(sorted(nums), 3) if not sum(abc)})
    ]


def find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]]:
    
    target_sum = 0

    
    output_arr = []

    
    for index, item in enumerate(arr[:-2]):
        
        set_initialize = set()

        
        current_sum = target_sum - item

        
        for other_item in arr[index + 1 :]:
            
            required_value = current_sum - other_item

            
            if required_value in set_initialize:
                
                combination_array = sorted([item, other_item, required_value])
                if combination_array not in output_arr:
                    output_arr.append(combination_array)

            
            
            set_initialize.add(other_item)

    
    return output_arr


if __name__ == "__main__":
    from doctest import testmod

    testmod()