data_structures.arrays.find_triplets_with_0_sum =============================================== .. py:module:: data_structures.arrays.find_triplets_with_0_sum Functions --------- .. autoapisummary:: data_structures.arrays.find_triplets_with_0_sum.find_triplets_with_0_sum data_structures.arrays.find_triplets_with_0_sum.find_triplets_with_0_sum_hashing Module Contents --------------- .. py:function:: find_triplets_with_0_sum(nums: list[int]) -> list[list[int]] 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]] .. py:function:: find_triplets_with_0_sum_hashing(arr: list[int]) -> list[list[int]] 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)