From ecef580b8e88a4f8ff12daf268d276a521abc90d Mon Sep 17 00:00:00 2001 From: Durgesh Date: Fri, 22 Oct 2021 22:09:16 +0530 Subject: [PATCH] Solved 2 Questions from leetcode 1. 3Sum 2. Majority Element --- LeetCode/3Sum.cpp | 42 +++++++++++++++++++++++++++++++++++ LeetCode/Majority Element.cpp | 24 ++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 LeetCode/3Sum.cpp create mode 100644 LeetCode/Majority Element.cpp diff --git a/LeetCode/3Sum.cpp b/LeetCode/3Sum.cpp new file mode 100644 index 0000000..9b81dd4 --- /dev/null +++ b/LeetCode/3Sum.cpp @@ -0,0 +1,42 @@ +// i j k +// -2 -2 -1 -1 0 0 0 2 2 2 + +// i j k +// 1 -1 -1 0 +#include +using namespace std; +class Solution { + public: +vector> threeSum(vector& nums) { + vector>ans; + if(nums.size()<=2)return ans; + sort(nums.begin(), nums.end()); + for(int i=0;itemp){ + k--; + if(k>j && k>i)while(nums[k+1]==nums[k])k--; + } + else if(nums[j]+nums[k] +using namespace std; + +class Solution { +public: + int majorityElement(vector& nums) { + int element=nums[0]; + int freq=1; + for(int i=1;i