From 0919b89fc7ee97c40223c3f00bd81db88808c93c Mon Sep 17 00:00:00 2001 From: Adarsh jaiswal Date: Tue, 4 Oct 2022 17:02:02 +0530 Subject: [PATCH] Revert "Renamed files and added 5 problems (#370)" This reverts commit d838d019b7a350d630ebf5b697fb414c8ae7bdef. --- .DS_Store | Bin 8196 -> 0 bytes CPP/.DS_Store | Bin 10244 -> 0 bytes CPP/Problems/.DS_Store | Bin 14340 -> 0 bytes ....cpp => BinaryTreeLevelOrderTraversal.cpp} | 1 - CPP/Problems/BitonicArray.cpp | 156 +++++++++--------- ...ater.cpp => Container With Most Water.cpp} | 1 - ...elete-And-Earn.cpp => Delete_and_Earn.cpp} | 2 + ...st Position of Element in Sorted Array.cpp | 21 +++ ...e-1346-Check-If-N-and-Its-Double-Exist.cpp | 15 -- ...nt-Negative-Numbers-in-a-Sorted-Matrix.cpp | 17 -- .../Leetcode-1572-Matrix-Diagonal-Sum.cpp | 23 --- CPP/Problems/Leetcode-231-Power-of-Two.cpp | 7 - ...Leetcode-287-Find-the-Duplicate-Number.cpp | 30 ---- ...st-position-of-element-in-sorted-array.cpp | 44 +++-- CPP/Problems/Leetcode-43-Multiply-Strings.cpp | 8 - CPP/Problems/Leetcode-7-Reverse-Integer.cpp | 15 -- ...ontinuous-Increasing-Subsequence-Easy.cpp} | 1 - ...ubarray.cpp => MaximumProductSubarray.cpp} | 3 + ...ys.cpp => Median of Two Sorted Arrays.cpp} | 1 - ... Number of Dice Rolls With Target Sum.cpp} | 1 - ...drome-Number.cpp => Palindrome Number.cpp} | 1 - CPP/Problems/Reverse Integer.cpp | 17 ++ ...an-to-Integer.cpp => Roman to Integer.cpp} | 1 - ...atoi).cpp => String to Integer (atoi).cpp} | 1 - ...dd-Two-Numbers.cpp => add-two-numbers.cpp} | 1 - ...ll-Pairings.cpp => biwise_xor_pairing.cpp} | 1 - ...cpp => check-if-it-is-a-straight-line.cpp} | 0 CPP/Problems/findTheDuplicateNumber.cpp | 64 +++++++ ...imum-Subarray.cpp => maximum-subarray.cpp} | 0 ...ted-Arrays.cpp => merge-sorted-arrays.cpp} | 3 +- ...de-283-Move-Zeroes.cpp => move-zeroes.cpp} | 1 - ...{Leetcode-50-Pow(x,n).cpp => pow(x,n).cpp} | 1 - ...ries-on-number-of-points-inside-circle.cpp | 25 +++ .../{Leetcode-1-Two-Sum.cpp => two-sum.cpp} | 1 - ...ain-Array.cpp => valid-mountain-array.cpp} | 2 +- 35 files changed, 241 insertions(+), 224 deletions(-) delete mode 100644 .DS_Store delete mode 100644 CPP/.DS_Store delete mode 100644 CPP/Problems/.DS_Store rename CPP/Problems/{Leetcode-102-Binary-Tree-Level-Order-Traversal.cpp => BinaryTreeLevelOrderTraversal.cpp} (92%) rename CPP/Problems/{Leetcode-11-Container-With-Most-Water.cpp => Container With Most Water.cpp} (86%) rename CPP/Problems/{Leetcode-740-Delete-And-Earn.cpp => Delete_and_Earn.cpp} (90%) create mode 100644 CPP/Problems/Find First and Last Position of Element in Sorted Array.cpp delete mode 100644 CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp delete mode 100644 CPP/Problems/Leetcode-1351-Count-Negative-Numbers-in-a-Sorted-Matrix.cpp delete mode 100644 CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp delete mode 100644 CPP/Problems/Leetcode-231-Power-of-Two.cpp delete mode 100644 CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp delete mode 100644 CPP/Problems/Leetcode-43-Multiply-Strings.cpp delete mode 100644 CPP/Problems/Leetcode-7-Reverse-Integer.cpp rename CPP/Problems/{Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp => Longest-Continuous-Increasing-Subsequence-Easy.cpp} (82%) rename CPP/Problems/{Leetcode-152-Maximum-Product-Subarray.cpp => MaximumProductSubarray.cpp} (89%) rename CPP/Problems/{Leetcode-4-Median-of-Two-Sorted-Arrays.cpp => Median of Two Sorted Arrays.cpp} (93%) rename CPP/Problems/{Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp => Number of Dice Rolls With Target Sum.cpp} (90%) rename CPP/Problems/{Leetcode-9-Palindrome-Number.cpp => Palindrome Number.cpp} (89%) create mode 100644 CPP/Problems/Reverse Integer.cpp rename CPP/Problems/{Leetcode-13-Roman-to-Integer.cpp => Roman to Integer.cpp} (90%) rename CPP/Problems/{Leetcode-8-String-to-Integer-(atoi).cpp => String to Integer (atoi).cpp} (95%) rename CPP/Problems/{Leetcode-2-Add-Two-Numbers.cpp => add-two-numbers.cpp} (96%) rename CPP/Problems/{Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp => biwise_xor_pairing.cpp} (89%) rename CPP/Problems/{Leetcode-1232-Check-If-It-Is-A-Straight-Line.cpp => check-if-it-is-a-straight-line.cpp} (100%) create mode 100644 CPP/Problems/findTheDuplicateNumber.cpp rename CPP/Problems/{Leetcode-53-Maximum-Subarray.cpp => maximum-subarray.cpp} (100%) rename CPP/Problems/{Leetcode-88-Merge-Sorted-Arrays.cpp => merge-sorted-arrays.cpp} (89%) rename CPP/Problems/{Leetcode-283-Move-Zeroes.cpp => move-zeroes.cpp} (89%) rename CPP/Problems/{Leetcode-50-Pow(x,n).cpp => pow(x,n).cpp} (89%) create mode 100644 CPP/Problems/queries-on-number-of-points-inside-circle.cpp rename CPP/Problems/{Leetcode-1-Two-Sum.cpp => two-sum.cpp} (92%) rename CPP/Problems/{Leetcode-941-Valid-Mountain-Array.cpp => valid-mountain-array.cpp} (88%) diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index cdfa6b6a518464d25013af7b822bba9f20f19629..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeHMU279T6ur}?&4!|+_*D^M!Pi>T7D|dQX_^QXKWbC-K_w>HZ5q1S3E53UDTTbM z|G~fDqyNPJ;**{`GuGWCg+7VJ8JM}Vch0#x_w2BDqt1(FBRbT zgNw?xWb8;=_3A*QjsUP3xK%-4ef-0^?0_v9JJJ>&6rriGnkvi@LzuIJ&~%KK41T1o zrjt;2MjUfzVNNK*96f}q;3P`g+SV#y6_`|j&+fCdO}n(a6Qs`XTNqjSsfwZ*RC5^h z%qjm{vln!txKQ{mW@a;U^I0eBEI99LhjLhp8}T42H~Me+YG2Br`Mcj}zV7(Ldj96N z4C6*8^!tj?@q9quz3GIW9G2xE^diNr=>aF_E@4TpdhqD+Ui~QOhVrA%QkXXdUDMa+?I-k1GRL)k7|5^<52J5Wg=(}#52;T+9aD!Q zR7oK`Z5p8O!M{R1*n=^-zJi~W>jq+-fZ3ofcr9`vaUZeTXxYc!klN`e;{I5aEFCDE z2ezbW4E=cwouQ|QeN2rp+>bHbU3!K*e2OtIsd92@lh&z#Sw`>#$Th}&Ir9}@delot zoMFY1+1@*kC=)SP5p$i2myHVLiM}M3TozGxusEyq0?(zIo&&MsN%R~&NA@utPUys{ zCXw?{^F$$$VLGZ{O_SI>y;4uUMMubE&#VidhSS%9V6Bt<95_{ER>AD5;B?7L-%)9q zWGo`rU3!UUSy#L*dV&}`N`m{r?JPW~XcwunJsO0g1 diff --git a/CPP/.DS_Store b/CPP/.DS_Store deleted file mode 100644 index 27ac926a7532c159524038c8256358e1535e2f8b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10244 zcmeHML2nyH6n>MWjUA_n(Gn z|G)tDY%aM%B+b<@U>GnA{KEj}2Md*DIhIF})Tsj(eFT7t z$R-~@J9&PuyQ+?7yqw-bz4DhXXc3)G=m&h(Pw0M^(Ve0?ITTVGn1FWC_NbM{h!+Mh z4cgH*tYb?V%$FI=G`&p{WA|X80Xr6LAR^9QhOE*uwU%m@!XEE zC78=3xg$gHi9U! z5q#hs6f-F{*Dy~ZFm2SQ7@7=x`h5R*hp!Tx?`EO46|E2q6wSQW0uWISb7|6RY{3p(+7`@Y_Boggfgeimcn z6Q@p3<|cE6+@tD_>QzHK>;@Ivf53MeO1brZ+pgbpWUp47zoop;c0B28fYX#vd3fLP znyOb(U9TBvY&Ck2%jfd7;`xJv*Gkuyt!p<9m#u>vS4+#*_0qM&!+dUG>6JI`)^^>t zr#{Dvfg(gC=}av9;tMneXmA>PaaiKVeptU2i~YdzQ-$d>Gqc6Hv*+gK=N1+g7SAtS zxOnloTJc=nZtS*vcKDWZm0xkYa?@!juj)LNo1uIXa-&u}o6)-~n;#!{ZR~fTGk@Hn z$9l(?(tFVIo#xv1b|AyeT5&dL_{w$HRp5k<^0pFBw;l%IdNq{oRc~9t;vRTj>#$mI zPx=Am0Dg1J_I=xH$;$pC*RD%<&8w&|RBhJe(GbYGFL@+wsoJp{j@Mec`Fq6XQhB}K zcsXhmX(?)epIHi>(XE$pV=4V&jx6K1(EtJoKbafV`m$Nt`+{BoWhKLLyOf67k^K zsBMH(L=rcpEwTPfsGM9J^3&dNnoVJ5fN`PMj6B1qbKbl>+$-v3u$&f>_9lVlbi>>Y zY^a*Kr1m(pLvqt&YPOyw*y0#CF?Ydns97GKNRyZg8et>iP8`L3omhE$<8*wS=!R}{ zI@Kn}JEgY-y)mY})$AoTXZ5zho-z-Y;WNKO4a^&F3z>IGr`04n{lc0%!+>GHFkl!k z44fba#-muR0_5${|NoyLMog%Nfs@C8m{_TGu-;O#; diff --git a/CPP/Problems/.DS_Store b/CPP/Problems/.DS_Store deleted file mode 100644 index c0de1f5d0a243c15c919ad428fd3176efb56fc86..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14340 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 levelOrder(TreeNode* root) { diff --git a/CPP/Problems/BitonicArray.cpp b/CPP/Problems/BitonicArray.cpp index 1e3b4b6e..84c920aa 100644 --- a/CPP/Problems/BitonicArray.cpp +++ b/CPP/Problems/BitonicArray.cpp @@ -1,95 +1,95 @@ -// Bitonic array is an array having strictly increasing order upto an element and then having strictly -// decreasing order. It is also called Mountain array. + // Bitonic array is an array having strictly increasing order upto an element and then having strictly + // decreasing order. It is also called Mountain array. -// Program to find element in a bitonic array -// T.C.: O(log n) -// S.C.: O(1) -#include -#include -using namespace std; + // Program to find element in a bitonic array + // T.C.: O(log n) + // S.C.: O(1) + #include + #include + using namespace std; -int findPeakIndex (vector vect, int size) { // find index of peak element - int lo = 0, hi = size-1; - int mid = lo + (hi - lo)/2; + int findPeakIndex (vector vect, int size) { // find index of peak element + int lo = 0, hi = size-1; + int mid = lo + (hi - lo)/2; - while (lo < hi) { - if (mid == size-1 || vect[mid] > vect[mid+1]) { - hi = mid; + while (lo < hi) { + if (mid == size-1 || vect[mid] > vect[mid+1]) { + hi = mid; + } + else if (mid == 0 || vect[mid] > vect[mid-1]) { + lo = mid+1; + } + mid = lo + (hi - lo)/2; } - else if (mid == 0 || vect[mid] > vect[mid-1]) { - lo = mid+1; - } - mid = lo + (hi - lo)/2; + return lo; } - return lo; -} -int leftPart (vector vect, int target, int hi) { // search element in left part - int lo = 0; - int mid = lo + (hi - lo)/2; - while (lo <= hi) { - if (vect[mid] == target) { - return mid; - } - else if (vect[mid] > target) { - hi = mid - 1; + int leftPart (vector vect, int target, int hi) { // search element in left part + int lo = 0; + int mid = lo + (hi - lo)/2; + while (lo <= hi) { + if (vect[mid] == target) { + return mid; + } + else if (vect[mid] > target) { + hi = mid - 1; + } + else { + lo = mid + 1; + } + mid = lo + (hi - lo)/2; } - else { - lo = mid + 1; - } - mid = lo + (hi - lo)/2; + return -1; } - return -1; -} -int rightPart (vector vect, int target, int lo) { // search element in right part - int hi = vect.size()-1; - int mid = lo + (hi - lo)/2; - while (lo <= hi) { - if (vect[mid] == target) { - return mid; - } - else if (vect[mid] < target) { - hi = mid - 1; + int rightPart (vector vect, int target, int lo) { // search element in right part + int hi = vect.size()-1; + int mid = lo + (hi - lo)/2; + while (lo <= hi) { + if (vect[mid] == target) { + return mid; + } + else if (vect[mid] < target) { + hi = mid - 1; + } + else { + lo = mid + 1; + } + mid = lo + (hi - lo)/2; } - else { - lo = mid + 1; - } - mid = lo + (hi - lo)/2; + return -1; } - return -1; -} -int findElement(vector vect, int size, int target) { - // DRIVER CODE - // find peak of array - int peak = findPeakIndex (vect, size); - // compare peak element with target - if (target > vect[peak]) {return -1;} - else if (target == vect[peak]) {return peak;} - // find element in increasing/decreasing sequence - else { - int leftIndex = leftPart(vect, target, peak); - if (leftIndex != -1) { - return leftIndex; - } + int findElement(vector vect, int size, int target) { + // DRIVER CODE + // find peak of array + int peak = findPeakIndex (vect, size); + // compare peak element with target + if (target > vect[peak]) {return -1;} + else if (target == vect[peak]) {return peak;} + // find element in increasing/decreasing sequence else { - return rightPart(vect, target, peak); + int leftIndex = leftPart(vect, target, peak); + if (leftIndex != -1) { + return leftIndex; + } + else { + return rightPart(vect, target, peak); + } } + return 0; } - return 0; -} -int main () { - vector numvect; - int num, target; - cin>>num; - cin>>target; - for (int i = 0; i < num; i++) { - int temp; - cin>>temp; - numvect.push_back(temp); + int main () { + vector numvect; + int num, target; + cin>>num; + cin>>target; + for (int i = 0; i < num; i++) { + int temp; + cin>>temp; + numvect.push_back(temp); + } + cout<& height) { diff --git a/CPP/Problems/Leetcode-740-Delete-And-Earn.cpp b/CPP/Problems/Delete_and_Earn.cpp similarity index 90% rename from CPP/Problems/Leetcode-740-Delete-And-Earn.cpp rename to CPP/Problems/Delete_and_Earn.cpp index e6b523f5..96b1d5c7 100644 --- a/CPP/Problems/Leetcode-740-Delete-And-Earn.cpp +++ b/CPP/Problems/Delete_and_Earn.cpp @@ -1,4 +1,6 @@ +// Question Link // https://leetcode.com/problems/delete-and-earn/ +// ---------------------------------------------- class Solution { public: int deleteAndEarn(vector& nums) { diff --git a/CPP/Problems/Find First and Last Position of Element in Sorted Array.cpp b/CPP/Problems/Find First and Last Position of Element in Sorted Array.cpp new file mode 100644 index 00000000..8d00eff3 --- /dev/null +++ b/CPP/Problems/Find First and Last Position of Element in Sorted Array.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector searchRange(vector& nums, int target) { + vectorans; + auto low=lower_bound(nums.begin(),nums.end(),target); + auto upp=upper_bound(nums.begin(),nums.end(),target); + if(low==nums.end()|| *low!=target ) + { + ans.push_back(-1); + ans.push_back(-1); + } + else + { + int start=low-nums.begin(); + int end=upp-nums.begin()-1; + ans.push_back(start); + ans.push_back(end); + } + return ans; + } +}; diff --git a/CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp b/CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp deleted file mode 100644 index de7313bd..00000000 --- a/CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// https://leetcode.com/problems/check-if-n-and-its-double-exist/ -class Solution { -public: - bool checkIfExist(vector& arr) { - for (int i = 0; i < arr.size(); i++) { - for (int j = 0; j < arr.size(); j++) { - if (i!=j) { - if (arr[i] == 2*arr[j]) - return true; - } - } - } - return false; - } -}; \ No newline at end of file diff --git a/CPP/Problems/Leetcode-1351-Count-Negative-Numbers-in-a-Sorted-Matrix.cpp b/CPP/Problems/Leetcode-1351-Count-Negative-Numbers-in-a-Sorted-Matrix.cpp deleted file mode 100644 index 4054ec1b..00000000 --- a/CPP/Problems/Leetcode-1351-Count-Negative-Numbers-in-a-Sorted-Matrix.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/ -class Solution { -public: - int countNegatives(vector>& grid) { - int count = 0; - - for (int row = 0; row < grid.size(); row++) { - for (int col = grid[row].size()-1; col >= 0; col--) { - if (grid[row][col] < 0) - count++; - else - break; - } - } - return count; - } -}; \ No newline at end of file diff --git a/CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp b/CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp deleted file mode 100644 index de887a0d..00000000 --- a/CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// https://leetcode.com/problems/matrix-diagonal-sum/ -class Solution { -public: - int diagonalSum(vector>& mat) { - int rc = 0; - int TotalSum = 0; - // Primary Diagonal - while (rc < mat.size()) { - TotalSum += mat[rc][rc]; - rc++; - } - // Secondary Diagonal - int r = mat.size()-1, c = 0; - while (r >= 0 && c < mat.size()) { - TotalSum += mat[r][c]; - r--; - c++; - } - if (mat.size() & 1) - return TotalSum - (mat[mat.size()/2][mat.size()/2]); - return TotalSum; - } -}; \ No newline at end of file diff --git a/CPP/Problems/Leetcode-231-Power-of-Two.cpp b/CPP/Problems/Leetcode-231-Power-of-Two.cpp deleted file mode 100644 index 7a074138..00000000 --- a/CPP/Problems/Leetcode-231-Power-of-Two.cpp +++ /dev/null @@ -1,7 +0,0 @@ -// https://leetcode.com/problems/power-of-two/ -class Solution { -public: - bool isPowerOfTwo(int x) { - return x && (!(x&(x-1))); - } -}; \ No newline at end of file diff --git a/CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp b/CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp deleted file mode 100644 index b6b30564..00000000 --- a/CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp +++ /dev/null @@ -1,30 +0,0 @@ -// https://leetcode.com/problems/find-the-duplicate-number/ -class Solution { -public: - int findDuplicate(vector& nums) { - - // first thing that comes to my mind is using the set - with O(N) space - // using two for loop (N^2) - // using another array of same size and updating the value if every times - //element is there - int slow=0; - int fast=0; - - do { - slow=nums[slow]; - fast=nums[nums[fast]]; - - }while (slow !=fast); - - int slow2=0; - - while (slow2!=slow){ - slow2=nums[slow2]; - slow=nums[slow]; - } - - return slow; - - } -}; - diff --git a/CPP/Problems/Leetcode-34-find-first-and-last-position-of-element-in-sorted-array.cpp b/CPP/Problems/Leetcode-34-find-first-and-last-position-of-element-in-sorted-array.cpp index a98ff0cd..6ebcb4e2 100644 --- a/CPP/Problems/Leetcode-34-find-first-and-last-position-of-element-in-sorted-array.cpp +++ b/CPP/Problems/Leetcode-34-find-first-and-last-position-of-element-in-sorted-array.cpp @@ -1,22 +1,36 @@ -// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ class Solution { public: vector searchRange(vector& nums, int target) { - vectorans; - auto low=lower_bound(nums.begin(),nums.end(),target); - auto upp=upper_bound(nums.begin(),nums.end(),target); - if(low==nums.end()|| *low!=target ) - { - ans.push_back(-1); - ans.push_back(-1); + int n=nums.size(); + int l=0,r=n-1; + int p1=-1,p2=-1; + while(l<=r){ + int mid=l+(r-l)/2; + if(nums[mid]==target){ + p1=mid; + r=mid-1; + } + else if(nums[mid]>target){ + r=mid-1; + } + else + l=mid+1; } - else - { - int start=low-nums.begin(); - int end=upp-nums.begin()-1; - ans.push_back(start); - ans.push_back(end); + l=0,r=n-1; + while(l<=r){ + int mid=l+(r-l)/2; + if(nums[mid]>=target+1){ + r=mid-1; + } + else{ + + l=mid+1; + } + } - return ans; + p2=l; + if(p1!=-1) + return {p1,p2-1}; + return {-1,-1}; } }; diff --git a/CPP/Problems/Leetcode-43-Multiply-Strings.cpp b/CPP/Problems/Leetcode-43-Multiply-Strings.cpp deleted file mode 100644 index 6b572c10..00000000 --- a/CPP/Problems/Leetcode-43-Multiply-Strings.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// https://leetcode.com/problems/multiply-strings/ -class Solution { -public: - string multiply(string num1, string num2) { - int n1 = stoi(num1), n2 = stoi(num2); - return to_string(n1*n2); - } -}; \ No newline at end of file diff --git a/CPP/Problems/Leetcode-7-Reverse-Integer.cpp b/CPP/Problems/Leetcode-7-Reverse-Integer.cpp deleted file mode 100644 index e9dfd061..00000000 --- a/CPP/Problems/Leetcode-7-Reverse-Integer.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// https://leetcode.com/problems/reverse-integer/ -class Solution { -public: - int reverse(int x) { - int reverse =0; - while (x!=0) - { - if(214748364 reverse) - return 0; - reverse= (reverse * 10) + x%10; - x/=10; - } - return reverse; - } -}; diff --git a/CPP/Problems/Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp b/CPP/Problems/Longest-Continuous-Increasing-Subsequence-Easy.cpp similarity index 82% rename from CPP/Problems/Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp rename to CPP/Problems/Longest-Continuous-Increasing-Subsequence-Easy.cpp index 6676d1eb..755ca9e8 100644 --- a/CPP/Problems/Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp +++ b/CPP/Problems/Longest-Continuous-Increasing-Subsequence-Easy.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/longest-continuous-increasing-subsequence/ class Solution { public: int findLengthOfLCIS(vector& nums) { diff --git a/CPP/Problems/Leetcode-152-Maximum-Product-Subarray.cpp b/CPP/Problems/MaximumProductSubarray.cpp similarity index 89% rename from CPP/Problems/Leetcode-152-Maximum-Product-Subarray.cpp rename to CPP/Problems/MaximumProductSubarray.cpp index c3f97ea9..5a5c56a9 100644 --- a/CPP/Problems/Leetcode-152-Maximum-Product-Subarray.cpp +++ b/CPP/Problems/MaximumProductSubarray.cpp @@ -1,4 +1,7 @@ +// Question Link // https://leetcode.com/problems/maximum-product-subarray/ +// ---------------------------------------------- + class Solution { public: int maxProduct(vector& nums) { diff --git a/CPP/Problems/Leetcode-4-Median-of-Two-Sorted-Arrays.cpp b/CPP/Problems/Median of Two Sorted Arrays.cpp similarity index 93% rename from CPP/Problems/Leetcode-4-Median-of-Two-Sorted-Arrays.cpp rename to CPP/Problems/Median of Two Sorted Arrays.cpp index b83e8dcf..fb2772ea 100644 --- a/CPP/Problems/Leetcode-4-Median-of-Two-Sorted-Arrays.cpp +++ b/CPP/Problems/Median of Two Sorted Arrays.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/median-of-two-sorted-arrays/ class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { diff --git a/CPP/Problems/Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp b/CPP/Problems/Number of Dice Rolls With Target Sum.cpp similarity index 90% rename from CPP/Problems/Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp rename to CPP/Problems/Number of Dice Rolls With Target Sum.cpp index 45527c8a..fc091137 100644 --- a/CPP/Problems/Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp +++ b/CPP/Problems/Number of Dice Rolls With Target Sum.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/ class Solution { public: diff --git a/CPP/Problems/Leetcode-9-Palindrome-Number.cpp b/CPP/Problems/Palindrome Number.cpp similarity index 89% rename from CPP/Problems/Leetcode-9-Palindrome-Number.cpp rename to CPP/Problems/Palindrome Number.cpp index 27605362..6b4ba480 100644 --- a/CPP/Problems/Leetcode-9-Palindrome-Number.cpp +++ b/CPP/Problems/Palindrome Number.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/palindrome-number/ class Solution { public: bool isPalindrome(int x) { diff --git a/CPP/Problems/Reverse Integer.cpp b/CPP/Problems/Reverse Integer.cpp new file mode 100644 index 00000000..f390d5de --- /dev/null +++ b/CPP/Problems/Reverse Integer.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int reverse(int x) { +int reverse =0; +while (x!=0) +{ +if(214748364 reverse) +return 0; +reverse= (reverse * 10) + x%10; +x/=10; +} +return reverse; +} + + + +}; diff --git a/CPP/Problems/Leetcode-13-Roman-to-Integer.cpp b/CPP/Problems/Roman to Integer.cpp similarity index 90% rename from CPP/Problems/Leetcode-13-Roman-to-Integer.cpp rename to CPP/Problems/Roman to Integer.cpp index d9d179f7..bb2a56ad 100644 --- a/CPP/Problems/Leetcode-13-Roman-to-Integer.cpp +++ b/CPP/Problems/Roman to Integer.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/roman-to-integer/ class Solution { public: int romanToInt(string s) { diff --git a/CPP/Problems/Leetcode-8-String-to-Integer-(atoi).cpp b/CPP/Problems/String to Integer (atoi).cpp similarity index 95% rename from CPP/Problems/Leetcode-8-String-to-Integer-(atoi).cpp rename to CPP/Problems/String to Integer (atoi).cpp index daf1e6a1..53cd345d 100644 --- a/CPP/Problems/Leetcode-8-String-to-Integer-(atoi).cpp +++ b/CPP/Problems/String to Integer (atoi).cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/string-to-integer-atoi/ class Solution { public: int myAtoi(string s) { diff --git a/CPP/Problems/Leetcode-2-Add-Two-Numbers.cpp b/CPP/Problems/add-two-numbers.cpp similarity index 96% rename from CPP/Problems/Leetcode-2-Add-Two-Numbers.cpp rename to CPP/Problems/add-two-numbers.cpp index 80c433a9..af90a79c 100644 --- a/CPP/Problems/Leetcode-2-Add-Two-Numbers.cpp +++ b/CPP/Problems/add-two-numbers.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/add-two-numbers/ /** * Definition for singly-linked list. * struct ListNode { diff --git a/CPP/Problems/Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp b/CPP/Problems/biwise_xor_pairing.cpp similarity index 89% rename from CPP/Problems/Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp rename to CPP/Problems/biwise_xor_pairing.cpp index 3bcb7b30..c15876e5 100644 --- a/CPP/Problems/Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp +++ b/CPP/Problems/biwise_xor_pairing.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/bitwise-xor-of-all-pairings/ class Solution { public: int xorAllNums(vector& nums1, vector& nums2) { diff --git a/CPP/Problems/Leetcode-1232-Check-If-It-Is-A-Straight-Line.cpp b/CPP/Problems/check-if-it-is-a-straight-line.cpp similarity index 100% rename from CPP/Problems/Leetcode-1232-Check-If-It-Is-A-Straight-Line.cpp rename to CPP/Problems/check-if-it-is-a-straight-line.cpp diff --git a/CPP/Problems/findTheDuplicateNumber.cpp b/CPP/Problems/findTheDuplicateNumber.cpp new file mode 100644 index 00000000..e37f2d7c --- /dev/null +++ b/CPP/Problems/findTheDuplicateNumber.cpp @@ -0,0 +1,64 @@ +/* Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. + +There is only one repeated number in nums, return this repeated number. + +You must solve the problem without modifying the array nums and uses only constant extra space. + + + +Example 1: + +Input: nums = [1,3,4,2,2] +Output: 2 +Example 2: + +Input: nums = [3,1,3,4,2] +Output: 3 + + +Constraints: + +1 <= n <= 105 +nums.length == n + 1 +1 <= nums[i] <= n +All the integers in nums appear only once except for precisely one integer which appears two or more times. + + +Follow up: + +How can we prove that at least one duplicate number must exist in nums? +Can you solve the problem in linear runtime complexity? + + + +Solution:*/ + +class Solution { +public: + int findDuplicate(vector& nums) { + + // first thing that comes to my mind is using the set - with O(N) space + // using two for loop (N^2) + // using another array of same size and updating the value if every times + //element is there + int slow=0; + int fast=0; + + do { + slow=nums[slow]; + fast=nums[nums[fast]]; + + }while (slow !=fast); + + int slow2=0; + + while (slow2!=slow){ + slow2=nums[slow2]; + slow=nums[slow]; + } + + return slow; + + } +}; + diff --git a/CPP/Problems/Leetcode-53-Maximum-Subarray.cpp b/CPP/Problems/maximum-subarray.cpp similarity index 100% rename from CPP/Problems/Leetcode-53-Maximum-Subarray.cpp rename to CPP/Problems/maximum-subarray.cpp diff --git a/CPP/Problems/Leetcode-88-Merge-Sorted-Arrays.cpp b/CPP/Problems/merge-sorted-arrays.cpp similarity index 89% rename from CPP/Problems/Leetcode-88-Merge-Sorted-Arrays.cpp rename to CPP/Problems/merge-sorted-arrays.cpp index 1f8b1778..584fb99c 100644 --- a/CPP/Problems/Leetcode-88-Merge-Sorted-Arrays.cpp +++ b/CPP/Problems/merge-sorted-arrays.cpp @@ -1,8 +1,7 @@ -// https://leetcode.com/problems/merge-sorted-array/ class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) { - int h=m; + int h=m; int j=(m+n)-1; int i=0; int k=0; diff --git a/CPP/Problems/Leetcode-283-Move-Zeroes.cpp b/CPP/Problems/move-zeroes.cpp similarity index 89% rename from CPP/Problems/Leetcode-283-Move-Zeroes.cpp rename to CPP/Problems/move-zeroes.cpp index e4722a2f..68e67964 100644 --- a/CPP/Problems/Leetcode-283-Move-Zeroes.cpp +++ b/CPP/Problems/move-zeroes.cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/move-zeroes/ class Solution { public: void moveZeroes(vector& nums) { diff --git a/CPP/Problems/Leetcode-50-Pow(x,n).cpp b/CPP/Problems/pow(x,n).cpp similarity index 89% rename from CPP/Problems/Leetcode-50-Pow(x,n).cpp rename to CPP/Problems/pow(x,n).cpp index 0b497a84..c844629b 100644 --- a/CPP/Problems/Leetcode-50-Pow(x,n).cpp +++ b/CPP/Problems/pow(x,n).cpp @@ -1,4 +1,3 @@ -// https://leetcode.com/problems/powx-n/ class Solution { public: //binary exponentiation diff --git a/CPP/Problems/queries-on-number-of-points-inside-circle.cpp b/CPP/Problems/queries-on-number-of-points-inside-circle.cpp new file mode 100644 index 00000000..78a3fda4 --- /dev/null +++ b/CPP/Problems/queries-on-number-of-points-inside-circle.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + bool checkStraightLine(vector>& coordinates) { + bool flag=true; + + int x1=coordinates[0][0]; + int y1=coordinates[0][1]; + int x2=coordinates[1][0]; + int y2=coordinates[1][1]; + int m1=y2-y1; + int m2=x2-x1; + int curdx=0; + int curdy=0; + for(int i=2;i twoSum(vector& nums, int target) { diff --git a/CPP/Problems/Leetcode-941-Valid-Mountain-Array.cpp b/CPP/Problems/valid-mountain-array.cpp similarity index 88% rename from CPP/Problems/Leetcode-941-Valid-Mountain-Array.cpp rename to CPP/Problems/valid-mountain-array.cpp index 301f38d6..936c9fb5 100644 --- a/CPP/Problems/Leetcode-941-Valid-Mountain-Array.cpp +++ b/CPP/Problems/valid-mountain-array.cpp @@ -1,4 +1,4 @@ -// https://leetcode.com/problems/valid-mountain-array/ + class Solution { public: bool validMountainArray(vector& A) {