Search⌘ K

Solved Problem - Triplet Sum

Explore the two-pointer technique to solve the triplet sum problem efficiently. Understand how to identify three integers in a sorted array that add up to a specific value, optimizing from a brute force O(N3) approach to an O(N2) solution. This lesson helps you implement and analyze the algorithm for competitive programming challenges.

Problem statement

Given a sorted array of NN integers A[]A[]. Find if there exists a triplet of integers A[i]A[i], A[j]A[j] and A[k]A[k] such that the sum is equal to given integer XX.

Input format

The first line of input consists of two space-separated integers N(1N105)N(1 \leq N \leq 10^{5}) and X(1X106)X(1 \leq X \leq 10^{6}) ...