#K55057. Triplet Sum Problem
Triplet Sum Problem
Triplet Sum Problem
You are given an array of N integers and an integer target value. Your task is to determine whether there exists a triplet of numbers in the array which adds up exactly to the target value.
You should design an efficient algorithm with a time complexity better than O(N3). A common approach is to first sort the array and then use the two-pointer technique for each element, which gives a time complexity of O(N2).
The problem includes negative numbers and zeros, so be cautious when handling these cases. The input is read from stdin and the answer should be output to stdout.
If a triplet exists whose sum equals the target, print True
; otherwise, print False
.
inputFormat
The input will be given in three lines:
- The first line contains an integer N, the size of the array.
- The second line contains N space-separated integers representing the elements of the array.
- The third line contains an integer representing the target sum.
You should read the input from stdin.
outputFormat
Output a single line containing either True
if there exists a triplet of numbers in the array that sum up to the target; otherwise, output False
.
The output must be written to stdout.
## sample6
12 3 4 1 6 9
24
True