
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
3Sum Smaller in C++
The 3Sum Smaller is one of the problem variations in which we need to find the number of triplets in an array such that their sum is less than a given target.
We are given an array of n integers called nums and a target value, and we have to find the number of index triplets (i, j, k) where i, j, k are indices of an array all in range 0 to n - 1, such that they satisfy the condition:
nums[i] + nums[j] + nums[k] < target.
Scenario 1
Input: arr= [-2,0,1,3], target= 2 Output: 2 Explanation In this, the value of the given target is 2; therefore, we can find that two possible triplets from a given array, whose sum is less than 2. That's [-2,0,1] and [-2,0,3].
Scenario 2
Input: [-1, 0, 1, 2, -1, -4], target= 1 Output: 7 Explanation In this, the value of the given target is 1; therefore, we can find that there are seven possible triplets from a given array, whose sum is less than 1. That's [-4, -1, -1], [-4, -1, 0], [-4, -1, 1], [-4, -1, 2], [-4, 0, 1], [-4, 0, 2], [-4, 1, 2].
In this article, we will learn two methods to solve the given problem of 3Sum Smaller in C++.
Finding 3Sum Smaller Using Nested for Loop
When a loop is created inside another loop, forming multiple levels of loops is said to be a nested loop.
In this, we will use three nested for loops to fix each element of the triplet one by one, and will check all possible combinations, and for every combination we will calculate the sum and compare it with a given target.
Algorithm
- First, initialize an integer named ans and set it to 0.
- Fix the first element using a for loop with index i from 0 to n-3.
- Now, inside that loop, fix the second element using another loop with index j from i + 1 to n - 2.
- Then, use a third loop to fix the third element with index k from j + 1 to n - 1.
- For each triplet (arr[i], arr[j], arr[k]), check if the sum is less than the given target.
- If true, increment the value of ans by 1.
- Return the final value of ans.
Example
Here is the following example code in C++ to check for triplets using a nested for loop.
#include <iostream> using namespace std; int find_triplets(int arr[], int n, int sum) { int ans = 0; // fix the first element as a[i] for (int i = 0; i < n - 2; i++) { // fix the second element as a[j] for (int j = i + 1; j < n - 1; j++) { // looking for the third element for (int k = j + 1; k < n; k++) if (arr[i] + arr[j] + arr[k] < sum) ans++; } } return ans; } int main() { int arr[] = { -1, 0, 1, 2, -1, -4 }; int n = sizeof arr / sizeof arr[0]; int sum = 0; cout << find_triplets(arr, n, sum) << endl; return 0; }
Output
12
Time complexity = O(n^3), as there are three nested loops.
Space complexity = O(1)
Finding 3Sum Smaller Using Two-Pointer
The two-pointer is a technique that uses two pointers or indices to traverse through a sorted array to find or count pairs, triplets, or other combinations.
Here is the following algorithm we will follow to solve our given problem.
- First, sort the given array.
- Fix the first element of the triplet at index i and iterate i from 0 to n-2 (as we need two more elements after i).
- Use two pointers (left and right) to find the other two elements.
- While left < right, perform sum = nums[i] + nums[left] + nums[right].
- If sum < target, then all pairs between left and right, including nums[i], will form valid triplets, because the array is sorted and increasing right will only increase the sum.
- So, add (right-left) to the count and do left++ to find more possible combinations, and if sum >= target then decrement right--.
Example
Here is the following example code in C++.
#include <bits/stdc++.h> using namespace std; class Solution { public: int threeSumSmaller(vector<int>& a, int t) { int ret = 0; sort(a.begin(), a.end()); int n = a.size(); // fix the first element of the triplet for (int i = 0; i < n - 2; i++) { int left = i + 1; // second pointer just after i int right = n - 1; // third pointer starting from end while (left < right) { int sum = a[i] + a[left] + a[right]; if (sum < t) { ret += right - left; left++; } else right--; } } return ret; } }; main(){ Solution ob; vector<int> v = { -2, 0, 1, 3 }; cout << (ob.threeSumSmaller(v,2)); return 0; }
Output
2
Time complexity = O(n^2)
Sorting the array will take O(n log n), and two nested loops (for and while) will take O(n^2). Therefore, adding both, we will get O(n^2).
Space Complexity = O(n)