Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

3 Sum

Medium Acceptance 35.80% Points 30.00

You are given an array arr[] and an integer target, determine if there exists a triplet in the array whose sum equals the given target.

Return true if such a triplet exists, otherwise, return false.

Examples
Example 1

Example 1

Input: arr[] = [1, 4, 45, 6, 10, 8], target = 13

Output: true

Explanation: The triplet {1, 4, 8} sums up to 13

Example 2

Example 2

Input: arr[] = [1, 2, 4, 3, 6, 7], target = 10

Output: true

Explanation: The triplets {1, 3, 6} and {1, 2, 7} both sum to 10.

Hints
Hint 1
Expected Time Complexity: O(n^2)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 3 <= arr.size() <= 10^3
  • 1 <= arr[i] <= 10^5
Companies
Microsoft Apple Samsung Visa
Topics
Array Sorting Algorithm
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions