├── LeetCode ├── Arrays │ ├── [C++] 344. Reverse String.cpp │ ├── [C++] [String] 3110. Score of a String.cpp │ ├── [C++] [String] 1903. Largest Odd Number in String.cpp │ ├── [C++] 3151. Special Array I.cpp │ ├── [C++] 2778. Sum of Squares of Special Elements .cpp │ ├── [C++] 1752. Check if Array Is Sorted and Rotated.cpp │ ├── [C++] [String] 2490. Circular Sentence.cpp │ ├── [C++] 905. Sort Array By Parity.cpp │ ├── [C++] [String] 3174. Clear Digits.cpp │ ├── [C++] 2486. Append Characters to String to Make Subsequence.cpp │ ├── [C++] [String] 1614. Maximum Nesting Depth of the Parentheses.cpp │ ├── [C++] [Matrix] 867. Transpose Matrix.cpp │ ├── [C++] [String] 2696. Minimum String Length After Removing Substrings.cpp │ ├── [C++] 1460. Make Two Arrays Equal by Reversing Subarrays.cpp │ ├── [C++] 2176. Count Equal and Divisible Pairs in an Array.cpp │ ├── [C++] 2540. Minimum Common Value.cpp │ ├── [C++] [String] 1759. Count Number of Homogenous Substrings.cpp │ ├── [C++] 2124. Check if All A's Appears Before All B's.cpp │ ├── [C++] 3223. Minimum Length of String After Operations.cpp │ ├── [C++] 1800. Maximum Ascending Subarray Sum.cpp │ ├── [C++] [String] 2938. Separate Black and White Balls.cpp │ ├── [C++] 1346. Check If N and Its Double Exist.cpp │ ├── [C++] 53. Maximum Subarray.cpp │ ├── [C++] [String] 1544. Make The String Great.cpp │ ├── [C++] 3024. Type of Triangle.cpp │ ├── [C++] [String] 168. Excel Sheet Column Title.cpp │ ├── [C++] [String] 459. Repeated Substring Pattern.cpp │ ├── [C++] 1823. Find the Winner of the Circular Game.cpp │ ├── [C++] [String] 2109. Adding Spaces to a String.cpp │ ├── [C++] 2185. Counting Words With a Given Prefix.cpp │ ├── [C++] [String] 1945. Sum of Digits of String After Convert.cpp │ ├── [C++] [String] 3163. String Compression III.cpp │ ├── [C++] 1014. Best Sightseeing Pair.cpp │ ├── [C++] [String] 412. Fizz Buzz.cpp │ ├── [C++] [String] 242. Valid Anagram.cpp │ ├── [C++] 2108. Find First Palindromic String in the Array.cpp │ ├── [C++] 2874. Maximum Value of an Ordered Triplet II.cpp │ ├── [C++] 1287. Element Appearing More Than 25% In Sorted Array.cpp │ ├── [C++] 1598. Crawler Log Folder.cpp │ ├── [C++] 349. Intersection of Two Arrays.cpp │ ├── [C++] 3392. Count Subarrays of Length Three With a Condition.cpp │ ├── [C++] [String] 58. Length of Last Word.cpp │ ├── [C++] [String] 2483. Minimum Penalty for a Shop.cpp │ ├── [C++] 2073. Time Needed to Buy Tickets.cpp │ ├── [C++] [String] 2825. Make String a Subsequence Using Cyclic Increments.cpp │ ├── [C++] [String] 1408. String Matching in an Array.cpp │ ├── [C++] 518. Water Bottles.cpp │ ├── [C++] 962. Maximum Width Ramp.cpp │ ├── [C++] 2000. Reverse Prefix of Word.cpp │ ├── [C++] 2554. Maximum Number of Integers to Choose From a Range I.cpp │ ├── [C++] 1464. Maximum Product of Two Elements in an Array.cpp │ ├── [C++] 3005. Count Elements With Maximum Frequency.cpp │ ├── [C++] [String] 1400. Construct K Palindrome Strings.cpp │ ├── [C++] [Matrix] 2022. Convert 1D Array Into 2D Array.cpp │ ├── [C++] 134. Gas Station.cpp │ ├── [C++] 135. Candy.cpp │ ├── [C++] 1894. Find the Student that Will Replace the Chalk.cpp │ ├── [C++] [String] 205. Isomorphic Strings.cpp │ ├── [C++] 624. Maximum Distance in Arrays.cpp │ ├── [C++] [String] 2038. Remove Colored Pieces if Both Neighbors are the Same Color.cpp │ ├── [C++] [String] 6. Zigzag Conversion.cpp │ ├── [C++] 1512. Number of Good Pairs.cpp │ ├── [C++] 2706. Buy Two Chocolates.cpp │ ├── [C++] 66. Plus One.cpp │ ├── [C++] [String+Hashmap] 1647. Minimum Deletions to Make Character Frequencies Unique.cpp │ ├── [C++] 1503. Last Moment Before All Ants Fall Out of a Plank.cpp │ ├── [C++] 167. Two Sum II - Input Array Is Sorted.cpp │ ├── [C++] 2419. Longest Subarray With Maximum Bitwise AND.cpp │ ├── [C++] 1545. Find Kth Bit in Nth Binary String.cpp │ ├── [C++] 1422. Maximum Score After Splitting a String.cpp │ ├── [C++] 2028. Find Missing Observations.cpp │ ├── [C++] 2270. Number of Ways to Split Array.cpp │ ├── [C++] [String] 2678. Number of Senior Citizens.cpp │ ├── [C++] 169. Majority Element.cpp │ ├── [C++] 1535. Find the Winner of an Array Game.cpp │ ├── [C++] 1920. Build Array from Permutation.cpp │ ├── [C++] 3232. Find if Digit Game Can Be Won.cpp │ ├── [C++] 3191. Minimum Operations to Make Binary Array Elements Equal to One I.cpp │ ├── [C++] [String] 557. Reverse Words in a String III.cpp │ ├── [C++] [Matrix] 1424. Diagonal Traverse II.cpp │ ├── [C++] [String] 2864. Maximum Odd Binary Number.cpp │ ├── [C++] 62. Unique Paths.cpp │ ├── [C++] 3375. Minimum Operations to Make Array Values Equal to K.cpp │ ├── [C++] 1701. Average Waiting Time.cpp │ ├── [C++] 2657. Find the Prefix Common Array of Two Arrays.cpp │ ├── [C++] 881. Boats to Save People.cpp │ ├── [C++] 896. Monotonic Array.cpp │ ├── [C++] [Matrix] 1605. Find Valid Matrix Given Row and Column Sums.cpp │ ├── [C++] 1796. Second Largest Digit in a String.cpp │ ├── [C++] 3169. Count Days Without Meetings.cpp │ ├── [C++] [String] 214. Shortest Palindrome.cpp │ ├── [C++] [String] 1704. Determine if String Halves Are Alike.cpp │ ├── [C++] [String] 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.cpp │ ├── [C++] 446. Arithmetic Slices II - Subsequence.cpp │ ├── [C++] 2971. Find Polygon With the Largest Perimeter.cpp │ ├── [C++] 860. Lemonade Change.cpp │ ├── [C++] [String] 1957. Delete Characters to Make Fancy String.cpp │ ├── [C++] 3355. Zero Array Transformation I.cpp │ ├── [C++] 1534. Count Good Triplets.cpp │ ├── [C++] 2460. Apply Operations to an Array.cpp │ ├── [C++] [String] 2264. Largest 3-Same-Digit Number in String.cpp │ ├── [C++] [String] 796. Rotate String.cpp │ ├── [C++] 2914. Minimum Number of Changes to Make Binary String Beautiful.cpp │ ├── [C++] 948. Bag of Tokens.cpp │ ├── [C++] [Matrix] 2017. Grid Game.cpp │ ├── [C++] 977. Squares of a Sorted Array.cpp │ ├── [C++] 1700. Number of Students Unable to Eat Lunch.cpp │ ├── [C++] [String] 1790. Check if One String Swap Can Make Strings Equal.cpp │ ├── [C++] 2900. Longest Unequal Adjacent Groups Subsequence I.cpp │ ├── [C++] 56. Merge Intervals.cpp │ ├── [C++] 1267. Count Servers that Communicate.cpp │ ├── [C++] 2444. Count Subarrays With Fixed Bounds.cpp │ ├── [C++] 1769. Minimum Number of Operations to Move All Balls to Each Box.cpp │ ├── [C++] 452. Minimum Number of Arrows to Burst Balloons.cpp │ ├── [C++] 1750. Minimum Length of String After Deleting Similar Ends.cpp │ ├── [C++] [String] 880. Decoded String at Index.cpp │ ├── [C++] 238. Product of Array Except Self.cpp │ ├── [C++] [Matrix] 200. Number of Islands.cpp │ └── [C++] [String] 1436. Destination City.cpp ├── Bit Manipulation │ ├── [C++] 338. Counting Bits.cpp │ ├── [C++] 3133. Minimum Array End.cpp │ ├── [C++] 2433. Find The Original Array of Prefix Xor.cpp │ ├── [C++] 476. Number Complement.cpp │ ├── [C++] 461. Hamming Distance.cpp │ ├── [C++] 342. Power of Four.cpp │ ├── [C++] 231. Power of Two.cpp │ ├── [C++] 2425. Bitwise XOR of All Pairings.cpp │ ├── [C++] 201. Bitwise AND of Numbers Range.cpp │ ├── [C++] 2220. Minimum Bit Flips to Convert Number.cpp │ ├── [C++] 268. Missing Number.cpp │ ├── [C++] 191. Number of 1 Bits.cpp │ ├── [C++] 2997. Minimum Number of Operations to Make Array XOR Equal to K.cpp │ ├── [C++] 389. Find the Difference.cpp │ ├── [C++] 2275. Largest Combination With Bitwise AND Greater Than Zero.cpp │ ├── [C++] 1310. XOR Queries of a Subarray.cpp │ └── [C++] 1611. Minimum One Bit Operations to Make Integers Zero.cpp ├── Math │ ├── [C++] 458. Poor Pigs.cpp │ ├── [C++] 2579. Count Total Number of Colored Cells.cpp │ ├── [C++] 2894. Divisible and Non-divisible Sums Difference.cpp │ ├── [C++] 1266. Minimum Time Visiting All Points.cpp │ ├── [C++] 2485. Find the Pivot Integer.cpp │ ├── [C++] 781. Rabbits in Forest.cpp │ ├── [C++] 2145. Count the Hidden Sequences.cpp │ ├── [C++] 2849. Determine if a Cell Is Reachable at a Given Time.cpp │ ├── [C++] 1359. Count All Valid Pickup and Delivery Options.cpp │ ├── [C++] 2125. Number of Laser Beams in a Bank.cpp │ ├── [C++] 1688. Count of Matches in Tournament.cpp │ ├── [C++] 1685. Sum of Absolute Differences in a Sorted Array.cpp │ ├── [C++] 1295. Find Numbers with Even Number of Digits.cpp │ ├── [C++] 1922. Count Good Numbers.cpp │ ├── [C++] 2147. Number of Ways to Divide a Long Corridor.cpp │ ├── [C++] 1716. Calculate Money in Leetcode Bank.cpp │ └── [C++] 1814. Count Nice Pairs in an Array.cpp ├── Graph │ ├── [C++] 1791. Find Center of Star Graph.cpp │ ├── [C++] 2924. Find Champion II.cpp │ └── [C++] 2285. Maximum Total Importance of Roads.cpp ├── Sorting and Searching │ ├── [C++] 3075. Maximize Happiness of Selected Children.cpp │ ├── [C++] 1887. Reduction Operations to Make the Array Elements Equal.cpp │ ├── [C++] 2037. Minimum Number of Moves to Seat Everyone.cpp │ ├── [C++] 1877. Minimize Maximum Pair Sum in Array.cpp │ ├── [C++] 455. Assign Cookies.cpp │ ├── [C++] 1637. Widest Vertical Area Between Two Points Containing No Points.cpp │ ├── [C++] 1636. Sort Array by Increasing Frequency.cpp │ ├── [C++] 945. Minimum Increment to Make Array Unique.cpp │ ├── [C++] 2966. Divide Array Into Arrays With Max Difference.cpp │ ├── [C++] 2491. Divide Players Into Teams of Equal Skill.cpp │ ├── [C++] 1561. Maximum Number of Coins You Can Get.cpp │ ├── [C++] 350. Intersection of Two Arrays II.cpp │ ├── [C++] 2551. Put Marbles in Bags.cpp │ ├── 4. Median of Two Sorted Arrays │ │ └── [C++] [Brute Force] 4. Median of Two Sorted Arrays.cpp │ ├── [C++] 1122. Relative Sort Array.cpp │ └── [C++] 1921. Eliminate Maximum Number of Monsters.cpp ├── Hashmap │ ├── [C++] 1. Two Sum.cpp │ ├── [C++] 2206. Divide Array Into Equal Pairs.cpp │ ├── [C++] 387. First Unique Character in a String.cpp │ ├── [C++] 997. Find the Town Judge.cpp │ ├── [C++] 1207. Unique Number of Occurrences.cpp │ ├── [C++] 2053. Kth Distinct String in an Array.cpp │ ├── [C++] 2441. Largest Positive Integer That Exists With Its Negative.cpp │ ├── [C++] 1624. Largest Substring Between Two Equal Characters.cpp │ ├── [C++] 3295. Report Spam Message.cpp │ ├── [C++] 560. Subarray Sum Equals K.cpp │ ├── [C++] 930. Binary Subarrays With Sum.cpp │ ├── [C++] 1331. Rank Transform of an Array.cpp │ ├── [C++] 13. Roman to Integer.cpp │ ├── [C++] 1347. Minimum Number of Steps to Make Two Strings Anagram.cpp │ ├── [C++] 1128. Number of Equivalent Domino Pairs.cpp │ ├── [C++] 2501. Longest Square Streak in an Array.cpp │ ├── [C++] 1726. Tuple with Same Product.cpp │ ├── [C++] 645. Set Mismatch.cpp │ ├── [C++] 523. Continuous Subarray Sum.cpp │ ├── [C++] 2870. Minimum Number of Operations to Make Array Empty.cpp │ ├── [C++] 1684. Count the Number of Consistent Strings.cpp │ ├── [C++] 525. Contiguous Array.cpp │ ├── [C++] [Weekly Contest 381 Q1] 3014. Minimum Number of Pushes to Type Word I.cpp │ ├── [C++] 1248. Count Number of Nice Subarrays.cpp │ ├── [C++] 2610. Convert an Array Into a 2D Array With Conditions.cpp │ ├── [C++] 2225. Find Players With Zero or One Losses.cpp │ ├── [C++] 1002. Find Common Characters.cpp │ ├── [C++] 1657. Determine if Two Strings Are Close.cpp │ ├── [C++] 974. Subarray Sums Divisible by K.cpp │ ├── [C++] 2965. Find Missing and Repeated Values.cpp │ ├── [C++] 1496. Path Crossing.cpp │ ├── [C++] 763. Partition Labels.cpp │ ├── [C++] 2981. Find Longest Special Substring That Occurs Thrice I.cpp │ └── [C++] 823. Binary Trees With Factors.cpp ├── Sliding Window │ ├── [C++] 3. Longest Substring Without Repeating Characters.cpp │ ├── [C++] 2958. Length of Longest Subarray With at Most K Frequency.cpp │ ├── [C++] 1208. Get Equal Substrings Within Budget.cpp │ ├── [C++] 209. Minimum Size Subarray Sum.cpp │ ├── [C++] 1574. Shortest Subarray to be Removed to Make Array Sorted.cpp │ ├── [C++] 2779. Maximum Beauty of an Array After Applying Operation.cpp │ ├── [C++] 2302. Count Subarrays With Score Less Than K.cpp │ ├── [C++] 2401. Longest Nice Subarray.cpp │ ├── [C++] 3254. Find the Power of K-Size Subarrays I.cpp │ ├── [C++] 1358. Number of Substrings Containing All Three Characters.cpp │ ├── [C++] 2762. Continuous Subarrays.cpp │ ├── [C++] 713. Subarray Product Less Than K.cpp │ ├── [C++] 2537. Count the Number of Good Subarrays.cpp │ ├── 1456. Maximum Number of Vowels in a Substring of Given Length.cpp │ ├── [C++] 3208. Alternating Groups II.cpp │ └── [C++] 2379. Minimum Recolors to Get K Consecutive Black Blocks.cpp ├── Binary Search │ ├── [C++] 852. Peak Index in a Mountain Array.cpp │ └── [C++] 153. Find Minimum in Rotated Sorted Array.cpp ├── Heap │ ├── [C++] 2558. Take Gifts From the Richest Pile.cpp │ ├── [C++] 1845. Seat Reservation Manager.cpp │ ├── [C++] 3264. Final Array State After K Multiplication Operations I.cpp │ ├── [C++] 215. Kth Largest Element in an Array.cpp │ ├── [C++] 3066. Minimum Operations to Exceed Threshold Value II.cpp │ ├── [C++] 1481. Least Number of Unique Integers after K Removals.cpp │ ├── [C++] 1337. The K Weakest Rows in a Matrix.cpp │ └── [C++] 3016. Minimum Number of Pushes to Type Word II.cpp ├── Dynamic Programming │ ├── [C++] 343. Integer Break.cpp │ ├── [C++] 279. Perfect Squares.cpp │ ├── [C++] 790. Domino and Tromino Tiling.cpp │ ├── [C++] 70. Climbing Stairs.cpp │ ├── [C++] 198. House Robber.cpp │ ├── [C++] 1043. Partition Array for Maximum Sum.cpp │ ├── [C++] 1155. Number of Dice Rolls With Target Sum.cpp │ ├── [C++] 650. 2 Keys Keyboard.cpp │ ├── [C++] 647. Palindromic Substrings.cpp │ ├── [C++] 64. Minimum Path Sum.cpp │ ├── [C++] 1143. Longest Common Subsequence.cpp │ ├── [C++] 1425. Constrained Subsequence Sum.cpp │ ├── [C++] 494. Target Sum.cpp │ ├── [C++] 2370. Longest Ideal Subsequence.cpp │ ├── [C++] 322. Coin Change.cpp │ ├── [C++] 91. Decode Ways.cpp │ └── [C++] 1289. Minimum Falling Path Sum II.cpp ├── Stack │ ├── [C++] 739. Daily Temperatures.cpp │ ├── 921. Minimum Add to Make Parentheses Valid │ │ └── [C++] [Approach II] 921. Minimum Add to Make Parentheses Valid.cpp │ ├── [C++] 1475. Final Prices With a Special Discount in a Shop.cpp │ ├── [C++] 503. Next Greater Element II.cpp │ ├── [C++] 1190. Reverse Substrings Between Each Pair of Parentheses.cpp │ ├── [C++] 456. 132 Pattern.cpp │ └── [C++] 1963. Minimum Number of Swaps to Make the String Balanced.cpp ├── Recursion │ ├── [C++] 386. Lexicographical Numbers.cpp │ ├── [C++] 779. K-th Symbol in Grammar.cpp │ ├── [C++] 2044. Count Number of Maximum Bitwise-OR Subsets.cpp │ ├── [C++] [Recursion+Backtracking] 46. Permutations.cpp │ └── [C++] 39. Combination Sum.cpp ├── Queue │ ├── [C++] [Deque] 950. Reveal Cards In Increasing Order.cpp │ └── [C++] 239. Sliding Window Maximum.cpp ├── Linked List │ ├── [C++] 237. Delete Node in a Linked List.cpp │ └── [C++] 141. Linked List Cycle.cpp └── Binary Tree │ └── [C++] 590. N-ary Tree Postorder Traversal.cpp ├── Recursion ├── [C++] Reverse String using Recursion.cpp ├── [C++] Check Palindrome using Recursion.cpp ├── [C++] [CodeStudio] Find power of a number.cpp ├── [C++] [Optimised] Calculate Power using Recursion.cpp ├── [C++] [CodeStudio] Remove Consecutive Duplicates.cpp ├── [C++] [CodeStudio] Family Structure.cpp ├── [C++] [CodeStudio] Subsequences of String.cpp ├── [C++] Selection Sort using Recursion.cpp └── [C++] Insertion Sort using Recursion.cpp ├── Queue ├── Queue Reversal │ ├── [C++] [GFG] [Using Stack] Queue Reversal.cpp │ └── [C++] [GFG] [Using Recursion] Queue Reversal.cpp ├── [C++] [GFG] Reverse First K elements of Queue.cpp └── [C++] [GFG] First non-repeating character in a stream.cpp ├── Hashmaps └── [C++] [CodeStudio] Maximum Frequency Number.cpp ├── Stack ├── [C++] [CodeStudio] Delete middle element from stack.cpp ├── [C++] [CodeStudio] Next Smaller Element.cpp ├── [C++] [CodeStudio] Insert An Element At Its Bottom In A Given Stack.cpp ├── [C++] [CodeStudio] Sort a Stack.cpp ├── [C++] [CodeStudio] Reverse Stack Using Recursion.cpp ├── [C++] [CodeStudio] Minimum Cost To Make String Valid.cpp └── [C++] [CodeStudio] Redundant Brackets.cpp ├── Heap └── [C++] [CodeStudio] Build Min Heap.cpp └── Binary Tree └── [C++] [GFG] Left View of Binary Tree.cpp /LeetCode/Arrays/[C++] 344. Reverse String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int n = s.size(); 5 | 6 | for(int i=0;i=0;j--) 5 | if((num[j] - '0')&1) 6 | return num.substr(0, j+1); 7 | 8 | return ""; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/Bit Manipulation/[C++] 338. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | vector ans(n+1); 5 | ans[0] = 0; 6 | for(int i=1;i<=n;i++) 7 | { 8 | ans[i] = ans[i/2] + (i&1); 9 | } 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/Math/[C++] 458. Poor Pigs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 4 | int noOfTrails = minutesToTest/minutesToDie; 5 | 6 | int pigs = 0; 7 | while(pow(noOfTrails+1, pigs) < buckets) 8 | pigs++; 9 | 10 | return pigs; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 3151. Special Array I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isArraySpecial(vector& nums) { 4 | int n = nums.size(); 5 | for(int i=0; i 2 | 3 | using namespace std; 4 | void rev(string &a, int s){ 5 | if(s>(a.length()/2)){ 6 | return; 7 | } 8 | swap(a[s], a[a.length()-s-1]); 9 | return rev(a, s+1); 10 | } 11 | 12 | int main() 13 | { 14 | string a="abcde"; 15 | rev(a,0); 16 | cout< 2 | 3 | using namespace std; 4 | bool palin(string &a,int i=0){ 5 | if(i>(a.length()/2)) 6 | return true; 7 | if(a[i] != a[a.length()-1-i]) 8 | return false; 9 | return palin(a, i+1); 10 | } 11 | int main() 12 | { 13 | string a="kishan"; 14 | cout<& nums) { 4 | int ans = 0, n = nums.size(); 5 | for(int i=0; i O(n) 3 | S.C -> O(n) 4 | */ 5 | 6 | queue rev(queue q) 7 | { 8 | stack s; 9 | while(!q.empty()) 10 | { 11 | s.push(q.front()); 12 | q.pop(); 13 | } 14 | 15 | while(!s.empty()) 16 | { 17 | q.push(s.top()); 18 | s.pop(); 19 | } 20 | return q; 21 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1752. Check if Array Is Sorted and Rotated.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& nums) { 4 | int n = nums.size(), counter = 0; 5 | for(int i=1; i nums[0]) 10 | counter++; 11 | 12 | return counter <= 1; 13 | } 14 | }; -------------------------------------------------------------------------------- /Recursion/[C++] [CodeStudio] Find power of a number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | long long Pow(int X, int N) 3 | { 4 | //base case 5 | if(N == 0) 6 | return 1; 7 | if(N == 1) 8 | return X; 9 | 10 | long long ans = Pow(X, N/2); 11 | 12 | //if N is odd 13 | if(N & 1) 14 | return X*ans*ans; 15 | 16 | //if N is Even 17 | else 18 | return ans*ans; 19 | } -------------------------------------------------------------------------------- /LeetCode/Graph/[C++] 1791. Find Center of Star Graph.cpp: -------------------------------------------------------------------------------- 1 | // Since in Star Graph, Center node is the only one repeating multiple times. 2 | 3 | class Solution { 4 | public: 5 | int findCenter(vector>& edges) { 6 | int u = edges[0][0]; 7 | int v = edges[0][1]; 8 | 9 | if(u == edges[1][0] || u == edges[1][1]) 10 | return u; 11 | else 12 | return v; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/Math/[C++] 2579. Count Total Number of Colored Cells.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long coloredCells(int n) { 4 | long long ans = 0; 5 | for(int i=0; i& happiness, int k) { 4 | sort(begin(happiness), end(happiness), greater()); 5 | 6 | long long ans = 0; 7 | for(int i=0;i O(n) 3 | S.C -> O(n) [since recursion uses stack internally] 4 | */ 5 | 6 | void reversal(queue &q) 7 | { 8 | if(q.empty()) 9 | return; 10 | 11 | int e = q.front(); 12 | q.pop(); 13 | reversal(q); 14 | q.push(e); 15 | 16 | } 17 | 18 | queue rev(queue q) 19 | { 20 | reversal(q); 21 | return q; 22 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 2490. Circular Sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isCircularSentence(string s) { 4 | int n = s.length(); 5 | 6 | for(int i=0; i findArray(vector& pref) { 10 | for(int i = pref.size()-1;i>=1;i--){ 11 | pref[i] = pref[i] ^ pref[i-1]; 12 | } 13 | 14 | return pref; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 905. Sort Array By Parity.cpp: -------------------------------------------------------------------------------- 1 | //Two Pointer 2 | 3 | class Solution { 4 | public: 5 | vector sortArrayByParity(vector& nums) { 6 | int i=0,j=nums.size()-1; 7 | while(i= '0' && ch <= '9' && !ans.empty()) 8 | ans.pop_back(); 9 | else 10 | ans += ch; 11 | } 12 | 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/Math/[C++] 2894. Divisible and Non-divisible Sums Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int differenceOfSums(int n, int m) { 4 | int sum1 = (n*(n+1))/2; 5 | 6 | // Sum of no.s that are divisible. 7 | // Sum of Arithemetic Progression, Sn = n/2 * [2a + (n - 1)d] 8 | int n1 = n/m; 9 | int sumDiv = (n1 * (2*m + (n1-1)*m))/2; 10 | 11 | return (sum1 - sumDiv) - sumDiv; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 1. Two Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | unordered_map mp; 5 | 6 | for(int i = 0; i& nums) { 4 | unordered_map mp; 5 | for(int &num : nums) 6 | mp[num]++; 7 | 8 | for(auto &i : mp){ 9 | if(mp[i.first]&1) 10 | return false; 11 | } 12 | 13 | return true; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/Sorting and Searching/[C++] 1887. Reduction Operations to Make the Array Elements Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reductionOperations(vector& nums) { 4 | int ans = 0; 5 | sort(begin(nums), end(nums), greater()); 6 | 7 | for(int i=0;i nums[i+1]) 9 | ans += i+1; 10 | } 11 | 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2486. Append Characters to String to Make Subsequence.cpp: -------------------------------------------------------------------------------- 1 | // Two-Pointer 2 | 3 | class Solution { 4 | public: 5 | int appendCharacters(string s, string t) { 6 | int m = s.length(), n = t.length(); 7 | int i = 0, j = 0; 8 | 9 | while(i < m && j < n){ 10 | if(s[i] == t[j]) 11 | j++; 12 | 13 | i++; 14 | } 15 | 16 | return n-j; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 387. First Unique Character in a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | unordered_map count; 5 | for(int i=0;i>= 1; 10 | } 11 | 12 | return (~num)&mask; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 1614. Maximum Nesting Depth of the Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(string s) { 4 | int ans = 0; 5 | int count = 0; 6 | for(int i=0;i 2 | 3 | using namespace std; 4 | int power(int a, int b){ 5 | if(b==0) 6 | return 1; 7 | if(b==1) 8 | return a; 9 | if(b%2 == 0) 10 | return (power(a,b/2)*power(a,b/2)); 11 | else 12 | return a*(power(a,b/2)*power(a,b/2)); 13 | } 14 | int main() 15 | { 16 | int a,b; 17 | cin>>a>>b; 18 | cout<> transpose(vector>& matrix) { 5 | int n = matrix.size(); 6 | int m = matrix[0].size(); 7 | vector> ans(m, vector(n)); 8 | 9 | for(int i=0;i &arr, int n) 2 | { 3 | unordered_map count; 4 | int maxFreq = 0; 5 | for(auto i:arr) 6 | { 7 | count[i]++; 8 | maxFreq = max(maxFreq, count[i]); 9 | } 10 | 11 | //Return the very first element which has maximum frequency 12 | for(auto i:arr) 13 | { 14 | if(maxFreq == count[i]) 15 | return i; 16 | } 17 | return -1; 18 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 2696. Minimum String Length After Removing Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minLength(string s) { 4 | string temp; 5 | for(char ch:s){ 6 | if((ch == 'B' && temp.size() && temp.back() == 'A') || (ch == 'D' && temp.size() && temp.back() == 'C')) 7 | temp.pop_back(); 8 | else 9 | temp.push_back(ch); 10 | } 11 | 12 | return temp.size(); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/Sorting and Searching/[C++] 2037. Minimum Number of Moves to Seat Everyone.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMovesToSeat(vector& seats, vector& students) { 4 | int n = seats.size(); 5 | sort(begin(seats), end(seats)); 6 | sort(begin(students), end(students)); 7 | 8 | int ans = 0; 9 | for(int i=0; i& target, vector& arr) { 4 | vector numCount(1001, 0); 5 | 6 | for(int &n : target) 7 | numCount[n]++; 8 | 9 | for(int &n : arr) 10 | numCount[n]--; 11 | 12 | for(int &n : numCount) 13 | if(n != 0) 14 | return false; 15 | 16 | return true; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2176. Count Equal and Divisible Pairs in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPairs(vector& nums, int k) { 4 | int ans = 0, n = nums.size(); 5 | for(int i=0; i& nums) { 4 | sort(begin(nums), end(nums)); 5 | 6 | int i = 0; 7 | int j = nums.size()-1; 8 | int sum; 9 | int ans = INT_MIN; 10 | 11 | while(i < j){ 12 | sum = nums[i++] + nums[j--]; 13 | ans = max(ans, sum); 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Sorting and Searching/[C++] 455. Assign Cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | sort(begin(g), end(g)); 5 | sort(begin(s), end(s)); 6 | 7 | int i = 0, j = 0; 8 | int m = g.size(), n = s.size(); 9 | while(i= g[i]){ 11 | i++; 12 | } 13 | j++; 14 | } 15 | 16 | return i; 17 | } 18 | }; -------------------------------------------------------------------------------- /Stack/[C++] [CodeStudio] Delete middle element from stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve(stack&Stack, int count, int N){ 4 | //base case 5 | if(count == N/2) 6 | { 7 | Stack.pop(); 8 | return; 9 | } 10 | int e = Stack.top(); 11 | Stack.pop(); 12 | solve(Stack, count+1, N); 13 | Stack.push(e); 14 | } 15 | 16 | void deleteMiddle(stack&inputStack, int N){ 17 | 18 | // Write your code here 19 | solve(inputStack, 0, N); 20 | 21 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2540. Minimum Common Value.cpp: -------------------------------------------------------------------------------- 1 | // Two Pointer 2 | 3 | class Solution { 4 | public: 5 | int getCommon(vector& nums1, vector& nums2) { 6 | int i=0, j=0; 7 | while(i 2 | vector nextSmallerElement(vector &arr, int n) 3 | { 4 | // Write your code here. 5 | stack st; 6 | st.push(-1); 7 | vector ans(n); 8 | for(int i = n-1;i>=0;i--) 9 | { 10 | int curr = arr[i]; 11 | while(st.top() >= curr) 12 | { 13 | st.pop(); 14 | } 15 | ans[i] = st.top(); 16 | st.push(arr[i]); 17 | } 18 | return ans; 19 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 1759. Count Number of Homogenous Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int MOD = 1e9 + 7; 4 | int countHomogenous(string s) { 5 | int ans = 0; 6 | int len = 1; 7 | 8 | for(int i=0;i0 && s[i] == s[i-1]) 10 | len++; 11 | else 12 | len = 1; 13 | 14 | ans = (ans + len)%MOD; 15 | } 16 | 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Stack/[C++] [CodeStudio] Insert An Element At Its Bottom In A Given Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void solve(stack &stack, int x) 3 | { 4 | if(stack.empty()) 5 | { 6 | stack.push(x); 7 | return; 8 | } 9 | 10 | int e = stack.top(); 11 | stack.pop(); 12 | solve(stack, x); 13 | stack.push(e); 14 | } 15 | stack pushAtBottom(stack& myStack, int x) 16 | { 17 | // Write your code here. 18 | solve(myStack, x); 19 | return myStack; 20 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2124. Check if All A's Appears Before All B's.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkString(string s) { 4 | int lastPosA = -1, firstPosB = 101; 5 | for(int i=0; i charCount(26, 0); 5 | 6 | for(char &ch : s) 7 | charCount[ch-'a']++; 8 | 9 | int ans = 0; 10 | for(int i=0; i<26; i++) 11 | if(charCount[i] & 1) 12 | ans += 1; 13 | else if(charCount[i] > 0) 14 | ans += 2; 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Bit Manipulation/[C++] 461. Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | int ans = 0; 5 | while(x || y) { 6 | int xEndBit = x & 1; 7 | int yEndBit = y & 1; 8 | 9 | // cout<>= 1; 14 | y >>= 1; 15 | } 16 | 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 997. Find the Town Judge.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findJudge(int n, vector>& trust) { 4 | vector trustCount(n+1, 0), trustedBy(n+1, 0); 5 | 6 | for(vector &t:trust){ 7 | trustCount[t[0]]++; 8 | trustedBy[t[1]]++; 9 | } 10 | 11 | for(int i=1;i<=n;i++) 12 | if(trustCount[i] == 0 && trustedBy[i] == n-1) 13 | return i; 14 | 15 | return -1; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1800. Maximum Ascending Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAscendingSum(vector& nums) { 4 | int maxSum = nums[0], currSum = nums[0]; 5 | 6 | for(int i=1; i nums[i-1]) { 8 | currSum += nums[i]; 9 | maxSum = max(maxSum, currSum); 10 | } 11 | else 12 | currSum = nums[i]; 13 | } 14 | 15 | return maxSum; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 2938. Separate Black and White Balls.cpp: -------------------------------------------------------------------------------- 1 | // whenever you encounter a white ball(0), check how many black balls(1) appeared in past and swap with each. 2 | class Solution { 3 | public: 4 | long long minimumSteps(string s) { 5 | long long black = 0, ans = 0; 6 | for(int i=0; i should be a power of 2 4 | -> (n-1) is divisible by 3 5 | e.x. -> 4-1 = 3, 16-1 = 15, 64-1 = 63.... 6 | 7 | */ 8 | 9 | class Solution { 10 | public: 11 | bool isPowerOfFour(int n) { 12 | if(n <= 0) 13 | return false; 14 | 15 | else if((n&(n-1)) == 0 && (n-1)%3 == 0) 16 | return true; 17 | 18 | else 19 | return false; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 1207. Unique Number of Occurrences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool uniqueOccurrences(vector& arr) { 4 | unordered_map mp; 5 | 6 | for(int &i:arr) 7 | mp[i]++; 8 | 9 | unordered_map occurance; 10 | 11 | for(auto &i:mp){ 12 | occurance[i.second]++; 13 | 14 | if(occurance[i.second] > 1) 15 | return false; 16 | } 17 | 18 | return true; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Math/[C++] 1266. Minimum Time Visiting All Points.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minTimeToVisitAllPoints(vector>& points) { 4 | int ans = 0; 5 | for(int i=1;i& arr) { 6 | unordered_map mp; 7 | 8 | for(int i=0; i& nums) { 6 | int ans = nums[0]; 7 | int maxEnding = nums[0]; 8 | 9 | for(int i=1; i nums[i]) ? maxEnding + nums[i] : nums[i]; 11 | 12 | ans = max(ans, maxEnding); 13 | } 14 | 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 1544. Make The String Great.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string makeGood(string s) { 4 | string ans; 5 | ans += s[0]; 6 | 7 | for(int i=1;i& nums) { 4 | sort(begin(nums), end(nums)); 5 | 6 | if(nums[0] + nums[1] <= nums[2]) 7 | return "none"; 8 | 9 | if(nums[0] == nums[1] && nums[0] == nums[2]) 10 | return "equilateral"; 11 | else if(nums[0] == nums[1] || nums[0] == nums[2] || nums[1] == nums[2]) 12 | return "isosceles"; 13 | else 14 | return "scalene"; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 168. Excel Sheet Column Title.cpp: -------------------------------------------------------------------------------- 1 | // The goal of this problem is to convert base10 system to base26 system 2 | 3 | class Solution { 4 | public: 5 | string convertToTitle(int columnNumber) { 6 | string ans; 7 | while(columnNumber > 0) 8 | { 9 | //addition of character at starting of string. 10 | ans = char('A' + (columnNumber - 1) % 26) + ans; 11 | columnNumber = (columnNumber-1)/26; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 459. Repeated Substring Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | -> Create a string double of it's length with itself repeating. 3 | -> If string has repeating substring 4 | -> it would get found on the index which will be less than ending index of string. 5 | -> If not, 6 | -> it gets found out in the second iteration of string, as string got doubled in size. 7 | */ 8 | 9 | class Solution { 10 | public: 11 | bool repeatedSubstringPattern(string s) { 12 | return (s+s).find(s, 1) < s.size(); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 2053. Kth Distinct String in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string kthDistinct(vector& arr, int k) { 4 | unordered_map mp; 5 | for(string &str : arr) 6 | mp[str]++; 7 | 8 | int kCount = 0; 9 | for(string &str : arr){ 10 | if(mp[str] == 1) 11 | kCount++; 12 | 13 | if(kCount == k) 14 | return str; 15 | } 16 | 17 | return ""; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1823. Find the Winner of the Circular Game.cpp: -------------------------------------------------------------------------------- 1 | // Simulation 2 | 3 | class Solution { 4 | public: 5 | int findTheWinner(int n, int k) { 6 | vector persons; 7 | 8 | for(int i=1; i<=n; i++) 9 | persons.push_back(i); 10 | 11 | int i = 0; 12 | while(persons.size() > 1){ 13 | int idx = (i+k-1)%persons.size(); 14 | persons.erase(persons.begin()+idx); 15 | i = idx; 16 | } 17 | 18 | return persons[0]; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 2109. Adding Spaces to a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addSpaces(string s, vector& spaces) { 4 | string ans; 5 | int j = 0; 6 | for(int i=0; i>& points) { 4 | int ans = INT_MIN; 5 | vector x; 6 | for(int i=0;i& words, string pref) { 4 | int ans = 0; 5 | for(int i=0; i 1000 4 | 7 -> 0111 5 | 6 | 1000 & 0111 => 0 7 | */ 8 | 9 | class Solution { 10 | public: 11 | bool isPowerOfTwo(int n) { 12 | if(n<=0) 13 | return false; 14 | if((n&(n-1)) == 0) 15 | return true; 16 | return false; 17 | } 18 | }; 19 | 20 | 21 | // One-Liner 22 | /* 23 | class Solution { 24 | public: 25 | bool isPowerOfTwo(int n) { 26 | return (n > 0 && !(n & (n-1))); 27 | } 28 | }; 29 | */ -------------------------------------------------------------------------------- /LeetCode/Bit Manipulation/[C++] 2425. Bitwise XOR of All Pairings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int xorAllNums(vector& nums1, vector& nums2) { 4 | int n1 = nums1.size(), n2 = nums2.size(); 5 | 6 | int XORs = 0; 7 | if(n2 % 2 != 0) { 8 | for(int i=0; i>& edges) { 4 | unordered_map indegree; 5 | 6 | for(vector &edge : edges) 7 | indegree[edge[1]]++; 8 | 9 | int count = 0, ans; 10 | 11 | for(int i=0; i& answers) { 4 | unordered_map mp; 5 | for(int &i : answers) 6 | mp[i]++; 7 | 8 | int ans = 0; 9 | for(auto &i : mp) { 10 | auto [x, count] = i; 11 | 12 | int grpSize = x+1; 13 | int grpCount = ceil((double)count/grpSize); 14 | 15 | ans += grpCount * (x+1); 16 | } 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Stack/[C++] [CodeStudio] Sort a Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sortedInsert(stack &stack, int x) 4 | { 5 | if(stack.empty() || (x > stack.top())) 6 | { 7 | stack.push(x); 8 | return; 9 | } 10 | int ee = stack.top(); 11 | stack.pop(); 12 | sortedInsert(stack, x); 13 | stack.push(ee); 14 | } 15 | 16 | void sortStack(stack &stack) 17 | { 18 | // Write your code here 19 | if(stack.empty()) 20 | return; 21 | 22 | int e = stack.top(); 23 | stack.pop(); 24 | sortStack(stack); 25 | sortedInsert(stack, e); 26 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 1945. Sum of Digits of String After Convert.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getLucky(string s, int k) { 4 | string positions = ""; 5 | for(char &ch : s) { 6 | positions += to_string(ch-'a'+1); 7 | } 8 | 9 | int sum; 10 | while(k--) { 11 | sum = 0; 12 | for(char &ch : positions) 13 | sum += ch-'0'; 14 | 15 | positions = to_string(sum); 16 | } 17 | 18 | return sum; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 3163. String Compression III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string compressedString(string word) { 4 | int n = word.length(), i = 0; 5 | string ans = ""; 6 | 7 | while(i> 1; 12 | right = right >> 1; 13 | shift++; 14 | } 15 | 16 | return left << shift; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1014. Best Sightseeing Pair.cpp: -------------------------------------------------------------------------------- 1 | // Try to think about rearranged formula, (values[i] + i) + (values[j] - j) instead of values[i] + values[j] + i - j. 2 | 3 | class Solution { 4 | public: 5 | int maxScoreSightseeingPair(vector& values) { 6 | int maxPrev = values[0]; 7 | int ans = INT_MIN; 8 | for(int i=1; i fizzBuzz(int n) { 4 | vector ans; 5 | for(int i=1; i<=n; i++) { 6 | if(i%3 == 0 && i%5 == 0) 7 | ans.push_back("FizzBuzz"); 8 | else if(i%3 == 0) 9 | ans.push_back("Fizz"); 10 | else if(i%5 == 0) 11 | ans.push_back("Buzz"); 12 | else 13 | ans.push_back(to_string(i)); 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 242. Valid Anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if(s.length() != t.length()) 5 | return false; 6 | 7 | vector> map(26); 8 | 9 | for(int i=0;i& nums) { 4 | unordered_map m; 5 | int ans = -1; 6 | 7 | for(int i=0;i 0) 9 | m[nums[i]] = 1; 10 | 11 | for(int i=0;i ans) 13 | ans = abs(nums[i]); 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2108. Find First Palindromic String in the Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string firstPalindrome(vector& words) { 4 | for(string &i:words){ 5 | int x = 0, y = i.length() - 1; 6 | int flag = 0; 7 | while(x& nums) { 4 | int n = nums.size(); 5 | int maxElem = nums[0]; 6 | long long maxDiff = 0, triplet = 0; 7 | 8 | for(int i=1; i mp; 7 | while(j < s.length()) { 8 | mp[s[j]]++; 9 | 10 | while(mp[s[j]] > 1) { 11 | mp[s[i]]--; 12 | i++; 13 | } 14 | 15 | ans = max(ans, j-i+1); 16 | j++; 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Recursion/[C++] [CodeStudio] Remove Consecutive Duplicates.cpp: -------------------------------------------------------------------------------- 1 | string removeDuplicate(string &s) 2 | { 3 | //base case 4 | if(s.length() == 1) 5 | return s; 6 | 7 | else 8 | { 9 | //storing the next elements for comparison after recursive calls 10 | char next = s[s.length()-1]; 11 | s.pop_back(); 12 | removeDuplicate(s); 13 | 14 | //comparing 15 | if(s[s.length()-1] != next) 16 | { 17 | s.push_back(next); 18 | } 19 | return s; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1287. Element Appearing More Than 25% In Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Since, there is exactly one integer in the array that occurs more than 25% of the time and also the array is sorted. 3 | So, if a ith indexed element is also present in (i + (n/4))th index, then it's the desired number. 4 | */ 5 | 6 | class Solution { 7 | public: 8 | int findSpecialInteger(vector& arr) { 9 | for(int i=0;i& logs) { 4 | int operations = 0; 5 | for(string &log : logs){ 6 | if(log == "../"){ 7 | if(operations == 0) 8 | continue; 9 | else 10 | operations--; 11 | } 12 | 13 | else if(log == "./") 14 | continue; 15 | else 16 | operations++; 17 | } 18 | return operations; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 349. Intersection of Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | vector m(1001, 0); 5 | 6 | for(int &i:nums1){ 7 | if(m[i] <= 0) 8 | m[i]++; 9 | } 10 | 11 | vector ans; 12 | for(int &j:nums2){ 13 | if(m[j] > 0 && m[j] < 2) 14 | { 15 | m[j]++; 16 | ans.push_back(j); 17 | } 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/Binary Search/[C++] 852. Peak Index in a Mountain Array.cpp: -------------------------------------------------------------------------------- 1 | // O(log(n)) [n = size of array] 2 | 3 | class Solution { 4 | public: 5 | int peakIndexInMountainArray(vector& a) { 6 | int start = 0; 7 | int end = a.size()-1; 8 | int i = start +(end-start)/2; 9 | while(starta[i+1]){ 11 | end = i; 12 | } 13 | else{ 14 | start = i+1; 15 | } 16 | i = start +(end-start)/2; 17 | } 18 | return i; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Bit Manipulation/[C++] 2220. Minimum Bit Flips to Convert Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minBitFlips(int start, int goal) { 4 | int ans = 0; 5 | while(start || goal) { 6 | int startEndBit = start & 1; 7 | int goalEndBit = goal & 1; 8 | 9 | // cout<>= 1; 14 | goal >>= 1; 15 | } 16 | 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 1624. Largest Substring Between Two Equal Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxLengthBetweenEqualCharacters(string s) { 4 | vector lastOccur(26, -1); 5 | 6 | int ans = -1; 7 | for(int i=0;i& message, vector& bannedWords) { 4 | unordered_map mp; 5 | for(auto &word : bannedWords) 6 | mp[word] = true; 7 | 8 | int count = 0; 9 | for(auto &word : message) { 10 | if(mp.find(word) != mp.end()) 11 | count++; 12 | 13 | if(count >= 2) 14 | return true; 15 | } 16 | 17 | return false; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 560. Subarray Sum Equals K.cpp: -------------------------------------------------------------------------------- 1 | // Hashmap + Prefix Sum 2 | 3 | class Solution { 4 | public: 5 | int subarraySum(vector& nums, int k) { 6 | unordered_map m; 7 | m[0] = 1; 8 | 9 | int pSum = 0; 10 | int ans = 0; 11 | for(int i=0;i& nums) { 4 | int n = nums.size(), ans = 0; 5 | 6 | for(int i=0; i<=n-3; i++) { 7 | int first = nums[i]; 8 | int second = nums[i+1]; 9 | int third = nums[i+2]; 10 | 11 | if((first+third)*2 == second){ 12 | ans++; 13 | } 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 58. Length of Last Word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | bool spaceBeforeWord = false; 5 | int ans = 0; 6 | for(int i=s.length()-1;i>=0;i--){ 7 | if(!spaceBeforeWord && s[i] == ' ') 8 | continue; 9 | else if(spaceBeforeWord && s[i] == ' ') 10 | return ans; 11 | else{ 12 | spaceBeforeWord = true; 13 | ans++; 14 | } 15 | } 16 | 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Recursion/[C++] [CodeStudio] Family Structure.cpp: -------------------------------------------------------------------------------- 1 | bool solve(long long int k) 2 | { 3 | //base case (when reaches akash i.e. MALE) 4 | if(k == 1) 5 | return 1; 6 | 7 | //parent's gender = solve(k+1)/2; 8 | //if k is even, then it is opposite to its's parent gender 9 | if(!(k & 1)) 10 | return 1-solve((k+1)/2); 11 | 12 | //if k is odd, then it is same as of it's parent. 13 | else 14 | return solve((k+1)/2); 15 | } 16 | 17 | string kthChildNthGeneration(int n, long long int k) 18 | { 19 | if(solve(k)) 20 | return "Male"; 21 | 22 | else 23 | return "Female"; 24 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 2483. Minimum Penalty for a Shop.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bestClosingTime(string customers) { 4 | int index = -1, maxi = 0, score = 0; 5 | for(int i=0;i maxi) 13 | { 14 | maxi = score; 15 | index = i; 16 | } 17 | } 18 | return index+1; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 930. Binary Subarrays With Sum.cpp: -------------------------------------------------------------------------------- 1 | // Similar to 560 [Cummulative Sum (Prefix Sum) + Map] 2 | 3 | class Solution { 4 | public: 5 | int numSubarraysWithSum(vector& nums, int goal) { 6 | unordered_map m; 7 | m[0] = 1; 8 | 9 | int pSum = 0, ans = 0; 10 | for(int i=0;i frequencySort(vector& nums) { 5 | unordered_map mp; 6 | for(int &num : nums){ 7 | mp[num]++; 8 | } 9 | 10 | auto lambda = [&](int &n1, int &n2){ 11 | if(mp[n1] == mp[n2]) 12 | return n1 > n2; 13 | 14 | return mp[n1] < mp[n2]; 15 | }; 16 | 17 | sort(begin(nums), end(nums), lambda); 18 | return nums; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2073. Time Needed to Buy Tickets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int timeRequiredToBuy(vector& tickets, int k) { 4 | int ans = 0, i=0, n = tickets.size(); 5 | 6 | while(true){ 7 | if(tickets[i%n] < 1){ 8 | i++; 9 | continue; 10 | } 11 | else{ 12 | tickets[i%n]--; 13 | ans++; 14 | } 15 | 16 | if(tickets[k] == 0) 17 | break; 18 | i++; 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 1331. Rank Transform of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector arrayRankTransform(vector& arr) { 4 | vector temp = arr; 5 | sort(begin(temp), end(temp)); 6 | 7 | unordered_map mp; 8 | int rank = 1; 9 | for(int &i : temp) { 10 | if(mp.find(i) == mp.end()){ 11 | mp[i] = rank++; 12 | } 13 | } 14 | 15 | for(int i=0; i& nums, int k) { 4 | int n = nums.size(), i = 0, j = 0, ans = INT_MIN; 5 | unordered_map m; 6 | 7 | while(j k){ 12 | m[nums[i]]--; 13 | i++; 14 | } 15 | 16 | ans = max(ans, (j-i+1)); 17 | j++; 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 2825. Make String a Subsequence Using Cyclic Increments.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canMakeSubsequence(string str1, string str2) { 4 | int j = 0; 5 | for(int i=0; i& nums) { 11 | int n = nums.size(); 12 | int result = n; 13 | 14 | for(int i=0;i maxCost){ 12 | currCost -= abs(s[i] - t[i]); 13 | i++; 14 | } 15 | 16 | ans = max(ans, j-i+1); 17 | j++; 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/Sliding Window/[C++] 209. Minimum Size Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int target, vector& nums) { 4 | int n = nums.size(); 5 | int i = 0, j = 0, currSum = 0, ans = n+1; 6 | 7 | while(j= target) { 11 | ans = min(ans, j-i+1); 12 | currSum -= nums[i]; 13 | i++; 14 | } 15 | 16 | j++; 17 | } 18 | 19 | return ans == n+1 ? 0 : ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 1408. String Matching in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector stringMatching(vector& words) { 4 | int n = words.size(); 5 | vector ans; 6 | 7 | for(int i=0; i= numExchange){ 9 | ans += numBottles / numExchange; 10 | int consumedBottles = numBottles / numExchange; 11 | int leftBottles = numBottles % numExchange; 12 | 13 | numBottles = consumedBottles + leftBottles; 14 | } 15 | 16 | ans += initialBottles; 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Queue/[C++] [GFG] Reverse First K elements of Queue.cpp: -------------------------------------------------------------------------------- 1 | queue modifyQueue(queue q, int k) { 2 | 3 | //Using Stack to reverse first 'k' elements 4 | stack st; 5 | for(int i=0;i& nums) { 4 | int n = nums.size(), i = 0, j = 0; 5 | vector rightMax(n); 6 | rightMax[n-1] = nums[n-1]; 7 | for(int i=n-2; i>=0; i--) 8 | rightMax[i] = max(rightMax[i+1], nums[i]); 9 | 10 | int ans = 0; 11 | while(j < n) { 12 | while(i < j && nums[i] > rightMax[j]) 13 | i++; 14 | 15 | ans = max(ans, j-i); 16 | j++; 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Heap/[C++] 2558. Take Gifts From the Richest Pile.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long pickGifts(vector& gifts, int k) { 4 | priority_queue pq; // max-heap 5 | 6 | for(int &gift : gifts) 7 | pq.push(gift); 8 | 9 | while(k-- && !pq.empty()) { 10 | int n = pq.top(); 11 | pq.pop(); 12 | 13 | pq.push(floor(sqrt(n))); 14 | } 15 | 16 | long long ans = 0; 17 | while(!pq.empty()) { 18 | ans += pq.top(); 19 | pq.pop(); 20 | } 21 | 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2000. Reverse Prefix of Word.cpp: -------------------------------------------------------------------------------- 1 | // Two Pointer 2 | 3 | class Solution { 4 | public: 5 | string reversePrefix(string word, char ch) { 6 | int n = word.length(); 7 | int i = 0, j; 8 | while(i mp; 4 | int maxCount(vector& banned, int n, int maxSum) { 5 | for(int &i : banned) { 6 | if(i <= n) 7 | mp[i] = true; 8 | } 9 | 10 | int currSum = 0; 11 | int ans = 0; 12 | for(int i=1; i<=n; i++) { 13 | if(mp.find(i) == mp.end() && currSum + i <= maxSum) { 14 | ans++; 15 | currSum += i; 16 | } 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Dynamic Programming/[C++] 343. Integer Break.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[59]; 4 | 5 | int solve(int n){ 6 | if(n == 1) 7 | return 1; 8 | 9 | if(dp[n] != -1) 10 | return dp[n]; 11 | 12 | int ans = INT_MIN; 13 | for(int i=1;i<=n-1;i++) 14 | { 15 | int prod = i * max(n-i, solve(n-i)); 16 | ans = max(ans, prod); 17 | } 18 | 19 | return dp[n] = ans; 20 | } 21 | int integerBreak(int n) { 22 | memset(dp, -1, sizeof(dp)); 23 | return solve(n); 24 | } 25 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 13. Roman to Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | unordered_map m; 4 | int romanToInt(string s) { 5 | m['I'] = 1; 6 | m['V'] = 5; 7 | m['X'] = 10; 8 | m['L'] = 50; 9 | m['C'] = 100; 10 | m['D'] = 500; 11 | m['M'] = 1000; 12 | 13 | int ans = 0; 14 | int n = s.length()-1; 15 | for(int i=n;i>=0;i--){ 16 | if(i count1(26, 0), count2(26, 0); 5 | for(int i=0;i count1[i]) 13 | continue; 14 | 15 | else 16 | ans += count1[i] - count2[i]; 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1464. Maximum Product of Two Elements in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | int n = nums.size(); 5 | int largest = nums[0]; 6 | int second_largest = 0; 7 | 8 | for(int i=1;i= largest){ 10 | second_largest = largest; 11 | largest = nums[i]; 12 | } 13 | else if(nums[i] >= second_largest){ 14 | second_largest = nums[i]; 15 | } 16 | } 17 | 18 | return (largest-1)*(second_largest-1); 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 3005. Count Elements With Maximum Frequency.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxFrequencyElements(vector& nums) { 4 | int maxFreq = INT_MIN, freqCount; 5 | vector freq(101, 0); 6 | 7 | for(int &i:nums){ 8 | freq[i]++; 9 | if(maxFreq < freq[i]){ 10 | maxFreq = freq[i]; 11 | // reset count 12 | freqCount = 1; 13 | } 14 | else if(maxFreq == freq[i]){ 15 | freqCount++; 16 | } 17 | } 18 | 19 | return maxFreq*freqCount; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 1400. Construct K Palindrome Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string s, int k) { 4 | int n = s.length(); 5 | if(n < k) 6 | return false; 7 | 8 | vector charCount(26, 0); 9 | for(char &ch : s) 10 | charCount[ch - 'a']++; 11 | 12 | int oddFreq = 0; 13 | for(int i=0; i<26; i++) 14 | if(charCount[i] & 1) 15 | oddFreq++; 16 | 17 | if(oddFreq > k) 18 | return false; 19 | else 20 | return true; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/Sliding Window/[C++] 1574. Shortest Subarray to be Removed to Make Array Sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLengthOfShortestSubarray(vector& arr) { 4 | int n = arr.size(); 5 | 6 | int j = n-1; 7 | while(j > 0 && arr[j] >= arr[j-1]) 8 | j--; 9 | 10 | int i = 0; 11 | int ans = j; 12 | while(i= arr[i-1])) { 13 | while(j& nums) { 4 | int ans = 0; 5 | sort(nums.begin(), nums.end()); 6 | 7 | for(int i=1; i nums[i]){ 13 | ans += nums[i-1] - nums[i] + 1; 14 | nums[i] = nums[i-1] + 1; 15 | } 16 | } 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 1128. Number of Equivalent Domino Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numEquivDominoPairs(vector>& dominoes) { 4 | unordered_map mp; 5 | 6 | int ans = 0; 7 | for(int i=0; i temp = {dominoes[i][0], dominoes[i][1]}; 9 | sort(begin(temp), end(temp)); 10 | 11 | int key = temp[0]*10 + temp[1]; 12 | 13 | if(mp.find(key) != mp.end()) 14 | ans += mp[key]; 15 | 16 | mp[key]++; 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 2501. Longest Square Streak in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSquareStreak(vector& nums) { 4 | sort(begin(nums), end(nums)); 5 | 6 | unordered_map mp; 7 | int ans = 0; 8 | for(int &num : nums) { 9 | int root = (int)sqrt(num); 10 | 11 | if(root*root == num && mp.find(root) != mp.end()) 12 | mp[num] = mp[root] + 1; 13 | else 14 | mp[num] = 1; 15 | 16 | ans = max(ans, mp[num]); 17 | } 18 | 19 | return ans < 2 ? -1 : ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Sliding Window/[C++] 2779. Maximum Beauty of an Array After Applying Operation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumBeauty(vector& nums, int k) { 4 | int n = nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | 7 | // The problem becomes the following: find maximum subarray A[i … j] such that A[j] - A[i] ≤ 2 * k. 8 | int i = 0, j = 0, ans = 0; 9 | while(j 2*k) 11 | i++; 12 | 13 | ans = max(ans, j-i+1); 14 | j++; 15 | } 16 | 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Stack/[C++] [CodeStudio] Reverse Stack Using Recursion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | T.C -> O(n^2) 3 | S>C -> O(n) 4 | */ 5 | 6 | void insertAtBottom(stack &stack, int x) 7 | { 8 | if(stack.empty()) 9 | { 10 | stack.push(x); 11 | return; 12 | } 13 | int e = stack.top(); 14 | stack.pop(); 15 | insertAtBottom(stack, x); 16 | stack.push(e); 17 | } 18 | 19 | void reverseStack(stack &stack) { 20 | // Write your code here 21 | if(stack.empty()) 22 | { 23 | return; 24 | } 25 | int e = stack.top(); 26 | stack.pop(); 27 | reverseStack(stack); 28 | insertAtBottom(stack, e); 29 | } -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [Matrix] 2022. Convert 1D Array Into 2D Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> construct2DArray(vector& original, int m, int n) { 4 | if(original.size() != m*n) 5 | return {}; 6 | 7 | int row = 0, col = 0; 8 | vector> ans(m, vector(n, 0)); 9 | for(int i=0; i& nums) { 4 | int n = nums.size(); 5 | unordered_map products; 6 | 7 | int ans = 0; 8 | for(int i=0; i& gas, vector& cost) { 4 | int deficit = 0; 5 | int balance = 0; 6 | int start = 0; 7 | 8 | for(int i=0;i= 0) 19 | return start; 20 | else 21 | return -1; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 135. Candy.cpp: -------------------------------------------------------------------------------- 1 | //Prefix Sum and Suffix Sum 2 | 3 | class Solution { 4 | public: 5 | int candy(vector& ratings) { 6 | int n = ratings.size(); 7 | vector candy(n, 1); 8 | 9 | for(int i=1;i ratings[i-1]) 12 | candy[i] = candy[i-1] + 1; 13 | } 14 | 15 | for(int i=n-2;i>=0;i--) 16 | { 17 | if(ratings[i] > ratings[i+1] && candy[i] <= candy[i+1]) 18 | candy[i] = candy[i+1] + 1; 19 | } 20 | 21 | return accumulate(candy.begin(), candy.end(), 0); 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1894. Find the Student that Will Replace the Chalk.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int chalkReplacer(vector& chalk, int k) { 4 | int n = chalk.size(); 5 | long long prefixSum = chalk[0]; 6 | for(int i=1; i=0; i--){ 16 | sum -= chalk[i]; 17 | if(sum <= k) 18 | return i; 19 | } 20 | 21 | return -1; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/Sorting and Searching/[C++] 2966. Divide Array Into Arrays With Max Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> divideArray(vector& nums, int k) { 4 | vector> ans; 5 | int n = nums.size(); 6 | 7 | sort(begin(nums), end(nums)); 8 | 9 | for(int i=0;i temp(begin(nums)+i, begin(nums)+i+3); 12 | ans.push_back(temp); 13 | } 14 | else{ 15 | return {}; 16 | } 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 205. Isomorphic Strings.cpp: -------------------------------------------------------------------------------- 1 | // check whether there is a one-to-one correspondence between s & t 2 | 3 | class Solution { 4 | public: 5 | bool isIsomorphic(string s, string t) { 6 | unordered_map m1, m2; 7 | 8 | for(int i=0;i n = (n & (n-1)) 3 | class Solution { 4 | public: 5 | int hammingWeight(uint32_t n) { 6 | int count = 0; 7 | while(n){ 8 | n &= n-1; 9 | count++; 10 | } 11 | return count; 12 | } 13 | }; 14 | 15 | 16 | //Approach 2 17 | /* 18 | class Solution { 19 | public: 20 | int hammingWeight(uint32_t n) { 21 | int count = 0; 22 | while(n){ 23 | count += n&1; 24 | n = n>>1; 25 | } 26 | return count; 27 | } 28 | }; 29 | */ -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 624. Maximum Distance in Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDistance(vector>& arrays) { 4 | int prevMin = arrays[0][0]; 5 | int prevMax = arrays[0][arrays[0].size()-1]; 6 | 7 | int ans = INT_MIN; 8 | for(int i=1; i bob; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] [String] 6. Zigzag Conversion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convert(string s, int numRows) { 4 | if(numRows <= 1) 5 | return s; 6 | 7 | vectorv(numRows, ""); 8 | 9 | int j = 0, dir = -1; 10 | 11 | for(int i = 0; i < s.length(); i++) 12 | { 13 | 14 | if(j == numRows - 1 || j == 0) 15 | dir *= (-1); 16 | 17 | v[j] += s[i]; 18 | 19 | if(dir == 1) 20 | j++; 21 | else 22 | j--; 23 | } 24 | 25 | string res; 26 | for(auto &it : v) 27 | res += it; 28 | 29 | return res; 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /LeetCode/Dynamic Programming/[C++] 279. Perfect Squares.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[10001]; 4 | 5 | int solve(int n){ 6 | if(n == 0) 7 | return 0; 8 | 9 | if(dp[n] != -1) 10 | return dp[n]; 11 | 12 | int ans = INT_MAX; 13 | 14 | for(int i=1;i<=sqrt(n);i++){ 15 | int square = i*i; 16 | int ways = 1 + solve(n-square); 17 | ans = min(ans, ways); 18 | } 19 | 20 | return dp[n] = ans; 21 | } 22 | int numSquares(int n) { 23 | memset(dp, -1, sizeof(dp)); 24 | return solve(n); 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode/Hashmap/[C++] 645. Set Mismatch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector& nums) { 4 | int n = nums.size(); 5 | vector map(n+1); 6 | int totalSum = (n*(n+1))/2; 7 | int temp = 0; 8 | int tempSum = 0; 9 | 10 | for(int i=0;i 1) 13 | { 14 | temp = nums[i]; 15 | continue; 16 | } 17 | else{ 18 | tempSum += nums[i]; 19 | } 20 | } 21 | 22 | return {temp, totalSum-tempSum}; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode/Stack/[C++] 739. Daily Temperatures.cpp: -------------------------------------------------------------------------------- 1 | // Variation of Next Greater Element 2 | class Solution { 3 | public: 4 | vector dailyTemperatures(vector& temperatures) { 5 | int n = temperatures.size(); 6 | stack> st; 7 | vector ans(n, 0); 8 | 9 | for(int i=n-1;i>=0;i--){ 10 | while(!st.empty() && st.top().first <= temperatures[i]) 11 | st.pop(); 12 | 13 | if(!st.empty()) 14 | ans[i] = st.top().second - i; 15 | 16 | st.push({temperatures[i], i}); 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Recursion/[C++] [CodeStudio] Subsequences of String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void subseq(string &str, vector &ans, string output, int i){ 3 | if(i >= str.length()) 4 | { 5 | if(!output.empty()) 6 | { 7 | ans.push_back(output); 8 | } 9 | return; 10 | } 11 | //exclude 12 | subseq(str, ans, output, i+1); 13 | 14 | //include 15 | char element = str[i]; 16 | output.push_back(element); 17 | subseq(str, ans, output, i+1); 18 | } 19 | vector subsequences(string str){ 20 | // Write your code here 21 | vector ans; 22 | string output =""; 23 | int index=0; 24 | subseq(str, ans, output, index); 25 | return ans; 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 1512. Number of Good Pairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Use of Combination 3 | No. of ways to make pair (2 element) from n elements -> nCr , here r=2, => nC2 4 | 5 | nC2 => n! / (2! * (n-2)!) => (n * (n-1)!) / 2 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int numIdenticalPairs(vector& nums) { 11 | unordered_map m; 12 | for(int i:nums) 13 | m[i]++; 14 | 15 | int ans = 0; 16 | for(auto i:m) 17 | { 18 | if(i.second > 1) 19 | { 20 | ans += (i.second*(i.second-1))/2; 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode/Arrays/[C++] 2706. Buy Two Chocolates.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int buyChoco(vector& prices, int money) { 4 | int smallest = INT_MAX, second_smallest = 0; 5 | 6 | for(int i=0;i