Find three elements in an array that sum to a zero.

Objec­tive:  Given an array of integer write an algorithm to find 3 elements that sum to a zero. In short a+b+c = 0.

Example

int a [] = { 3,-1,-7,-4,-5,9,10};
Elements are -4 9 -5

Approach: Brute Force

Use 3 nested loops and find the 3 elements which sum to 0.

Time Complexity: O(n^3)

Code:

Approach: Sorting

You may also like...

%d bloggers like this: