├── 1213: intersection-of-three-sorted-arrays.cpp ├── 1509. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp ├── 2181. Merge Nodes in Between Zeros.cpp ├── Intersection of Two Arrays II.cpp ├── LIS.cpp ├── absolute.cpp ├── bfs.cpp ├── build-array-from-permutation.cpp ├── climbing-stairs.cpp ├── concatenation.cpp ├── count set bits ├── count sort - easy ├── countPairs.cpp ├── dot-product.cpp ├── dp-1.cpp ├── dp-2.cpp ├── dp-3.cpp ├── dp-4.cpp ├── dp-5.cpp ├── expedia-1.cpp ├── fibbonacci number ├── flatten.cpp ├── good substring ├── increasing-order-search-tree ├── inorder ├── inorder iterative ├── invertTree ├── kth factor ├── lonely node ├── lt 1550. Three Consecutive Odds.cpp ├── max min sum pair ├── max_prod_subarray.cpp ├── merge tree ├── min xor sum ├── min-cost-climbing-stairs.cpp ├── morris traversal preorder ├── nth tribonacci ├── number_of_island.cpp ├── post order ├── range sum bst ├── remove duplicate from sorted array - explain ├── reverse bits ├── rotate matrix ├── searchBST ├── sell stock II ├── sell-stocks.cpp ├── topo_sort.cpp ├── trailing zeroes ├── trap water └── tree /1213: intersection-of-three-sorted-arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Question: 3 | Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] 10 | Output: [1,5] 11 | Explanation: Only 1 and 5 appeared in the three arrays. 12 | Example 2: 13 | 14 | Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764] 15 | Output: [] 16 | 17 | 18 | Constraints: 19 | 20 | 1 <= arr1.length, arr2.length, arr3.length <= 1000 21 | 1 <= arr1[i], arr2[i], arr3[i] <= 2000 22 | */ 23 | 24 | 25 | // Solution: 26 | class Solution { 27 | public: 28 | vector arraysIntersection(vector& arr1, vector& arr2, vector& arr3) { 29 | int p1=0,p2=0,p3=0; 30 | vector res; 31 | while(p1= arr2[p2] && arr2[p2] >= arr3[p3], therefore move p3. 65 | */ 66 | -------------------------------------------------------------------------------- /1509. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an integer array nums. 3 | 4 | In one move, you can choose one element of nums and change it to any value. 5 | 6 | Return the minimum difference between the largest and smallest value of nums after performing at most three moves. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [5,3,2,4] 13 | Output: 0 14 | Explanation: We can make at most 3 moves. 15 | In the first move, change 2 to 3. nums becomes [5,3,3,4]. 16 | In the second move, change 4 to 3. nums becomes [5,3,3,3]. 17 | In the third move, change 5 to 3. nums becomes [3,3,3,3]. 18 | After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0. 19 | Example 2: 20 | 21 | Input: nums = [1,5,0,10,14] 22 | Output: 1 23 | Explanation: We can make at most 3 moves. 24 | In the first move, change 5 to 0. nums becomes [1,0,0,10,14]. 25 | In the second move, change 10 to 0. nums becomes [1,0,0,0,14]. 26 | In the third move, change 14 to 1. nums becomes [1,0,0,0,1]. 27 | After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1. 28 | It can be shown that there is no way to make the difference 0 in 3 moves. 29 | Example 3: 30 | 31 | Input: nums = [3,100,20] 32 | Output: 0 33 | Explanation: We can make at most 3 moves. 34 | In the first move, change 100 to 7. nums becomes [3,7,20]. 35 | In the second move, change 20 to 7. nums becomes [3,7,7]. 36 | In the third move, change 3 to 7. nums becomes [7,7,7]. 37 | After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0. 38 | 39 | 40 | Constraints: 41 | 42 | 1 <= nums.length <= 105 43 | -109 <= nums[i] <= 109 44 | */ 45 | 46 | class Solution { 47 | public: 48 | int minDifference(vector& nums) { 49 | int numsSize = nums.size(); 50 | 51 | // If the array has 4 or fewer elements, return 0 52 | if (numsSize <= 4) return 0; 53 | 54 | sort(nums.begin(), nums.end()); 55 | 56 | int minDiff = INT_MAX; 57 | 58 | // Four scenarios to compute the minimum difference 59 | for (int left = 0, right = numsSize - 4; left < 4; left++, right++) { 60 | minDiff = min(minDiff, nums[right] - nums[left]); 61 | } 62 | 63 | return minDiff; 64 | } 65 | }; 66 | 67 | 68 | 69 | /* 70 | Algorithm 71 | Initialization: 72 | Determine the size of the array nums and store it in numsSize. 73 | If numsSize is less than or equal to 4, return 0. 74 | Sort the array nums. 75 | Initialize minDiff to a very large number to store the minimum difference. 76 | Iterate through the first four elements of the sorted array: 77 | For each index left from 0 to 3: 78 | Calculate the corresponding right index as numsSize - 4 + left. 79 | Compute the difference between the elements at indices right and left in the sorted array. 80 | Update minDiff with the minimum value between minDiff and the computed difference. 81 | Return minDiff, which stores the minimum difference between the largest and smallest values after removing up to three elements 82 | */ 83 | -------------------------------------------------------------------------------- /2181. Merge Nodes in Between Zeros.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 4 | 5 | You are given the head of a linked list, which contains a series of integers separated by 0's. The beginning and end of the linked list will have Node.val == 0. 6 | 7 | For every two consecutive 0's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0's. 8 | 9 | Return the head of the modified linked list. 10 | 11 | 12 | 13 | Example 1: 14 | 15 | 16 | Input: head = [0,3,1,0,4,5,2,0] 17 | Output: [4,11] 18 | Explanation: 19 | The above figure represents the given linked list. The modified list contains 20 | - The sum of the nodes marked in green: 3 + 1 = 4. 21 | - The sum of the nodes marked in red: 4 + 5 + 2 = 11. 22 | Example 2: 23 | 24 | 25 | Input: head = [0,1,0,3,0,2,2,0] 26 | Output: [1,3,4] 27 | Explanation: 28 | The above figure represents the given linked list. The modified list contains 29 | - The sum of the nodes marked in green: 1 = 1. 30 | - The sum of the nodes marked in red: 3 = 3. 31 | - The sum of the nodes marked in yellow: 2 + 2 = 4. 32 | 33 | 34 | Constraints: 35 | 36 | The number of nodes in the list is in the range [3, 2 * 105]. 37 | 0 <= Node.val <= 1000 38 | There are no two consecutive nodes with Node.val == 0. 39 | The beginning and end of the linked list have Node.val == 0. 40 | 41 | 42 | 43 | * Definition for singly-linked list. 44 | * struct ListNode { 45 | * int val; 46 | * ListNode *next; 47 | * ListNode() : val(0), next(nullptr) {} 48 | * ListNode(int x) : val(x), next(nullptr) {} 49 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 50 | * }; 51 | */ 52 | class Solution { 53 | public: 54 | ListNode* mergeNodes(ListNode* head) { 55 | ListNode dummy(0); // Use a dummy node to simplify edge cases 56 | ListNode* res = &dummy; // Start res at the dummy node 57 | int sum = 0; 58 | 59 | head = head->next; // Skip the initial zero as per the problem statement 60 | 61 | while (head) { 62 | if (head->val != 0) { 63 | sum += head->val; 64 | } else { 65 | if (sum != 0) { 66 | res->next = new ListNode(sum); // Create a new node with the sum 67 | res = res->next; // Move to the new node 68 | sum = 0; // Reset the sum for the next segment 69 | } 70 | } 71 | head = head->next; // Move to the next node in the input list 72 | } 73 | 74 | return dummy.next; // Return the head of the merged nodes list 75 | } 76 | }; 77 | -------------------------------------------------------------------------------- /Intersection of Two Arrays II.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: nums1 = [1,2,2,1], nums2 = [2,2] 10 | Output: [2,2] 11 | Example 2: 12 | 13 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 14 | Output: [4,9] 15 | Explanation: [9,4] is also accepted. 16 | 17 | 18 | Constraints: 19 | 20 | 1 <= nums1.length, nums2.length <= 1000 21 | 0 <= nums1[i], nums2[i] <= 1000 22 | */ 23 | 24 | 25 | 26 | class Solution { 27 | public: 28 | vector intersect(vector& nums1, vector& nums2) { 29 | vector res; 30 | sort(nums1.begin(), nums1.end()); 31 | sort(nums2.begin(), nums2.end()); 32 | 33 | int i=0, j=0; 34 | while(inums2[j]){ 39 | j++; 40 | }else{ 41 | i++; 42 | } 43 | } 44 | return res; 45 | } 46 | }; 47 | 48 | 49 | // two pointer approach 50 | -------------------------------------------------------------------------------- /LIS.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to find length of longest increasing subsequence. 5 | int longestSubsequence(int n, int a[]) 6 | { 7 | // your code here 8 | int dp[n+1]; 9 | for(int i=0;i<=n;i++) dp[i]=1; 10 | for(int i=1;ia[j]) dp[i] = max(1+dp[j], dp[i]); 13 | } 14 | } 15 | int ans = INT_MIN; 16 | for(int i=0;i<=n;i++) ans=max(ans, dp[i]); 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /absolute.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | //User function Template for C++ 8 | 9 | class Solution { 10 | public: 11 | long long countPairs(int n, int arr[], int k) { 12 | // code here 13 | long long res=0; 14 | for(int i=0;i mp; 18 | for(int i=0;i> t; 32 | while (t--) { 33 | int n,k; 34 | cin>>n; 35 | 36 | int arr[n]; 37 | for(int i=0; i>arr[i]; 39 | 40 | cin>>k; 41 | 42 | Solution ob; 43 | cout << ob.countPairs(n,arr,k) << endl; 44 | } 45 | return 0; 46 | } 47 | // } Driver Code Ends 48 | -------------------------------------------------------------------------------- /bfs.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | // Function to return Breadth First Traversal of given graph. 9 | vector bfsOfGraph(int V, vector adj[]) { 10 | // Code here 11 | vector res; 12 | queue q; 13 | int vis[V]={0}; 14 | vis[0]=1; 15 | q.push(0); 16 | res.push_back(0); //push initial value in queue and res 17 | while(!q.empty()){ 18 | int temp = q.front(); //pop the first value 19 | q.pop(); 20 | for(int e:adj[temp]) if(!vis[e]){ //if it's cyclic graph, we need to check if the node is visited or not 21 | vis[e]=1; //mark the node as visited 22 | q.push(e); //push the child or connected node to queue 23 | res.push_back(e); // as well as in res array 24 | } 25 | } 26 | return res; 27 | } 28 | }; 29 | 30 | //{ Driver Code Starts. 31 | int main() { 32 | int tc; 33 | cin >> tc; 34 | while (tc--) { 35 | int V, E; 36 | cin >> V >> 37 | 38 | E; 39 | 40 | vector adj[V]; 41 | 42 | for (int i = 0; i < E; i++) { 43 | int u, v; 44 | cin >> u >> v; 45 | adj[u].push_back(v); 46 | // adj[v].push_back(u); 47 | } 48 | // string s1; 49 | // cin>>s1; 50 | Solution obj; 51 | vector ans = obj.bfsOfGraph(V, adj); 52 | for (int i = 0; i < ans.size(); i++) { 53 | cout << ans[i] << " "; 54 | } 55 | cout << endl; 56 | } 57 | return 0; 58 | } 59 | // } Driver Code Ends 60 | -------------------------------------------------------------------------------- /build-array-from-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& nums) { 4 | int n = nums.size(); 5 | // suppose array is [1,2,3,4] 6 | for(int i=0;i getConcatenation(vector& nums) { 4 | int n = nums.size(); 5 | for(int i=0;i0){ // while loop to check if all the bits have been covered or not 5 | A=(A&(A-1)); count++; // A&1 = (0 if A is even, else 1) 6 | // (A-1) will convert the last bit as set bit (1), the operation (A&(A-1)) will remove the right most set bit 7 | // by removing the right most set bits, we are counting the number of set bits here 8 | } 9 | return count; 10 | } 11 | -------------------------------------------------------------------------------- /count sort - easy: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namesapce std; 4 | //0,1,2 sorting 5 | int find_max(std::vector v){ 6 | int ans = INT_MIN; 7 | for(int i=0;iv[i]) ans = v[i]; 9 | } 10 | return ans; 11 | } 12 | 13 | void count_sort(std::vector v){ 14 | //find maximum of array 15 | int maxx = find_max(v); 16 | std::vector cnt(maxx , 0); //vector with size n with 0 values 17 | 18 | //fill the array 19 | 20 | for(int i=0;i [0,1,2,3] cnt 26 | for(int i=0;i0){ 28 | int val = cnt[i]; 29 | while(val--){ 30 | v.push_back(i); 31 | } 32 | } 33 | } 34 | } 35 | 36 | int main(){ 37 | std::vector v; 38 | int n; 39 | cout<<"we will not take inputs if it's a negative value"<0){ 41 | cin>>n; 42 | v.push_back(n); 43 | } 44 | //sorting 45 | 46 | //count sort 47 | count_sort(v); 48 | //print the final sorted array 49 | 50 | for(int i : v) std::cout<& nums, int target) { 4 | sort(nums.begin(), nums.end()); 5 | int i = 0; 6 | int j = nums.size()-1; 7 | int ans = 0; 8 | while(i it will give us ways 34 | */ 35 | -------------------------------------------------------------------------------- /dot-product.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two sparse vectors, compute their dot product. 3 | 4 | Implement class SparseVector: 5 | 6 | SparseVector(nums) Initializes the object with the vector nums 7 | dotProduct(vec) Compute the dot product between the instance of SparseVector and vec 8 | A sparse vector is a vector that has mostly zero values, 9 | you should store the sparse vector efficiently and compute the dot product between two SparseVector. 10 | */ 11 | 12 | 13 | class SparseVector { 14 | public: 15 | vector temp; 16 | SparseVector(vector &nums) { 17 | this->temp = nums; 18 | } 19 | 20 | // Return the dotProduct of two sparse vectors 21 | int dotProduct(SparseVector& vec) { 22 | int ans = 0; 23 | for(int i=0;ia, pair b){ 20 | return a.second> v; 28 | for(int i=0;i dp, int &val){ 21 | int l=0; 22 | int h=dp.size()-1; 23 | while(lval) h=m-1; 27 | else l=m+1; 28 | } 29 | if (dp[l]>val) return dp[l-1]; 30 | return dp[l]; //getting the closest value from coins array 31 | } 32 | vector minPartition(int N) 33 | { 34 | // code here 35 | // int dp[]={1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 36 | vector dp({1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}); 37 | vector res; 38 | while(N>0){ 39 | int val = bsearch(dp, N); 40 | res.push_back(val); 41 | N-=val; 42 | } 43 | return res; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /dp-4.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // int count=0; 4 | // int lcs(string s1, string s2, int i, int j, int ans){ 5 | // if(i==0||j==0) return ans; 6 | // if(s1[i-1]==s2[j-1]) ans = lcs(s1,s2,i-1,j-1,ans+1); 7 | // ans = max(ans, max(lcs(s1,s2,i-1,j,0),lcs(s1,s2,i,j-1,0))); 8 | // return ans; 9 | // } 10 | int longestCommonSubstr (string s1, string s2, int n, int m) 11 | { 12 | // your code here 13 | // return lcs(s1,s2,n,m,0); 14 | 15 | int dp[n+1][m+1]; 16 | memset(dp,0,sizeof(dp)); 17 | 18 | int ans=INT_MIN; 19 | for(int i=1;i<=n;i++){ 20 | for(int j=1;j<=m;j++){ 21 | if(s1[i-1]==s2[j-1]){ 22 | dp[i][j]=1+dp[i-1][j-1]; 23 | 24 | } 25 | else dp[i][j]=0; 26 | ans=max(ans,dp[i][j]); 27 | } 28 | } 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /dp-5.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | 4 | public: 5 | int maxSumIS(int a[], int n) 6 | { 7 | // Your code goes here 8 | int dp[n+1]={0}; 9 | // dp[0] = a[0]; 10 | int sum = 0; 11 | int max_sum = INT_MIN; 12 | for(int i=0;ia[j]){ 16 | dp[i] = max(dp[j]+a[i], dp[i]); //comparing if the sequence has maximum sum 17 | // cout<0){ 17 | if(temp==0) return r-l+1; 18 | r++; 19 | } 20 | if(temp<0) l++; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | Given a sorted array, and its rotated at a point(pivot), find a given number in that array. 34 | 6, 7 ,8 , 1 , 2, 3 ,5 35 | 36 | Val : 7 37 | O/p : 1 38 | 39 | 40 | Val : 9 41 | O/p : -1 42 | 43 | 44 | 45 | //brute force 46 | o(n) --> traverse whole array 47 | 48 | 5,4,3,1,2 49 | 50 | //binary search 51 | step1-> what is pivot element? 52 | 53 | n = arr.size(); 54 | //6, 7 ,8 , 1 , 2, 3 ,5 55 | int left = 0 , right = n-1; 56 | 57 | int findpivot(vector arr,left, right){ 58 | int mid = (left+right)/2; 59 | if(arr[mid]arr[mid+1]) return mid; 60 | //6, 7 ,8 , 9 , 1, 2, 3 ,5 61 | else if(arr[mid]>arr[mid-1] &&arr[mid]>arr[mid+1]) 62 | findpivot(arr,mid+1,right); 63 | else 64 | findpivot(arr,left,mid-1); 65 | } 66 | 67 | 68 | int main(){ 69 | int n; 70 | vector arr; 71 | cin>>n; 72 | for(int i=0;i val) { 79 | right = pivot-1; 80 | } 81 | else left = pivot+1; 82 | res = -1; 83 | while(left<=right){ 84 | int mid = (left+right)/2; 85 | if(arr[mid]==val) {//return mid; res = mid cout<val) right = mid+1; 87 | else left = mid-1; 88 | } 89 | cout< 5 | using namespace std; 6 | 7 | struct Node { 8 | int data; 9 | struct Node * next; 10 | struct Node * bottom; 11 | 12 | Node(int x) { 13 | data = x; 14 | next = NULL; 15 | bottom = NULL; 16 | } 17 | 18 | }; 19 | 20 | void printList(Node *Node) 21 | { 22 | while (Node != NULL) 23 | { 24 | printf("%d ", Node->data); 25 | Node = Node->bottom; 26 | } 27 | } 28 | 29 | 30 | // } Driver Code Ends 31 | /* Node structure used in the program 32 | 33 | struct Node{ 34 | int data; 35 | struct Node * next; 36 | struct Node * bottom; 37 | 38 | Node(int x){ 39 | data = x; 40 | next = NULL; 41 | bottom = NULL; 42 | } 43 | 44 | }; 45 | */ 46 | 47 | class Solution { 48 | public: 49 | Node *merge(Node *a, Node *b){ 50 | if(a==NULL) return b; 51 | if(b==NULL) return a; 52 | Node *res; 53 | if(a->data<=b->data){ 54 | res=a; 55 | res->bottom = merge(a->bottom, b); 56 | } 57 | else{ 58 | res=b; 59 | res->bottom = merge(a,b->bottom); 60 | } 61 | return res; 62 | } 63 | Node *flatten(Node *root) 64 | { 65 | // Your code here 66 | if(root==NULL or root->next==NULL) return root; 67 | root = merge(root, flatten(root->next)); 68 | return root; 69 | } 70 | }; 71 | 72 | 73 | //{ Driver Code Starts. 74 | 75 | int main(void) { 76 | 77 | int t; 78 | cin >> t; 79 | while (t--) { 80 | int n, m, flag = 1, flag1 = 1; 81 | struct Node * temp = NULL; 82 | struct Node * head = NULL; 83 | struct Node * pre = NULL; 84 | struct Node * tempB = NULL; 85 | struct Node * preB = NULL; 86 | cin >> n; 87 | int work[n]; 88 | for (int i = 0; i < n; i++) 89 | cin >> work[i]; 90 | for (int i = 0; i < n; i++) { 91 | m = work[i]; 92 | --m; 93 | int data; 94 | scanf("%d", &data); 95 | temp = new Node(data); 96 | temp->next = NULL; 97 | temp->bottom = NULL; 98 | 99 | if (flag) { 100 | head = temp; 101 | pre = temp; 102 | flag = 0; 103 | flag1 = 1; 104 | } 105 | else { 106 | pre->next = temp; 107 | pre = temp; 108 | flag1 = 1; 109 | } 110 | for (int j = 0; j < m; j++) { 111 | 112 | int temp_data; 113 | scanf("%d", &temp_data); 114 | tempB = new Node(temp_data); 115 | 116 | if (flag1) { 117 | temp->bottom = tempB; 118 | preB = tempB; 119 | flag1 = 0; 120 | } 121 | else { 122 | preB->bottom = tempB; 123 | preB = tempB; 124 | } 125 | } 126 | } 127 | Node *fun = head; 128 | Node *fun2 = head; 129 | 130 | Solution ob; 131 | Node* root = ob.flatten(head); 132 | printList(root); 133 | cout << endl; 134 | 135 | } 136 | return 0; 137 | } 138 | 139 | // } Driver Code Ends 140 | -------------------------------------------------------------------------------- /good substring: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | bool isvalid(string s){ 5 | int l = s.length(); 6 | map m; 7 | for(int i=0;i1) return false; 10 | } 11 | return true; 12 | } 13 | int countGoodSubstrings(string s) { 14 | int l = s.size(); 15 | int count = 0; 16 | for(int i=0;i vec; 15 | void helper(TreeNode* root){ 16 | if(root==nullptr) return; 17 | helper(root->left); 18 | vec.push_back(root->val); 19 | helper(root->right); 20 | } 21 | TreeNode* increasingBST(TreeNode* root) { 22 | helper(root); 23 | TreeNode* r = new TreeNode(0); 24 | TreeNode* curr=r; 25 | for(auto v : vec){ 26 | curr->right=new TreeNode(v); 27 | curr=curr->right; 28 | } 29 | return r->right; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /inorder: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector res; 15 | vector inorderTraversal(TreeNode* root) { 16 | if(root==nullptr) return res; 17 | inorderTraversal(root->left); 18 | res.push_back(root->val); 19 | inorderTraversal(root->right); 20 | return res; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /inorder iterative: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector inorderTraversal(TreeNode* root) { 15 | vector res; 16 | if(root==nullptr) return res; 17 | TreeNode* curr = root; 18 | stack st; 19 | while(curr!=NULL or !st.empty()){ 20 | while(curr){ 21 | st.push(curr); 22 | curr = curr->left; 23 | } 24 | curr = st.top(); 25 | res.push_back(curr->val); 26 | st.pop(); 27 | curr = curr->right; 28 | } 29 | return res; 30 | } 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /invertTree: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void swap(TreeNode* root){ 4 | TreeNode* temp = root->left; 5 | root->left=root->right; 6 | root->right=temp; 7 | } 8 | TreeNode* invertTree(TreeNode* root) { 9 | if(root==nullptr) return root; 10 | swap(root); 11 | invertTree(root->left); 12 | invertTree(root->right); 13 | return root; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /kth factor: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthFactor(int n, int k) { 4 | // vector dp; 5 | for(int i=1;i<=sqrt(n);i++){ 6 | if(n%i==0){ 7 | k--; 8 | if(k==0) return i; 9 | } 10 | } 11 | return -1; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /lonely node: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector vec; 15 | vector getLonelyNodes(TreeNode* root) { 16 | if(!root) { 17 | return {}; 18 | } 19 | // if(root->left==nullptr xor root->right==nullptr) vec.push_back(root->left?root->left->val:root->right->val); 20 | // if(root->left) getLonelyNodes(root->left); 21 | // if(root->right) getLonelyNodes(root->right); 22 | // return vec; 23 | if(root->left!=nullptr and root->right==nullptr) vec.push_back(root->left->val); 24 | if(root->right!=nullptr and root->left==nullptr) vec.push_back(root->right->val); 25 | getLonelyNodes(root->left); 26 | // cout<val; 27 | getLonelyNodes(root->right); 28 | return vec; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /lt 1550. Three Consecutive Odds.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false. 3 | 4 | 5 | Example 1: 6 | 7 | Input: arr = [2,6,4,1] 8 | Output: false 9 | Explanation: There are no three consecutive odds. 10 | Example 2: 11 | 12 | Input: arr = [1,2,34,3,4,5,7,23,12] 13 | Output: true 14 | Explanation: [5,7,23] are three consecutive odds. 15 | 16 | 17 | Constraints: 18 | 19 | 1 <= arr.length <= 1000 20 | 1 <= arr[i] <= 1000 21 | */ 22 | 23 | 24 | class Solution { 25 | public: 26 | bool threeConsecutiveOdds(vector& arr) { 27 | int n = arr.size(); 28 | // Loop through the array up to the third-to-last element 29 | for (int i = 0; i < n - 2; i++) { 30 | // Check if the current element and the next two elements are all 31 | // odd 32 | if (arr[i] % 2 == 1 && arr[i + 1] % 2 == 1 && arr[i + 2] % 2 == 1) { 33 | return true; 34 | } 35 | } 36 | return false; 37 | } 38 | }; 39 | 40 | 41 | // by keeping a pointer to check 42 | -------------------------------------------------------------------------------- /max min sum pair: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPairSum(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int currsum=0, maxsum=0; 6 | int i=0,j=nums.size()-1; 7 | while(i<=j){ 8 | currsum = nums[i]+nums[j]; 9 | // cout<maxsum) maxsum = currsum; 11 | i++; 12 | j--; 13 | } 14 | return maxsum; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /max_prod_subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | int p1 = nums[0]; //taking first value for tracking max so far 5 | int p2 = nums[0]; //tracking first value for min so far 6 | int res = nums[0]; // store result 7 | for(int i=1;ival+=root2->val; 7 | root1->left = mergeTrees(root1->left, root2->left); 8 | root1->right = mergeTrees(root1->right, root2->right); 9 | return root1; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /min xor sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[14][16385]; 4 | int solve(vector& a, vector& b, int mask, int i){ 5 | int ans = INT_MAX; 6 | if(i==a.size()) return 0; 7 | if(dp[i][mask]>-1) return dp[i][mask]; 8 | for(int j=0;j& nums1, vector& nums2) { 15 | memset(dp,-1,sizeof(dp)); 16 | return solve(nums1, nums2, 0, 0); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | // size of cost array 5 | int n = cost.size(); 6 | // define dynamic array to store costs and calculate efficiently 7 | vectordp(n); 8 | // store first two values as we can take upto 2 steps at a time 9 | dp[0] = cost[0]; 10 | dp[1] = cost[1]; 11 | for(int i=2; i res; 15 | vector preorderTraversal(TreeNode* root) { 16 | // if(root==nullptr) return res; 17 | // res.push_back(root->val); 18 | // preorderTraversal(root->left); 19 | // preorderTraversal(root->right); 20 | // return res; 21 | TreeNode* curr = root; 22 | while(curr){ 23 | res.push_back(curr->val); 24 | if(!curr->left) curr= curr->right; 25 | else{ 26 | TreeNode* temp = curr->left; 27 | while(temp->right) temp=temp->right; 28 | temp->right = curr->right; 29 | curr->right = NULL; 30 | curr = curr->left; 31 | } 32 | } 33 | return res; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /nth tribonacci: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tribonacci(int n) { 4 | if(n==0) return n; 5 | if(n==1 or n==2) return 1; 6 | int dp[n+1]; 7 | dp[0]=0; 8 | dp[1]=1; 9 | dp[2]=1; 10 | for(int i=3;i<=n;i++) dp[i] = dp[i-1]+dp[i-2]+dp[i-3]; 11 | return dp[n]; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /number_of_island.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. 3 | 4 | An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. 5 | You may assume all four edges of the grid are all surrounded by water. 6 | Input: grid = [ 7 | ["1","1","1","1","0"], 8 | ["1","1","0","1","0"], 9 | ["1","1","0","0","0"], 10 | ["0","0","0","0","0"] 11 | ] 12 | Output: 1 13 | */ 14 | 15 | 16 | /* solution: here i am using dfs approach by assuming a 2D matrix as undirected graph. 17 | first, traversing the whole matrix from (0,0) to (m,n) to get first 1 18 | as soon as i am getting first 1, i will trigger dfs to track all the neighbours and mark those nodes as visited, along with incrementing our ans by 1 19 | as we will find all the land and convert those into 0, so that we will not get ourselves in infinite loop 20 | */ 21 | 22 | /* 23 | DFS: here we have implemented DFS as recursive DFS to check if any adjacent cell has 1 left, if not, we will get out of loop, and get our island 24 | */ 25 | class Solution { 26 | public: 27 | void dfs(vector>& grid, int r, int c){ 28 | int nr = grid.size(); 29 | int nc = grid[0].size(); 30 | grid[r][c] = '0'; //marked as visited 31 | if(r-1>=0 && grid[r-1][c]=='1') dfs(grid, r-1, c); 32 | if(c-1>=0 && grid[r][c-1]=='1') dfs(grid, r, c-1); 33 | if(r+1>& grid) { 37 | int nr = grid.size(); 38 | int nc = grid[0].size(); 39 | int ans = 0; 40 | for(int r=0;r res; 4 | vector postorderTraversal(TreeNode* root) { 5 | stack s1; 6 | vector v; 7 | if(root==NULL) return v; 8 | s1.push(root); 9 | while(!s1.empty()) 10 | { 11 | TreeNode* curr=s1.top(); 12 | s1.pop(); 13 | v.push_back(curr->val); 14 | if(curr->left) s1.push(curr->left); 15 | if(curr->right) s1.push(curr->right); 16 | } 17 | reverse(v.begin(),v.end()); 18 | return v; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /range sum bst: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sum=0; 4 | int rangeSumBST(TreeNode* root, int low, int high) { 5 | if(root==nullptr) return sum; 6 | if(root->val>=low and root->val<=high) sum+= root->val; 7 | if(root->val>low) rangeSumBST(root->left,low,high); 8 | if(root->valright,low,high); 9 | return sum; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /remove duplicate from sorted array - explain: -------------------------------------------------------------------------------- 1 | 2 | 3 | the array is sorted, we need to keep. track of unique elements and put them at first place. 4 | 5 | example: [1,1,2,2] , output: [1,2] 6 | return value should be the size of new array 7 | 8 | we have two pointers: one to keep track of unique number and another one is to keep finding the different value in the array 9 | 10 | two pointers: i and j 11 | 12 | 13 | i=0, j=1 till the end of array 14 | 15 | check if nums[i] == nums[j] :=> do j=j+1 16 | else if nums[i]!=nums[j] :=> increment i by 1 and put nums[j] to nums[i] 17 | 18 | for [1,1,2,2] 19 | [0,1,2,3] 20 | 21 | pass 1: i=0,j=1 22 | 1 == 1 --> j=2, i=0 23 | pass 2: i=0,j=2 24 | 1 != 2 --> i=1, arr[1] = 2 25 | [1,2,2,2] 26 | pass 3: i=1, j=2 27 | 2 == 2 --> i=1, j=3 28 | pass 4: i=1, j=3 29 | 2 == 2 --> i=1, j=4 == size of array 30 | break out of loop 31 | return i+1 i.e. 1+1 = 2 32 | 33 | 34 | result array would be [1,2,2,2] trace to 2 length 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Solution: 45 | 46 | 47 | 48 | class Solution { 49 | public: 50 | int removeDuplicates(vector& nums) { 51 | if(nums.size()<2){ 52 | return nums.size(); 53 | } 54 | int i=0,j=1; 55 | while(j!=nums.size()){ 56 | if(nums[i]!=nums[j]){ 57 | i++; 58 | nums[i]=nums[j]; 59 | } 60 | else{ 61 | j++; 62 | } 63 | } 64 | return i+1; 65 | } 66 | }; 67 | -------------------------------------------------------------------------------- /reverse bits: -------------------------------------------------------------------------------- 1 | // problem: Reverse bits of a given 32 bits unsigned integer. 2 | 3 | unsigned int Solution::reverse(unsigned int A) { 4 | unsigned int res = 0, power = 31; 5 | while(A!=0){ 6 | res+=(A&1)<>1; // iterating until A becomes 0 9 | } 10 | return res; 11 | } 12 | -------------------------------------------------------------------------------- /rotate matrix: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | // find transpose 5 | // transpose is swapping (m[i][j], m[j][i]) 6 | for(int i=0;ival == val) return root; 5 | return root->val>val?searchBST(root->left,val):searchBST(root->right,val); 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /sell stock II: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& a) { 4 | int b=a[0],s=0,p=0,mp=0; 5 | for(int i=1;i& a) { 4 | int p=0,s=0,b=a[0]; 5 | for(int i=1;i 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution 7 | { 8 | public: 9 | //Function to return list containing vertices in Topological order. 10 | vector topoSort(int V, vector adj[]) 11 | { 12 | // code here 13 | vector res, topo; 14 | for(int i=0;i q; 19 | for(int i=0;i &res, vector adj[]) { 42 | 43 | if(V!=res.size()) 44 | return 0; 45 | 46 | vector map(V, -1); 47 | for (int i = 0; i < V; i++) { 48 | map[res[i]] = i; 49 | } 50 | for (int i = 0; i < V; i++) { 51 | for (int v : adj[i]) { 52 | if (map[i] > map[v]) return 0; 53 | } 54 | } 55 | return 1; 56 | } 57 | 58 | int main() { 59 | int T; 60 | cin >> T; 61 | while (T--) { 62 | int N, E; 63 | cin >> E >> N; 64 | int u, v; 65 | 66 | vector adj[N]; 67 | 68 | for (int i = 0; i < E; i++) { 69 | cin >> u >> v; 70 | adj[u].push_back(v); 71 | } 72 | 73 | Solution obj; 74 | vector res = obj.topoSort(N, adj); 75 | 76 | cout << check(N, res, adj) << endl; 77 | } 78 | 79 | return 0; 80 | } 81 | // } Driver Code Ends 82 | -------------------------------------------------------------------------------- /trailing zeroes: -------------------------------------------------------------------------------- 1 | // problem: Given an integer A, count and return the number of trailing zeroes. 2 | class Solution: 3 | def solve(self, A): 4 | count=0 5 | while(A&1==0): // A&1: (0 if A is even, else 1) in this way checking if A has right most it is zero of not 6 | count=count+1 // if right most bit is 0, increase the count 7 | A=A>>1 // using right shift, we are just iterating over bits, in from left to right 8 | return count 9 | -------------------------------------------------------------------------------- /trap water: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& arr) { 4 | int n = arr.size(); 5 | // idea is to get the max water in the tank, hence we are taking left max and right max (defining boundaries) 6 | vector right(n); 7 | vector left(n); 8 | left[0] = arr[0]; //since we can not store anything at 0th index, we are taking whatever height we have been given 9 | right[n-1] = arr[n-1]; // same for right side boundary 10 | for(int i=1;i=0;i--){ 14 | right[i] = max(right[i+1], arr[i]); // defining right max in right array 15 | } 16 | int ans = 0; 17 | for(int i=0;i