├── Day-05 (Linked List Part - 1) ├── Delete a given node.cpp ├── Middle of the Linked List.cpp ├── Remove nth node from the end of LL.cpp ├── Reverse a LinkedList.cpp ├── Add two numbers.cpp └── Merge two sorted lists.cpp ├── Day-03 (Arrays Part - 3) ├── Majority Element.cpp ├── POW(X, N).cpp ├── Majority Elements 2.cpp ├── Unique Paths.cpp ├── Search in a 2D Matrix.cpp └── Reverse pairs.cpp ├── README.md ├── Day-02 (Arrays Part - 2) ├── Find the missing and repeated number.cpp ├── Rotate Matrix.cpp ├── Merge Sorted Array.cpp ├── Find the Duplicate Number.cpp ├── Count inversion in an array.cpp └── Merge Intervals.cpp ├── Day-06 (Linked List Part - 2) ├── Detect a cycle in LL.cpp └── Find intersection point of Y LL.cpp ├── Day-04 (Arrays Part - 4) ├── Subarrays with given XOR.cpp ├── Largest Subarry with Sum 0.cpp ├── 2-Sum.cpp ├── Longest Consecutive Sequence.cpp └── 4-Sum.cpp └── Day-01 (Arrays Part - 1) ├── Pascal's Triangle.cpp ├── Maximum Subarray.cpp ├── Stock buy and sell.cpp ├── Sort the colors.cpp ├── Set Matrix Zeroes.cpp └── Next Permutation.cpp /Day-05 (Linked List Part - 1)/Delete a given node.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/delete-node-in-a-linked-list/ 2 | 3 | class Solution 4 | { 5 | public: 6 | void deleteNode(ListNode *node) 7 | { 8 | ListNode *nextNode = node->next; 9 | 10 | // Changing the address stored into the 11 | *node = *nextNode; 12 | 13 | delete nextNode; 14 | } 15 | }; 16 | 17 | // Time Complexity - O(1) 18 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-03 (Arrays Part - 3)/Majority Element.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/majority-element/ 2 | 3 | class Solution 4 | { 5 | public: 6 | int majorityElement(vector &nums) 7 | { 8 | unordered_map mp; 9 | int ans = 0; 10 | 11 | for (int itt : nums) 12 | { 13 | mp[itt]++; 14 | if (mp[itt] > nums.size() / 2) 15 | return itt; 16 | } 17 | 18 | return -1; 19 | } 20 | }; 21 | 22 | // Time Complexity - O(N) 23 | // Space Complexity - O(N) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Striver SDE Sheet - 30 Days Challenge 2 | SDE Sheet contains very handily crafted and picked top coding interview questions from different topics of Data Structures & Algorithms. These questions are one of the most asked coding interview questions in coding interviews of companies like Amazon, Microsoft, Media.net, Flipkart, etc, and cover almost all of the concepts related to Data Structure & Algorithms. 3 | 4 | ### [Link to the sheet](https://takeuforward.org/interviews/strivers-sde-sheet-top-coding-interview-problems/) 5 | -------------------------------------------------------------------------------- /Day-03 (Arrays Part - 3)/POW(X, N).cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/powx-n/ 2 | 3 | class Solution 4 | { 5 | public: 6 | double myPow(double x, int n) 7 | { 8 | if (n == 0) 9 | return 1; 10 | 11 | if (n == INT_MIN) 12 | { 13 | x = x * x; 14 | n /= 2; 15 | } 16 | 17 | if (n < 0) 18 | { 19 | n = -1 * n; 20 | x = 1 / x; 21 | } 22 | 23 | return (n % 2 == 0) ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2); 24 | } 25 | }; 26 | 27 | // Time Complexity - O(log(N)) 28 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-05 (Linked List Part - 1)/Middle of the Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/middle-of-the-linked-list/ 2 | 3 | class Solution 4 | { 5 | public: 6 | ListNode *middleNode(ListNode *head) 7 | { 8 | ListNode *slow, *fast; 9 | slow = head; 10 | fast = head; 11 | 12 | while (fast && fast->next) 13 | { 14 | slow = slow->next; 15 | fast = fast->next->next; 16 | } 17 | 18 | return slow; 19 | } 20 | }; 21 | 22 | // Time Complexity - O(n) 23 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-02 (Arrays Part - 2)/Find the missing and repeated number.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://bit.ly/3Gs6wZu 2 | 3 | pair missingAndRepeating(vector &arr, int n) 4 | { 5 | unordered_map mp; 6 | int missing; 7 | int repeated; 8 | 9 | for (int i=0; inext) 13 | { 14 | slow = slow->next; 15 | fast = fast->next->next; 16 | 17 | if (slow == fast) 18 | return 1; 19 | } 20 | 21 | return 0; 22 | } 23 | }; 24 | 25 | // Time Complexity - O(n) 26 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-03 (Arrays Part - 3)/Majority Elements 2.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/majority-element-ii/ 2 | 3 | class Solution 4 | { 5 | public: 6 | vector majorityElement(vector &nums) 7 | { 8 | unordered_map freq; 9 | int check = nums.size() / 3; 10 | vector ans; 11 | 12 | for (int num : nums) 13 | { 14 | if (freq[num] != -1) 15 | freq[num]++; 16 | 17 | if (freq[num] > check) 18 | { 19 | freq[num] = -1; 20 | ans.push_back(num); 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | }; 27 | 28 | // Time Complexity - O(N) 29 | // Space Complexity - O(N) -------------------------------------------------------------------------------- /Day-03 (Arrays Part - 3)/Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/unique-paths 2 | 3 | class Solution 4 | { 5 | public: 6 | int uniquePaths(int m, int n) 7 | { 8 | vector> dp(m, vector(n, 0)); 9 | 10 | for (int i = 0; i < m; i++) 11 | dp[i][0] = 1; 12 | 13 | for (int i = 0; i < n; i++) 14 | dp[0][i] = 1; 15 | 16 | for (int i = 1; i < m; i++) 17 | { 18 | for (int j = 1; j < n; j++) 19 | { 20 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 21 | } 22 | } 23 | 24 | return dp[m - 1][n - 1]; 25 | } 26 | }; 27 | 28 | // Time Complexity - O(m*n) 29 | // Space Complexity - O(m*n) -------------------------------------------------------------------------------- /Day-04 (Arrays Part - 4)/Subarrays with given XOR.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://www.interviewbit.com/problems/subarray-with-given-xor/ 2 | 3 | int Solution::solve(vector &A, int B) 4 | { 5 | int currPrefXor = 0, count = 0; 6 | unordered_map subArray; 7 | 8 | for (int i = 0; i < A.size(); i++) 9 | { 10 | currPrefXor ^= A[i]; 11 | int prevPrefXor = currPrefXor ^ B; 12 | 13 | if (currPrefXor == B) 14 | count++; 15 | 16 | count += subArray[prevPrefXor]; 17 | subArray[currPrefXor]++; 18 | } 19 | 20 | return count; 21 | } 22 | 23 | // Time Complexity - O(n) 24 | // Space Complexity - O(n) -------------------------------------------------------------------------------- /Day-01 (Arrays Part - 1)/Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/pascals-triangle/ 2 | 3 | class Solution 4 | { 5 | public: 6 | vector> generate(int numRows) 7 | { 8 | vector> ans; 9 | 10 | for (int i = 0; i < numRows; i++) 11 | { 12 | // Initializing the row with 1s 13 | vector row(i + 1, 1); 14 | 15 | for (int j = 1; j < i; j++) 16 | { 17 | // Taking the sum of the previous row and previous column elements 18 | row[j] = (ans[i - 1][j] + ans[i - 1][j - 1]); 19 | } 20 | 21 | ans.push_back(row); 22 | } 23 | 24 | return ans; 25 | } 26 | }; 27 | 28 | // Time Complexity - O(n^2) (n is the number of rows) 29 | // Space Complexity - O(n^2) (n is the number of rows) -------------------------------------------------------------------------------- /Day-01 (Arrays Part - 1)/Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/maximum-subarray/ 2 | 3 | class Solution 4 | { 5 | public: 6 | int maxSubArray(vector &nums) 7 | { 8 | int curr = nums[0], ans = nums[0]; 9 | 10 | for (int i = 1; i < nums.size(); i++) 11 | { 12 | // Variable to store the current contigous sum of elements 13 | // There are two cases i.e 14 | // 1. Current element will added to the contigous subarray sum 15 | // 2. OR the subarray sum is resetted to the current element 16 | curr = max(curr + nums[i], nums[i]); 17 | 18 | // We pick the maximum subarray sum 19 | ans = max(ans, curr); 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | 26 | // Time Complexity - O(n) 27 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-04 (Arrays Part - 4)/Largest Subarry with Sum 0.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://practice.geeksforgeeks.org/problems/largest-subarray-with-0-sum/1 2 | 3 | class Solution 4 | { 5 | public: 6 | int maxLen(vector &a, int n) 7 | { 8 | int len = 0; 9 | int sum = 0; 10 | unordered_map sumTrack; 11 | 12 | for (int i = 0; i < a.size(); i++) 13 | { 14 | sum += a[i]; 15 | 16 | if (sum == 0) 17 | len = i + 1; 18 | 19 | if (sumTrack.find(sum) != sumTrack.end()) 20 | len = max(len, i - sumTrack[sum]); 21 | else 22 | sumTrack[sum] = i; 23 | } 24 | 25 | return len; 26 | } 27 | }; 28 | 29 | // Time Complexity - O(N) 30 | // Space Complexity - O(N) -------------------------------------------------------------------------------- /Day-04 (Arrays Part - 4)/2-Sum.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/two-sum/ 2 | 3 | class Solution 4 | { 5 | public: 6 | vector twoSum(vector &nums, int target) 7 | { 8 | map mp; 9 | 10 | for (int i = 0; i < nums.size(); i++) 11 | { 12 | // If the curr element does not exist in the array 13 | if (mp.find(nums[i]) == mp.end()) 14 | mp[nums[i]] = i; // mp[2] = 0 15 | 16 | // 9 - 2 = 7 17 | // mp[7] = ? 18 | // No - continue 19 | // Yes - return the indexes 20 | if (mp.find(target - nums[i]) != mp.end() && 21 | i != mp[target - nums[i]]) 22 | return {i, mp[target - nums[i]]}; 23 | } 24 | 25 | return {-1, -1}; 26 | } 27 | }; 28 | 29 | // Time Complexity - O(n) 30 | // Space Complexity - O(n) -------------------------------------------------------------------------------- /Day-02 (Arrays Part - 2)/Rotate Matrix.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/rotate-image/submissions/ 2 | 3 | class Solution 4 | { 5 | public: 6 | void rotate(vector> &matrix) 7 | { 8 | // Transpose Matrix -> Rows are reversed = Rotated Matrix 9 | 10 | // Transpose the matrix 11 | for (int i = 0; i < matrix[0].size(); i++) 12 | { 13 | for (int j = i + 1; j < matrix.size(); j++) 14 | { 15 | int temp = matrix[j][i]; 16 | matrix[j][i] = matrix[i][j]; 17 | matrix[i][j] = temp; 18 | } 19 | } 20 | 21 | // Reverse the rows 22 | for (int i = 0; i < matrix.size(); i++) 23 | { 24 | reverse(matrix[i].begin(), matrix[i].end()); 25 | } 26 | } 27 | }; 28 | 29 | // Time Complexity: O(M * N) 30 | // Space Complexity: O(1) -------------------------------------------------------------------------------- /Day-01 (Arrays Part - 1)/Stock buy and sell.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 2 | 3 | class Solution 4 | { 5 | public: 6 | int maxProfit(vector &prices) 7 | { 8 | int minPrice = INT_MAX, profit = 0; 9 | 10 | for (int i = 0; i < prices.size(); i++) 11 | { 12 | // In minPrice variable we store the minimum price 13 | // that we find on each and every iteration 14 | minPrice = min(prices[i], minPrice); 15 | 16 | // We calculate the maximum profit, 17 | // by taking the maximum out of the previous profit or the current profit 18 | // which is calculated by subtracting the current price with the 19 | // minimum recorded price as of the current iteration 20 | profit = max(profit, prices[i] - minPrice); 21 | } 22 | 23 | return profit; 24 | } 25 | }; 26 | 27 | // Time Complexity - O(n) 28 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-05 (Linked List Part - 1)/Remove nth node from the end of LL.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 2 | 3 | class Solution 4 | { 5 | public: 6 | ListNode *removeNthFromEnd(ListNode *head, int n) 7 | { 8 | if (head == NULL) 9 | return NULL; 10 | 11 | ListNode *newHead = new ListNode(-1), *fast = newHead, *slow = newHead; 12 | slow->next = head; 13 | fast->next = head; 14 | 15 | for (int i = 0; i < n; i++) 16 | fast = fast->next; 17 | 18 | while (fast->next) 19 | { 20 | slow = slow->next; 21 | fast = fast->next; 22 | } 23 | 24 | ListNode *temp = slow->next; 25 | 26 | slow->next = slow->next->next; 27 | delete temp; 28 | 29 | return newHead->next; 30 | ; 31 | } 32 | }; 33 | 34 | // Time Complexity - O(n) 35 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-02 (Arrays Part - 2)/Merge Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/merge-sorted-array/ 2 | 3 | class Solution 4 | { 5 | public: 6 | void merge(vector &nums1, int m, vector &nums2, int n) 7 | { 8 | int a = m - 1, b = n - 1, c = n + m - 1; 9 | // 1 2 3 0 0 0 2 5 6 10 | // a c b 11 | 12 | // We start filling the element from the last of the first array 13 | while (a >= 0 && b >= 0) 14 | { 15 | // 1. When last element of nums2 is greater 16 | // 2. When last element of nums1 is greater or equal 17 | if (nums1[a] < nums2[b]) 18 | { 19 | nums1[c] = nums2[b]; 20 | b--; 21 | } 22 | else 23 | { 24 | nums1[c] = nums1[a]; 25 | a--; 26 | } 27 | c--; 28 | } 29 | 30 | while (b >= 0) 31 | { 32 | nums1[c] = nums2[b]; 33 | c--; 34 | b--; 35 | } 36 | } 37 | }; 38 | 39 | // Time Complexity: O(M + N) 40 | // Space Complexity: O(1) -------------------------------------------------------------------------------- /Day-05 (Linked List Part - 1)/Reverse a LinkedList.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/reverse-linked-list/ 2 | 3 | // Iterative Solution 4 | class Solution 5 | { 6 | public: 7 | ListNode *reverseList(ListNode *head) 8 | { 9 | ListNode *nextNode, *prevNode = NULL; 10 | 11 | while (head) 12 | { 13 | nextNode = head->next; 14 | head->next = prevNode; 15 | prevNode = head; 16 | head = nextNode; 17 | } 18 | 19 | return prevNode; 20 | } 21 | }; 22 | 23 | // Time Complexity - O(n) 24 | // Space Complexity - O(1) 25 | 26 | // Recursive Solution 27 | class Solution 28 | { 29 | public: 30 | ListNode *reverseList(ListNode *head) 31 | { 32 | if (head == NULL || head->next == NULL) 33 | return head; 34 | 35 | ListNode *node = reverseList(head->next); 36 | head->next->next = head; 37 | head->next = NULL; 38 | 39 | return node; 40 | } 41 | }; 42 | 43 | // Time Complexity - O(n) 44 | // Space Complexity - O(n) - Recursion Stack () -------------------------------------------------------------------------------- /Day-04 (Arrays Part - 4)/Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/longest-consecutive-sequence/ 2 | 3 | class Solution 4 | { 5 | public: 6 | int longestConsecutive(vector &nums) 7 | { 8 | if (nums.size() <= 1) 9 | return nums.size(); 10 | 11 | // Removing the duplicates ans sorting the nums array 12 | unordered_set numSet; 13 | 14 | for (int num : nums) 15 | numSet.insert(num); 16 | 17 | nums.assign(numSet.begin(), numSet.end()); 18 | 19 | // Initializing variables len and ans with 1 because minimum length of the 20 | // sequence could be 1 i.e a single number and ans will be atleast 1, 21 | // if not zero :p 22 | int len = 1, ans = 0; 23 | 24 | for (int i = 0; i < nums.size(); i++) 25 | { 26 | if (numSet.find(nums[i] - 1) == numSet.end()) 27 | { 28 | int currNum = nums[i]; 29 | len = 1; 30 | 31 | while (numSet.find(currNum + 1) != numSet.end()) 32 | { 33 | currNum += 1; 34 | len++; 35 | } 36 | 37 | ans = max(len, ans); 38 | } 39 | } 40 | 41 | return ans; 42 | } 43 | }; 44 | 45 | // Time Complexity - O(N) 46 | // Space Complexity - O(N) -------------------------------------------------------------------------------- /Day-01 (Arrays Part - 1)/Sort the colors.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/sort-colors/ 2 | 3 | class Solution 4 | { 5 | public: 6 | void sortColors(vector &nums) 7 | { 8 | // lo is responsible for the handling of the 0s 9 | // mid is responsible for the handling of the 1s 10 | // hi is responsible for the handling of the 2s 11 | 12 | // For example, 13 | // Initialization is as follows - 14 | // 2 0 2 1 1 0 15 | // l -> (lo) 16 | // m -> (mid) 17 | // h -> (hi) 18 | int lo = 0, mid = 0, hi = nums.size() - 1; 19 | 20 | while (mid <= hi) 21 | { 22 | // If the mid element is 0, 23 | // then we swap the lo and mid elements 24 | // because lo is responsible for the 0s. 25 | if (nums[mid] == 0) 26 | swap(nums[lo++], nums[mid++]); 27 | 28 | // mid will be incremented as the number is already 1 29 | else if (nums[mid] == 1) 30 | mid++; 31 | 32 | // If the mid element is 2, 33 | // then we swap the hi and mid elements 34 | // because hi is responsible for the 2s. 35 | else 36 | swap(nums[mid], nums[hi--]); 37 | } 38 | } 39 | }; 40 | 41 | // Time Complexity - O(n) 42 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-02 (Arrays Part - 2)/Find the Duplicate Number.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/find-the-duplicate-number/ 2 | 3 | class Solution 4 | { 5 | public: 6 | int findDuplicate(vector &nums) 7 | { 8 | // We make the swaps until we have found the element 9 | // i.e a duplicate of the current element (nums[0]) 10 | 11 | // For example - [1, 3, 4, 2, 2] 12 | 13 | // 1 Iteration 14 | // [1, 3, 4, 2, 2] 15 | // nums[0] = 1 & nums[nums[0]] = 3 16 | // [3, 1, 4, 2, 2] 17 | 18 | // 2 Iteration 19 | // [3, 1, 4, 2, 2] 20 | // nums[0] = 3 & nums[nums[0]] = 2 21 | // [2, 1, 4, 3, 2] 22 | 23 | // 3 Iteration 24 | // [2, 1, 4, 3, 2] 25 | // nums[0] = 2 & nums[nums[0]] = 4 26 | // [4, 1, 2, 3, 2] 27 | 28 | // 4 Iteration 29 | // [4, 1, 2, 3, 2] 30 | // nums[0] = 4 & nums[nums[0]] = 2 31 | // [2, 1, 2, 3, 4] 32 | 33 | // 5 Iteration 34 | // [2, 1, 2, 3, 4] 35 | // nums[0] = 2 & nums[nums[0]] = 2 36 | // NOW, the nums[0] and nums[nums[0]] are equal, 37 | // Therefore, we have found the unique elements 38 | while (nums[0] != nums[nums[0]]) 39 | swap(nums[0], nums[nums[0]]); 40 | 41 | return nums[0]; 42 | } 43 | }; 44 | 45 | // Time Complexity: O(N) 46 | // Space Complexity: O(1) -------------------------------------------------------------------------------- /Day-06 (Linked List Part - 2)/Find intersection point of Y LL.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/intersection-of-two-linked-lists 2 | 3 | class Solution 4 | { 5 | public: 6 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 7 | { 8 | int len1 = 0; 9 | int len2 = 0; 10 | ListNode *temp = headA; 11 | 12 | while (temp) 13 | { 14 | len1++; 15 | temp = temp->next; 16 | } 17 | 18 | temp = headB; 19 | while (temp) 20 | { 21 | len2++; 22 | temp = temp->next; 23 | } 24 | 25 | if (len1 > len2) 26 | { 27 | int diff = len1 - len2; 28 | while (diff--) 29 | { 30 | headA = headA->next; 31 | } 32 | } 33 | else if (len2 > len1) 34 | { 35 | int diff = len2 - len1; 36 | 37 | while (diff--) 38 | { 39 | headB = headB->next; 40 | } 41 | } 42 | 43 | while (headA != headB) 44 | { 45 | headA = headA->next; 46 | headB = headB->next; 47 | } 48 | 49 | return headA; 50 | } 51 | }; 52 | 53 | // Time Complexity - O(m + n) 54 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-03 (Arrays Part - 3)/Search in a 2D Matrix.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/search-a-2d-matrix/ 2 | 3 | class Solution 4 | { 5 | public: 6 | bool binarySearch(vector vec, int target) 7 | { 8 | int mid, l = 0, r = vec.size() - 1; 9 | while (l <= r) 10 | { 11 | mid = (l + r) / 2; 12 | if (vec[mid] == target) 13 | return true; 14 | else if (vec[mid] < target) 15 | l = mid + 1; 16 | else 17 | r = mid - 1; 18 | } 19 | return false; 20 | } 21 | bool searchMatrix(vector> &matrix, int target) 22 | { 23 | if (matrix.size() == 1 && matrix[0].size() == 1) 24 | return target == matrix[0][0]; 25 | else if (target >= matrix[0][0] && matrix.size() == 1) 26 | return binarySearch(matrix[0], target); 27 | else if (target >= matrix[0][0] && target < matrix[1][0]) 28 | return binarySearch(matrix[0], target); 29 | else 30 | { 31 | for (int i = 1; i < matrix.size() - 1; i++) 32 | { 33 | if (target == matrix[i][0]) 34 | return true; 35 | else if (target > matrix[i][0] && target < matrix[i + 1][0]) 36 | { 37 | return binarySearch(matrix[i], target); 38 | } 39 | } 40 | return binarySearch(matrix[matrix.size() - 1], target); 41 | } 42 | return false; 43 | } 44 | }; 45 | 46 | // Time Complexity - O(N) 47 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-05 (Linked List Part - 1)/Add two numbers.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/add-two-numbers/ 2 | 3 | class Solution 4 | { 5 | public: 6 | ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) 7 | { 8 | if (l1 == NULL) 9 | return l2; 10 | if (l2 == NULL) 11 | return l1; 12 | 13 | int carry = 0, sum = 0; 14 | ListNode *head = l1, *prev; 15 | 16 | while (l1 && l2) 17 | { 18 | if (l1 && l2) 19 | { 20 | sum = (l1->val + l2->val + carry); 21 | carry = sum / 10; 22 | 23 | l1->val = sum % 10; 24 | 25 | prev = l1; 26 | l1 = l1->next; 27 | l2 = l2->next; 28 | } 29 | } 30 | 31 | while (l1) 32 | { 33 | sum = l1->val + carry; 34 | l1->val = sum % 10; 35 | carry = sum / 10; 36 | prev = l1; 37 | l1 = l1->next; 38 | } 39 | 40 | prev->next = l2; 41 | 42 | while (l2) 43 | { 44 | sum = l2->val + carry; 45 | l2->val = sum % 10; 46 | carry = sum / 10; 47 | prev = l2; 48 | l2 = l2->next; 49 | } 50 | 51 | while (carry) 52 | { 53 | prev->next = new ListNode(carry); 54 | prev = prev->next; 55 | carry /= 10; 56 | } 57 | 58 | return head; 59 | } 60 | }; 61 | 62 | // Time Complexity - O(n) 63 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-05 (Linked List Part - 1)/Merge two sorted lists.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/merge-two-sorted-lists 2 | 3 | class Solution 4 | { 5 | public: 6 | ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) 7 | { 8 | if (list1 == NULL) 9 | return list2; 10 | if (list2 == NULL) 11 | return list1; 12 | 13 | ListNode *curr1 = list1, *curr2 = list2, *prev = NULL, *temp; 14 | 15 | while (curr1 && curr2) 16 | { 17 | if (curr1->val > curr2->val) 18 | { 19 | if (prev) 20 | prev->next = curr2; 21 | prev = curr2; 22 | curr2 = curr2->next; 23 | } 24 | else if (curr1->val < curr2->val) 25 | { 26 | if (prev) 27 | prev->next = curr1; 28 | prev = curr1; 29 | curr1 = curr1->next; 30 | } 31 | else 32 | { 33 | if (prev) 34 | prev->next = curr1; 35 | 36 | prev = curr2; 37 | temp = curr1->next; 38 | curr1->next = curr2; 39 | curr1 = temp; 40 | curr2 = curr2->next; 41 | } 42 | } 43 | 44 | if (curr1) 45 | prev->next = curr1; 46 | if (curr2) 47 | prev->next = curr2; 48 | 49 | return list1->val <= list2->val ? list1 : list2; 50 | } 51 | }; 52 | 53 | // Time Complexity - O(n) 54 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-02 (Arrays Part - 2)/Count inversion in an array.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://bit.ly/339fFb7 2 | 3 | #include 4 | long long merge(long long arr[], long long temp[], long long left, long long mid, long long right) 5 | { 6 | long long invs = 0; 7 | long long i = left; 8 | long long j = mid; 9 | long long k = left; 10 | 11 | while ((i <= mid - 1) && (j <= right)) 12 | { 13 | if (arr[i] <= arr[j]) 14 | temp[k++] = arr[i++]; 15 | else 16 | { 17 | temp[k++] = arr[j++]; 18 | invs += mid - i; 19 | } 20 | } 21 | 22 | while (i <= mid - 1) 23 | temp[k++] = arr[i++]; 24 | 25 | while (j <= right) 26 | temp[k++] = arr[j++]; 27 | 28 | for (i = left; i <= right; i++) 29 | arr[i] = temp[i]; 30 | 31 | return invs; 32 | } 33 | 34 | long long mergeSort(long long arr[], long long temp[], long long left, long long right) 35 | { 36 | long long mid; 37 | long long invs = 0; 38 | 39 | if (right > left) 40 | { 41 | mid = (left + right) / 2; 42 | 43 | invs += mergeSort(arr, temp, left, mid); 44 | invs += mergeSort(arr, temp, mid + 1, right); 45 | 46 | invs += merge(arr, temp, left, mid + 1, right); 47 | } 48 | 49 | return invs; 50 | } 51 | 52 | long long getInversions(long long *arr, int n) 53 | { 54 | // We basically find the inversion pairs while merging the subarrays 55 | // and store the inversion count in invs. 56 | long long temp[n]; 57 | long long ans = mergeSort(arr, temp, 0, n - 1); 58 | 59 | return ans; 60 | } 61 | 62 | // Time Complexity: O(N * log(N)) 63 | // Space Complexity: O(N) -------------------------------------------------------------------------------- /Day-04 (Arrays Part - 4)/4-Sum.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/4sum 2 | 3 | class Solution 4 | { 5 | public: 6 | vector> fourSum(vector &nums, int target) 7 | { 8 | vector> ans; 9 | sort(nums.begin(), nums.end()); 10 | 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | for (int j = i + 1; j < nums.size(); j++) 14 | { 15 | long long reqNum = (long long)target - ((long long)nums[i] + (long long)nums[j]); 16 | 17 | // 2 - Pointers search 18 | int lo = j + 1, hi = nums.size() - 1; 19 | while (lo < hi) 20 | { 21 | int check = nums[lo] + nums[hi]; 22 | if (check == reqNum) 23 | { 24 | vector quad = {nums[i], nums[j], nums[lo], nums[hi]}; 25 | ans.push_back(quad); 26 | 27 | // Avoiding processing on the processed combinations of quads 28 | while (lo < hi && nums[lo] == quad[2]) 29 | lo++; 30 | while (lo < hi && nums[hi] == quad[3]) 31 | hi--; 32 | } 33 | else if (check < reqNum) 34 | lo++; 35 | else 36 | hi--; 37 | } 38 | 39 | // Avoiding processing on the processed combinations of quads 40 | while (j + 1 < nums.size() && nums[j] == nums[j + 1]) 41 | j++; 42 | } 43 | 44 | // Avoiding processing on the processed combinations of quads 45 | while (i + 1 < nums.size() && nums[i] == nums[i + 1]) 46 | i++; 47 | } 48 | 49 | return ans; 50 | } 51 | }; 52 | 53 | // Time Complexity - O(N^3) 54 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-01 (Arrays Part - 1)/Set Matrix Zeroes.cpp: -------------------------------------------------------------------------------- 1 | // Problem link - https://leetcode.com/problems/set-matrix-zeroes/submissions/ 2 | 3 | class Solution 4 | { 5 | public: 6 | void setZeroes(vector> &matrix) 7 | { 8 | bool colZero = false; 9 | 10 | for (int i = 0; i < matrix.size(); i++) 11 | { 12 | // Checking if we have any 0 in the first column 13 | if (matrix[i][0] == 0) 14 | colZero = true; 15 | 16 | // Marking the fist cell of the row and column as 0 17 | for (int j = 1; j < matrix[0].size(); j++) 18 | { 19 | if (matrix[i][j] == 0) 20 | { 21 | matrix[0][j] = 0; 22 | matrix[i][0] = 0; 23 | } 24 | } 25 | } 26 | 27 | // Making cells 0 in values who have their starting row cell and column cell as 0 28 | for (int i = 1; i < matrix.size(); i++) 29 | { 30 | for (int j = 1; j < matrix[0].size(); j++) 31 | { 32 | if (matrix[i][0] == 0 || matrix[0][j] == 0) 33 | matrix[i][j] = 0; 34 | } 35 | } 36 | 37 | // Checking if the first cell is 0 38 | // So that we can make the first row filled with 0s 39 | if (matrix[0][0] == 0) 40 | { 41 | for (int i = 0; i < matrix[0].size(); i++) 42 | { 43 | matrix[0][i] = 0; 44 | } 45 | } 46 | 47 | // Checking if there is any cell that is 0 in value in the first column then 48 | // We will fill the first column with 0s 49 | if (colZero) 50 | { 51 | for (int i = 0; i < matrix.size(); i++) 52 | { 53 | matrix[i][0] = 0; 54 | } 55 | } 56 | } 57 | }; 58 | 59 | // Time Complexity - O(m*n) 60 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-03 (Arrays Part - 3)/Reverse pairs.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/reverse-pairs 2 | 3 | class Solution 4 | { 5 | void _merge(vector &nums, int low, int mid, int high, int &reversePairCount) 6 | { 7 | int leftInd = low, rightInd = mid + 1; 8 | int r = rightInd, temp[high - low + 1], tempInd = 0, counter = 0; 9 | 10 | // 4 6 8, 1 2 3 11 | while (leftInd <= mid) 12 | { 13 | while (r <= high && (nums[leftInd] > (long)2 * nums[r])) 14 | counter++, r++; 15 | 16 | reversePairCount += counter; 17 | 18 | while (rightInd <= high && nums[rightInd] <= nums[leftInd]) 19 | { 20 | temp[tempInd++] = nums[rightInd++]; 21 | } 22 | 23 | temp[tempInd++] = nums[leftInd++]; 24 | } 25 | 26 | while (rightInd <= high) 27 | { 28 | temp[tempInd++] = nums[rightInd++]; 29 | } 30 | 31 | for (int i = 0; i < tempInd; i++) 32 | nums[low++] = temp[i]; 33 | } 34 | 35 | void _mergeSort(vector &nums, int low, int high, int &reversePairCount) 36 | { 37 | if (low < high) 38 | { 39 | int mid = low + (high - low) / 2; 40 | _mergeSort(nums, low, mid, reversePairCount); 41 | _mergeSort(nums, mid + 1, high, reversePairCount); 42 | _merge(nums, low, mid, high, reversePairCount); 43 | } 44 | } 45 | 46 | public: 47 | int reversePairs(vector &nums) 48 | { 49 | int reversePairCount = 0; 50 | int sz = nums.size(); 51 | 52 | if (sz <= 1) 53 | return reversePairCount; 54 | 55 | _mergeSort(nums, 0, sz - 1, reversePairCount); 56 | 57 | return reversePairCount; 58 | } 59 | }; 60 | 61 | // Time Complexity: O(N * log(N)) 62 | // Space Complexity: O(N) -------------------------------------------------------------------------------- /Day-01 (Arrays Part - 1)/Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/next-permutation/ 2 | 3 | class Solution 4 | { 5 | public: 6 | void nextPermutation(vector &nums) 7 | { 8 | int i = nums.size() - 2; 9 | 10 | // Get the i pointer to point on the element, 11 | // which is disturbing the sequence from the end of the array 12 | // For example, 13 | // 1. 1 2 3 14 | // i 15 | 16 | // 2. 1 1 5 17 | // 0 1 2 18 | // i 19 | 20 | // 3. 3 2 1 21 | // -1 0 1 2 22 | // i (-1) 23 | while (i >= 0 && nums[i] >= nums[i + 1]) 24 | i--; 25 | 26 | // So if we have a valid element, we will execute the below code 27 | if (i >= 0) 28 | { 29 | // Start our pointer from the last element of the array 30 | int j = nums.size() - 1; 31 | 32 | // We look for the element that is strictly greater, 33 | // than the element pointed by the ith pointer. 34 | while (nums[j] <= nums[i]) 35 | j--; 36 | 37 | // Then we swap the ith and jth element 38 | // 1 1 5 39 | // i j 40 | // After swapping 41 | // 1 5 1 42 | // j i 43 | swap(nums[i], nums[j]); 44 | } 45 | 46 | // Then we reverse the remaining (i + 1)th numbers 47 | // Covering the edge case -> [1, 3, 2] 48 | // Here the first and last element needs to be swapped 49 | // i.e - [2, 3, 1] 50 | // BUT the next lexicographically greatest permutation 51 | // should be [2, 1, 3] 52 | // Therefore, we reverse the remaining (i + 1)th elements 53 | reverse(nums.begin() + i + 1, nums.end()); 54 | } 55 | }; 56 | 57 | // Time Complexity - O(n) 58 | // Space Complexity - O(1) -------------------------------------------------------------------------------- /Day-02 (Arrays Part - 2)/Merge Intervals.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://leetcode.com/problems/merge-intervals/solution/ 2 | 3 | // <---------------------- ONLY SORTING SOLUTION ----------------------> 4 | class Solution 5 | { 6 | public: 7 | vector> merge(vector> &intervals) 8 | { 9 | sort(intervals.begin(), intervals.end()); 10 | vector> merged; 11 | 12 | for (auto interval : intervals) 13 | { 14 | if (merged.empty() || merged.back()[1] < interval[0]) 15 | merged.push_back(interval); 16 | else 17 | merged.back()[1] = max(merged.back()[1], interval[1]); 18 | } 19 | 20 | return merged; 21 | } 22 | }; 23 | 24 | // Time Complexity - O(N * log(N)) 25 | // Space Complexity - O(N) 26 | 27 | 28 | 29 | // <---------------------- STACK SOLUTION ----------------------> 30 | class Solution 31 | { 32 | public: 33 | vector> merge(vector> &intervals) 34 | { 35 | stack> st; 36 | sort(intervals.begin(), intervals.end()); 37 | 38 | for (int i = 0; i < intervals.size(); i++) 39 | { 40 | if (st.empty() == false) 41 | { 42 | pair curr = st.top(); 43 | if (curr.second >= intervals[i][0]) 44 | { 45 | st.pop(); 46 | curr.second = max(curr.second, intervals[i][1]); 47 | st.push(curr); 48 | } 49 | else 50 | st.push({intervals[i][0], intervals[i][1]}); 51 | } 52 | else 53 | st.push({intervals[i][0], intervals[i][1]}); 54 | } 55 | 56 | vector> ans; 57 | 58 | int i = 0; 59 | while (st.empty() == false) 60 | { 61 | ans.push_back({st.top().first, st.top().second}); 62 | st.pop(); 63 | i++; 64 | } 65 | 66 | reverse(ans.begin(), ans.end()); 67 | 68 | return ans; 69 | } 70 | }; 71 | 72 | // Time Complexity - O(N * log(N)) 73 | // Space Complexity - O(N) --------------------------------------------------------------------------------