From dec72be566be46cf1770e064df71e61de07846ed Mon Sep 17 00:00:00 2001 From: zahyaah Date: Tue, 4 Oct 2022 15:14:48 +0530 Subject: [PATCH] Renamed files and added 5 problems --- .DS_Store | Bin 0 -> 8196 bytes CPP/.DS_Store | Bin 0 -> 10244 bytes CPP/Problems/.DS_Store | Bin 0 -> 14340 bytes CPP/Problems/BitonicArray.cpp | 156 +++++++++--------- ...st Position of Element in Sorted Array.cpp | 21 --- .../{two-sum.cpp => Leetcode-1-Two-Sum.cpp} | 1 + ...102-Binary-Tree-Level-Order-Traversal.cpp} | 1 + ...Leetcode-11-Container-With-Most-Water.cpp} | 1 + ...5-Number-of-dice-rolls-with-targe-sum.cpp} | 1 + ...e-1232-Check-If-It-Is-A-Straight-Line.cpp} | 0 ...r.cpp => Leetcode-13-Roman-to-Integer.cpp} | 1 + ...e-1346-Check-If-N-and-Its-Double-Exist.cpp | 15 ++ ...nt-Negative-Numbers-in-a-Sorted-Matrix.cpp | 17 ++ ...Leetcode-152-Maximum-Product-Subarray.cpp} | 3 - .../Leetcode-1572-Matrix-Diagonal-Sum.cpp | 23 +++ ...ers.cpp => Leetcode-2-Add-Two-Numbers.cpp} | 1 + CPP/Problems/Leetcode-231-Power-of-Two.cpp | 7 + ...code-2425-Bitwise-XOR-Of-All-Pairings.cpp} | 1 + ...eroes.cpp => Leetcode-283-Move-Zeroes.cpp} | 1 + ...Leetcode-287-Find-the-Duplicate-Number.cpp | 30 ++++ ...st-position-of-element-in-sorted-array.cpp | 44 ++--- ...eetcode-4-Median-of-Two-Sorted-Arrays.cpp} | 1 + CPP/Problems/Leetcode-43-Multiply-Strings.cpp | 8 + ...{pow(x,n).cpp => Leetcode-50-Pow(x,n).cpp} | 1 + ...y.cpp => Leetcode-53-Maximum-Subarray.cpp} | 0 ...ontinuous-Increasing-Subsequence-Easy.cpp} | 1 + CPP/Problems/Leetcode-7-Reverse-Integer.cpp | 15 ++ ...n.cpp => Leetcode-740-Delete-And-Earn.cpp} | 2 - ...> Leetcode-8-String-to-Integer-(atoi).cpp} | 1 + ...pp => Leetcode-88-Merge-Sorted-Arrays.cpp} | 3 +- ...r.cpp => Leetcode-9-Palindrome-Number.cpp} | 1 + ... => Leetcode-941-Valid-Mountain-Array.cpp} | 2 +- CPP/Problems/Reverse Integer.cpp | 17 -- CPP/Problems/findTheDuplicateNumber.cpp | 64 ------- ...ries-on-number-of-points-inside-circle.cpp | 25 --- 35 files changed, 224 insertions(+), 241 deletions(-) create mode 100644 .DS_Store create mode 100644 CPP/.DS_Store create mode 100644 CPP/Problems/.DS_Store delete mode 100644 CPP/Problems/Find First and Last Position of Element in Sorted Array.cpp rename CPP/Problems/{two-sum.cpp => Leetcode-1-Two-Sum.cpp} (92%) rename CPP/Problems/{BinaryTreeLevelOrderTraversal.cpp => Leetcode-102-Binary-Tree-Level-Order-Traversal.cpp} (92%) rename CPP/Problems/{Container With Most Water.cpp => Leetcode-11-Container-With-Most-Water.cpp} (86%) rename CPP/Problems/{Number of Dice Rolls With Target Sum.cpp => Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp} (90%) rename CPP/Problems/{check-if-it-is-a-straight-line.cpp => Leetcode-1232-Check-If-It-Is-A-Straight-Line.cpp} (100%) rename CPP/Problems/{Roman to Integer.cpp => Leetcode-13-Roman-to-Integer.cpp} (90%) create mode 100644 CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp create mode 100644 CPP/Problems/Leetcode-1351-Count-Negative-Numbers-in-a-Sorted-Matrix.cpp rename CPP/Problems/{MaximumProductSubarray.cpp => Leetcode-152-Maximum-Product-Subarray.cpp} (89%) create mode 100644 CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp rename CPP/Problems/{add-two-numbers.cpp => Leetcode-2-Add-Two-Numbers.cpp} (96%) create mode 100644 CPP/Problems/Leetcode-231-Power-of-Two.cpp rename CPP/Problems/{biwise_xor_pairing.cpp => Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp} (89%) rename CPP/Problems/{move-zeroes.cpp => Leetcode-283-Move-Zeroes.cpp} (89%) create mode 100644 CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp rename CPP/Problems/{Median of Two Sorted Arrays.cpp => Leetcode-4-Median-of-Two-Sorted-Arrays.cpp} (93%) create mode 100644 CPP/Problems/Leetcode-43-Multiply-Strings.cpp rename CPP/Problems/{pow(x,n).cpp => Leetcode-50-Pow(x,n).cpp} (89%) rename CPP/Problems/{maximum-subarray.cpp => Leetcode-53-Maximum-Subarray.cpp} (100%) rename CPP/Problems/{Longest-Continuous-Increasing-Subsequence-Easy.cpp => Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp} (82%) create mode 100644 CPP/Problems/Leetcode-7-Reverse-Integer.cpp rename CPP/Problems/{Delete_and_Earn.cpp => Leetcode-740-Delete-And-Earn.cpp} (90%) rename CPP/Problems/{String to Integer (atoi).cpp => Leetcode-8-String-to-Integer-(atoi).cpp} (95%) rename CPP/Problems/{merge-sorted-arrays.cpp => Leetcode-88-Merge-Sorted-Arrays.cpp} (89%) rename CPP/Problems/{Palindrome Number.cpp => Leetcode-9-Palindrome-Number.cpp} (89%) rename CPP/Problems/{valid-mountain-array.cpp => Leetcode-941-Valid-Mountain-Array.cpp} (88%) delete mode 100644 CPP/Problems/Reverse Integer.cpp delete mode 100644 CPP/Problems/findTheDuplicateNumber.cpp delete mode 100644 CPP/Problems/queries-on-number-of-points-inside-circle.cpp diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..cdfa6b6a518464d25013af7b822bba9f20f19629 GIT binary patch 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 literal 0 HcmV?d00001 diff --git a/CPP/.DS_Store b/CPP/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..27ac926a7532c159524038c8256358e1535e2f8b GIT binary patch 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#; literal 0 HcmV?d00001 diff --git a/CPP/Problems/.DS_Store b/CPP/Problems/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c0de1f5d0a243c15c919ad428fd3176efb56fc86 GIT binary patch literal 14340 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 - #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; - } - else if (mid == 0 || vect[mid] > vect[mid-1]) { - lo = mid+1; - } - mid = lo + (hi - lo)/2; + while (lo < hi) { + if (mid == size-1 || vect[mid] > vect[mid+1]) { + hi = mid; } - return lo; + else if (mid == 0 || vect[mid] > vect[mid-1]) { + lo = mid+1; + } + mid = lo + (hi - lo)/2; } + 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; - } - else { - lo = mid + 1; - } - mid = lo + (hi - lo)/2; +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; } - return -1; + else { + lo = mid + 1; + } + mid = lo + (hi - lo)/2; } + 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; - } - else { - lo = mid + 1; - } - mid = lo + (hi - lo)/2; +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; } - return -1; + else { + lo = mid + 1; + } + mid = lo + (hi - lo)/2; } + 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 +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; + } else { - int leftIndex = leftPart(vect, target, peak); - if (leftIndex != -1) { - return leftIndex; - } - else { - return rightPart(vect, target, peak); - } + 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); - } - cout< numvect; + int num, target; + cin>>num; + cin>>target; + for (int i = 0; i < num; i++) { + int temp; + cin>>temp; + numvect.push_back(temp); } + cout< 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/two-sum.cpp b/CPP/Problems/Leetcode-1-Two-Sum.cpp similarity index 92% rename from CPP/Problems/two-sum.cpp rename to CPP/Problems/Leetcode-1-Two-Sum.cpp index da08263f..a3952559 100644 --- a/CPP/Problems/two-sum.cpp +++ b/CPP/Problems/Leetcode-1-Two-Sum.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/two-sum/ class Solution { public: vector twoSum(vector& nums, int target) { diff --git a/CPP/Problems/BinaryTreeLevelOrderTraversal.cpp b/CPP/Problems/Leetcode-102-Binary-Tree-Level-Order-Traversal.cpp similarity index 92% rename from CPP/Problems/BinaryTreeLevelOrderTraversal.cpp rename to CPP/Problems/Leetcode-102-Binary-Tree-Level-Order-Traversal.cpp index 1101222c..6a21ccea 100644 --- a/CPP/Problems/BinaryTreeLevelOrderTraversal.cpp +++ b/CPP/Problems/Leetcode-102-Binary-Tree-Level-Order-Traversal.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/binary-tree-level-order-traversal/ class Solution { public: vector> levelOrder(TreeNode* root) { diff --git a/CPP/Problems/Container With Most Water.cpp b/CPP/Problems/Leetcode-11-Container-With-Most-Water.cpp similarity index 86% rename from CPP/Problems/Container With Most Water.cpp rename to CPP/Problems/Leetcode-11-Container-With-Most-Water.cpp index 497442b4..6d516141 100644 --- a/CPP/Problems/Container With Most Water.cpp +++ b/CPP/Problems/Leetcode-11-Container-With-Most-Water.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/container-with-most-water/ class Solution { public: int maxArea(vector& height) { diff --git a/CPP/Problems/Number of Dice Rolls With Target Sum.cpp b/CPP/Problems/Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp similarity index 90% rename from CPP/Problems/Number of Dice Rolls With Target Sum.cpp rename to CPP/Problems/Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp index fc091137..45527c8a 100644 --- a/CPP/Problems/Number of Dice Rolls With Target Sum.cpp +++ b/CPP/Problems/Leetcode-1155-Number-of-dice-rolls-with-targe-sum.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/ class Solution { public: diff --git a/CPP/Problems/check-if-it-is-a-straight-line.cpp b/CPP/Problems/Leetcode-1232-Check-If-It-Is-A-Straight-Line.cpp similarity index 100% rename from CPP/Problems/check-if-it-is-a-straight-line.cpp rename to CPP/Problems/Leetcode-1232-Check-If-It-Is-A-Straight-Line.cpp diff --git a/CPP/Problems/Roman to Integer.cpp b/CPP/Problems/Leetcode-13-Roman-to-Integer.cpp similarity index 90% rename from CPP/Problems/Roman to Integer.cpp rename to CPP/Problems/Leetcode-13-Roman-to-Integer.cpp index bb2a56ad..d9d179f7 100644 --- a/CPP/Problems/Roman to Integer.cpp +++ b/CPP/Problems/Leetcode-13-Roman-to-Integer.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/roman-to-integer/ class Solution { public: int romanToInt(string s) { 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 new file mode 100644 index 00000000..de7313bd --- /dev/null +++ b/CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp @@ -0,0 +1,15 @@ +// 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 new file mode 100644 index 00000000..4054ec1b --- /dev/null +++ b/CPP/Problems/Leetcode-1351-Count-Negative-Numbers-in-a-Sorted-Matrix.cpp @@ -0,0 +1,17 @@ +// 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/MaximumProductSubarray.cpp b/CPP/Problems/Leetcode-152-Maximum-Product-Subarray.cpp similarity index 89% rename from CPP/Problems/MaximumProductSubarray.cpp rename to CPP/Problems/Leetcode-152-Maximum-Product-Subarray.cpp index 5a5c56a9..c3f97ea9 100644 --- a/CPP/Problems/MaximumProductSubarray.cpp +++ b/CPP/Problems/Leetcode-152-Maximum-Product-Subarray.cpp @@ -1,7 +1,4 @@ -// Question Link // https://leetcode.com/problems/maximum-product-subarray/ -// ---------------------------------------------- - class Solution { public: int maxProduct(vector& nums) { diff --git a/CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp b/CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp new file mode 100644 index 00000000..de887a0d --- /dev/null +++ b/CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp @@ -0,0 +1,23 @@ +// 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/add-two-numbers.cpp b/CPP/Problems/Leetcode-2-Add-Two-Numbers.cpp similarity index 96% rename from CPP/Problems/add-two-numbers.cpp rename to CPP/Problems/Leetcode-2-Add-Two-Numbers.cpp index af90a79c..80c433a9 100644 --- a/CPP/Problems/add-two-numbers.cpp +++ b/CPP/Problems/Leetcode-2-Add-Two-Numbers.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/add-two-numbers/ /** * Definition for singly-linked list. * struct ListNode { diff --git a/CPP/Problems/Leetcode-231-Power-of-Two.cpp b/CPP/Problems/Leetcode-231-Power-of-Two.cpp new file mode 100644 index 00000000..7a074138 --- /dev/null +++ b/CPP/Problems/Leetcode-231-Power-of-Two.cpp @@ -0,0 +1,7 @@ +// 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/biwise_xor_pairing.cpp b/CPP/Problems/Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp similarity index 89% rename from CPP/Problems/biwise_xor_pairing.cpp rename to CPP/Problems/Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp index c15876e5..3bcb7b30 100644 --- a/CPP/Problems/biwise_xor_pairing.cpp +++ b/CPP/Problems/Leetcode-2425-Bitwise-XOR-Of-All-Pairings.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/bitwise-xor-of-all-pairings/ class Solution { public: int xorAllNums(vector& nums1, vector& nums2) { diff --git a/CPP/Problems/move-zeroes.cpp b/CPP/Problems/Leetcode-283-Move-Zeroes.cpp similarity index 89% rename from CPP/Problems/move-zeroes.cpp rename to CPP/Problems/Leetcode-283-Move-Zeroes.cpp index 68e67964..e4722a2f 100644 --- a/CPP/Problems/move-zeroes.cpp +++ b/CPP/Problems/Leetcode-283-Move-Zeroes.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/move-zeroes/ class Solution { public: void moveZeroes(vector& nums) { diff --git a/CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp b/CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp new file mode 100644 index 00000000..b6b30564 --- /dev/null +++ b/CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp @@ -0,0 +1,30 @@ +// 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 6ebcb4e2..a98ff0cd 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,36 +1,22 @@ +// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ class Solution { public: vector searchRange(vector& nums, int target) { - 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; + 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); } - 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; - } - + else + { + int start=low-nums.begin(); + int end=upp-nums.begin()-1; + ans.push_back(start); + ans.push_back(end); } - p2=l; - if(p1!=-1) - return {p1,p2-1}; - return {-1,-1}; + return ans; } }; diff --git a/CPP/Problems/Median of Two Sorted Arrays.cpp b/CPP/Problems/Leetcode-4-Median-of-Two-Sorted-Arrays.cpp similarity index 93% rename from CPP/Problems/Median of Two Sorted Arrays.cpp rename to CPP/Problems/Leetcode-4-Median-of-Two-Sorted-Arrays.cpp index fb2772ea..b83e8dcf 100644 --- a/CPP/Problems/Median of Two Sorted Arrays.cpp +++ b/CPP/Problems/Leetcode-4-Median-of-Two-Sorted-Arrays.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/median-of-two-sorted-arrays/ class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { diff --git a/CPP/Problems/Leetcode-43-Multiply-Strings.cpp b/CPP/Problems/Leetcode-43-Multiply-Strings.cpp new file mode 100644 index 00000000..6b572c10 --- /dev/null +++ b/CPP/Problems/Leetcode-43-Multiply-Strings.cpp @@ -0,0 +1,8 @@ +// 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/pow(x,n).cpp b/CPP/Problems/Leetcode-50-Pow(x,n).cpp similarity index 89% rename from CPP/Problems/pow(x,n).cpp rename to CPP/Problems/Leetcode-50-Pow(x,n).cpp index c844629b..0b497a84 100644 --- a/CPP/Problems/pow(x,n).cpp +++ b/CPP/Problems/Leetcode-50-Pow(x,n).cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/powx-n/ class Solution { public: //binary exponentiation diff --git a/CPP/Problems/maximum-subarray.cpp b/CPP/Problems/Leetcode-53-Maximum-Subarray.cpp similarity index 100% rename from CPP/Problems/maximum-subarray.cpp rename to CPP/Problems/Leetcode-53-Maximum-Subarray.cpp diff --git a/CPP/Problems/Longest-Continuous-Increasing-Subsequence-Easy.cpp b/CPP/Problems/Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp similarity index 82% rename from CPP/Problems/Longest-Continuous-Increasing-Subsequence-Easy.cpp rename to CPP/Problems/Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp index 755ca9e8..6676d1eb 100644 --- a/CPP/Problems/Longest-Continuous-Increasing-Subsequence-Easy.cpp +++ b/CPP/Problems/Leetcode-674-Longest-Continuous-Increasing-Subsequence-Easy.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/longest-continuous-increasing-subsequence/ class Solution { public: int findLengthOfLCIS(vector& nums) { diff --git a/CPP/Problems/Leetcode-7-Reverse-Integer.cpp b/CPP/Problems/Leetcode-7-Reverse-Integer.cpp new file mode 100644 index 00000000..e9dfd061 --- /dev/null +++ b/CPP/Problems/Leetcode-7-Reverse-Integer.cpp @@ -0,0 +1,15 @@ +// 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/Delete_and_Earn.cpp b/CPP/Problems/Leetcode-740-Delete-And-Earn.cpp similarity index 90% rename from CPP/Problems/Delete_and_Earn.cpp rename to CPP/Problems/Leetcode-740-Delete-And-Earn.cpp index 96b1d5c7..e6b523f5 100644 --- a/CPP/Problems/Delete_and_Earn.cpp +++ b/CPP/Problems/Leetcode-740-Delete-And-Earn.cpp @@ -1,6 +1,4 @@ -// Question Link // https://leetcode.com/problems/delete-and-earn/ -// ---------------------------------------------- class Solution { public: int deleteAndEarn(vector& nums) { diff --git a/CPP/Problems/String to Integer (atoi).cpp b/CPP/Problems/Leetcode-8-String-to-Integer-(atoi).cpp similarity index 95% rename from CPP/Problems/String to Integer (atoi).cpp rename to CPP/Problems/Leetcode-8-String-to-Integer-(atoi).cpp index 53cd345d..daf1e6a1 100644 --- a/CPP/Problems/String to Integer (atoi).cpp +++ b/CPP/Problems/Leetcode-8-String-to-Integer-(atoi).cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/string-to-integer-atoi/ class Solution { public: int myAtoi(string s) { diff --git a/CPP/Problems/merge-sorted-arrays.cpp b/CPP/Problems/Leetcode-88-Merge-Sorted-Arrays.cpp similarity index 89% rename from CPP/Problems/merge-sorted-arrays.cpp rename to CPP/Problems/Leetcode-88-Merge-Sorted-Arrays.cpp index 584fb99c..1f8b1778 100644 --- a/CPP/Problems/merge-sorted-arrays.cpp +++ b/CPP/Problems/Leetcode-88-Merge-Sorted-Arrays.cpp @@ -1,7 +1,8 @@ +// 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/Palindrome Number.cpp b/CPP/Problems/Leetcode-9-Palindrome-Number.cpp similarity index 89% rename from CPP/Problems/Palindrome Number.cpp rename to CPP/Problems/Leetcode-9-Palindrome-Number.cpp index 6b4ba480..27605362 100644 --- a/CPP/Problems/Palindrome Number.cpp +++ b/CPP/Problems/Leetcode-9-Palindrome-Number.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/palindrome-number/ class Solution { public: bool isPalindrome(int x) { diff --git a/CPP/Problems/valid-mountain-array.cpp b/CPP/Problems/Leetcode-941-Valid-Mountain-Array.cpp similarity index 88% rename from CPP/Problems/valid-mountain-array.cpp rename to CPP/Problems/Leetcode-941-Valid-Mountain-Array.cpp index 936c9fb5..301f38d6 100644 --- a/CPP/Problems/valid-mountain-array.cpp +++ b/CPP/Problems/Leetcode-941-Valid-Mountain-Array.cpp @@ -1,4 +1,4 @@ - +// https://leetcode.com/problems/valid-mountain-array/ class Solution { public: bool validMountainArray(vector& A) { diff --git a/CPP/Problems/Reverse Integer.cpp b/CPP/Problems/Reverse Integer.cpp deleted file mode 100644 index f390d5de..00000000 --- a/CPP/Problems/Reverse Integer.cpp +++ /dev/null @@ -1,17 +0,0 @@ -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/findTheDuplicateNumber.cpp b/CPP/Problems/findTheDuplicateNumber.cpp deleted file mode 100644 index e37f2d7c..00000000 --- a/CPP/Problems/findTheDuplicateNumber.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* 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/queries-on-number-of-points-inside-circle.cpp b/CPP/Problems/queries-on-number-of-points-inside-circle.cpp deleted file mode 100644 index 78a3fda4..00000000 --- a/CPP/Problems/queries-on-number-of-points-inside-circle.cpp +++ /dev/null @@ -1,25 +0,0 @@ -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