The problem is taken from 15. 3Sum. Naive Approach Naive approach would be to go through all possible triplets in the array. And use set data structure to remove duplicates. Code - class Solution { public: vector<vector<int>> threeSum(vector<i...
The problem is taken from 118. Pascal's Triangle. Approach Let's make a pascal's triangle of size 5. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 We could see that the very first and the very last eleme...
The problem is taken from 88. Merge Sorted Array. Naive approach In this approach, we use a HashMap(Integer, Integer), where the first place would hold numbers in the array and second place would hold their occurrences. Code - class Solution { pu...
The problem is taken from 53. Maximum Subarray. Brute force approach For each element in the array we need to find all possible contiguous subarray to the right of it. Code - class Solution { public: int maxSubArray(vector<int>& nums) { ...
The problem is taken from 35. Search Insert Position. Approach The solution is very straight forward. We need to do binary search on the given array and try to find the target value. But the catch here is, if the target value is not found in the arr...
The problem is taken from 26. Remove Duplicates from Sorted Array - LeetCode. Approach The approach is very simple. Let's have two pointers pointing to the first two elements respectively(suppose nums.length >=2). There might be two possibilities - ...