In computational complexity theory, 3SUM is the following computational problem conjectured to require roughly quadratic time:

Given a set S of n integers, are there elements a, b, c in S such that a + b + c = 0?

It is possible to solve the algorithm in O(n2) time using simple algorithms, and matching lower bounds are known for some specialized models of computation.

The program mentioned below works only with **sorted List**

public static void SortedList3SumProblem(int[] inputArray) { int a, b, c; int length = inputArray.Length; for (int i = 0; i < length - 3; i++) { a = inputArray[i]; int k = i + 1; int l = length - 1; while (k < l) { b = inputArray[k]; c = inputArray[l]; if (a + b + c == 0) { Console.WriteLine(a + "," + b + "," + c); ; break; } else if (a + b + c > 0) l = l - 1; else k = k + 1; } } }

# Input

0, -25, -10, -7, -3, 2, 4, 8, 10

# Output

Input Array

0;-25;-10;-7;-3;2;4;8;10

——————————–

0,-10,10

-10,2,8

-7,-3,10

Filed under:
Array

Leave a comment