├── september ├── SEP_25.cpp ├── SEP_31_Move_all_zeroes_to_end_of_array.cpp ├── SEP_18_Pow_of_2.cpp ├── SEP_19_Find_first_set_bit.cpp ├── SEP_28_Wave_array.cpp ├── SEP_05_Print_adjacency_list.cpp ├── SEP_11_Lucky_Number.cpp ├── SEP_12_Perfect_Number.cpp ├── SEP_17_Print_first_n_Fibonacci_Numbers.cpp ├── SEP_24_Find_duplicates_in_an_array.cpp ├── SEP_16_Count_number_of_hops.cpp ├── SEP_21_Stickler_Thief.cpp ├── SEP_20_Rotate_Bit.cpp ├── SEP_13_Largest_number_possible.cpp ├── SEP_23_Equilibrium_Point.cpp ├── SEP_10_insert_node_in_BST.cpp ├── SEP_02_Leaf_under_budget.cpp ├── SEP_09_Kth_largest_element_in_BST.cpp ├── SEP_03_Check_if_Tree_is_Isomorphic.cpp ├── SEP_27_Find_the_closest_pair_from_two_arrays.cpp ├── SEP_06_Mother_Vertex.cpp ├── SEP_14_Perfect_Sum_Problem.cpp ├── SEP_01_Leftmost_and_rightmost_nodes_of_binary_tree.cpp ├── SEP_08_Binary_Tree_to_BST.cpp ├── SEP_30_Boolean_Matrix.cpp ├── SEP_07_Minimum_Multiplications_to_reach_End.cpp ├── SEP_15_Partition_Equal_subset_sum.cpp ├── SEP_29_Number_Of_Enclaves.cpp ├── SEP_04_Replace_Os_with_Xs.cpp ├── SEP_26_Find_All_Four_Sum_Numbers.cpp └── SEP_22_First_and_last_occurrences_of_x.cpp ├── README.md ├── october ├── OCT_09_Height_of_Binary_Tree.cpp ├── OCT_26_Minimum_Operations.cpp ├── OCT_21_Sum_of_all_divisors_from_1_to_n.cpp ├── OCT_20_Sum_of_all_divisors_from_1_to_n.cpp ├── OCT_03_Column_name_from_a_given_column_number.cpp ├── OCT_11_Check_for_Balanced_Tree.cpp ├── OCT_08_Insert_in_a_Sorted_List.cpp ├── OCT_23_Maximum_sum_increasing_subsequence.cpp ├── OCT_17_Transitive_closure_of_a_Graph.cpp ├── OCT_02_Number_of_distinct_subsequences.cpp ├── OCT_27_Minimum_Deletions.cpp ├── OCT_07_Pairwise_swap_elements_of_a_linked_list.cpp ├── OCT_01_Boundary_traversal_of_matrix.cpp ├── OCT_14_Find_Common_Nodes_in_two_BSTs.cpp ├── OCT_05_Count_number_of_substrings.cpp ├── OCT_19_Level_of_Nodes.cpp ├── OCT_22_Number_of_paths.cpp ├── OCT_25_Knapsack_with_Duplicate_Items.cpp ├── OCT_15_Normal_BST_to_Balanced_BST.cpp ├── OCT_18_Eventual_Safe_States.cpp ├── OCT_06_Reverse_alternate_nodes_in_Link_List.cpp ├── OCT_13_Floor_in_BST.cpp ├── OCT_10_ Nodes_at_given_distance_in_binary_tree.cpp ├── OCT_12_Duplicate_subtree_in_Binary_Tree.cpp ├── OCT_24_Palindromic_Partitioning.cpp ├── OCT_16_Making_A_Large_Island.cpp └── OCT_04_Roman_Number_to_Integer.cpp ├── august ├── AUG_27_Reverse_a_String.cpp ├── AUG_25_Palindrome_String.cpp ├── AUG_14_Non_Repeating_Numbers.cpp ├── AUG_13_Nth_Fibonacci_Number.cpp ├── AUG_05_Chocolate_Distribution_Problem.cpp ├── AUG_16_Nth_catalan_number.cpp ├── AUG_04_Reverse_a_Stack.cpp ├── AUG_18_Leader_in_an_array.cpp ├── AUG_09_Largest_prime_factor.cpp ├── AUG_28_Remove_duplicate_element_from_sorted_Linked_List.cpp ├── AUG_06_String_Permutations.cpp ├── AUG_12_Longest_Increasing_Subsequence.cpp ├── AUG_19_Subarray_with_given_sum.cpp ├── AUG_20_Number_of_occurrence.cpp ├── AUG_26_Longest_K_unique_characters_substring.cpp ├── AUG_01_DFS_of_Graph.cpp ├── AUG_30_Delete_a_Node_in_Single_Linked_List.cpp ├── AUG_15_Flip_Bits.cpp ├── AUG_10_Longest_Common_Subsequence.cpp ├── AUG_22_Make_Matrix_Beautiful.cpp ├── AUG_29_Delete_nodes_having_greater_value_on_right.cpp ├── AUG_08_Fraction_pairs_with_sum1.cpp ├── AUG_24_Multiply_two_strings.cpp ├── AUG_11_Coin_Change.cpp ├── AUG_17_Next_Smallest_palindrome.cpp ├── AUG_02_Shortest_Source_to_Destination_Path.cpp ├── AUG_21_Surrounded_the_1s.cpp ├── AUG_03_Shortest_path_in_Directed_Acyclic_Graph.cpp ├── AUG_07_Solve_the_Sudoku.cpp ├── AUG_23_Find_the_string_in_grid.cpp └── AUG_31_AVL_Tree_Deletion.cpp ├── july ├── JULY_01_Number_of_1_Bits.cpp ├── JULY_02_Copy_set_Bits_in_Range.cpp ├── JULY_09_Smallest_possitive_missing_number.cpp ├── JULY_10_Transpose_of_matrix.cpp ├── JULY_20_Non_Repeating_character.cpp ├── JULY_16_Queue_reversal.cpp ├── JULY_15_Delete_middle_element_of_a_stack.cpp ├── JULY_07_Merge_Without_Extra_Space.cpp ├── JULY_04_Count_the_subarrays_having_product_less_than_k.cpp ├── JULY_28_Lowest_Common_Ancestor_in_a_BST.cpp ├── JULY_13_Unique_number_of_occurrences.cpp ├── JULY_19_Longest_palindromic_subsequence.cpp ├── JULY_31_BFS_of_graph.cpp ├── JULY_18_Longest_Repeating_Subsequence.cpp ├── JULY_29_Median_of_BST.cpp ├── JULY_05_Stock_buy_and_sell_II.cpp ├── JULY_06_Quick_sort.cpp ├── JULY_08_Find_triplets_with_zero_sum.cpp ├── JULY_12_Power_of_numbers.cpp ├── JULY_14_Implement_two_stacks_in_an_array.cpp ├── JULY_22_Remove_duplicates_from_an_unsorted_linked_list.cpp ├── JULY_21_Reverse_a_linked_list_in_groups_of_given_size.cpp ├── JULY_23_sort_linked_list_of_0s_1s_and_2s.cpp ├── JULY_03_Maximum_index.cpp ├── JULY_24_Right_view_of_binary_tree.cpp ├── JULY_27_Heap_sort.cpp ├── JULY_11_Find_kth_element_of_spiral_matrix.cpp ├── JULY_26_Kth_Ancestor_in_a_Tree.cpp ├── JULY_17_First_non_repeating_character_in_a_stream.cpp ├── JULY_30_Inorder_Successor_in_BST.cpp └── JULY_25_level_order_traversal_in_a_spiral_form.cpp ├── november ├── NOV_1_Frequencies_of_Limited_Range_Array_Elements.cpp ├── NOV_3_Pythagorean_Triplet.cpp └── NOV_2_Minimum_distance_between_two_numbers.cpp └── june ├── JUNE_28_Maximum_Depth_Of_Binary_Tree.cpp ├── JUNE_30_Is_Binary_Number_Multiple_of_3.cpp └── JUNE_29_Next_Happy_Number.cpp /september/SEP_25.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GFG_POTD 2 | Geeks For Geeks problems of the day solutions. 3 | -------------------------------------------------------------------------------- /october/OCT_09_Height_of_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int height(struct Node* node){ 4 | return (!node)? 0 : 1 + max(height(node->left), height(node->right)); 5 | } 6 | }; -------------------------------------------------------------------------------- /september/SEP_31_Move_all_zeroes_to_end_of_array.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | void pushZerosToEnd(int arr[], int n) { 4 | // code here 5 | for(int i = 0, j = 0; i> i) & 1) << i; 13 | } 14 | return x; 15 | } 16 | }; -------------------------------------------------------------------------------- /october/OCT_20_Sum_of_all_divisors_from_1_to_n.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long sumOfDivisors(int N) 5 | { 6 | // Write Your Code here 7 | long long sum = 0; 8 | // value (N/i) repeats i times for each f(i) 9 | for(int i = 1; i<=N; i++){ 10 | sum += (N/i) *i; 11 | } 12 | return sum; 13 | } 14 | }; -------------------------------------------------------------------------------- /september/SEP_28_Wave_array.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // arr: input array 4 | // n: size of array 5 | //Function to sort the array into a wave-like array. 6 | void convertToWave(int n, vector& arr){ 7 | 8 | // Your code here 9 | for(int i = 0; i& arr,int N, int P) 5 | { 6 | // code here 7 | unordered_map map; 8 | for(auto i : arr) 9 | map[i]++; 10 | 11 | for(int i = 0; i> printGraph(int V, vector>edges) { 5 | // Code here 6 | vector> adj(V); 7 | for(auto edge : edges){ 8 | adj[edge.first].push_back(edge.second); 9 | adj[edge.second].push_back(edge.first); 10 | } 11 | return adj; 12 | } 13 | }; -------------------------------------------------------------------------------- /september/SEP_11_Lucky_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isLucky(int n) 5 | { 6 | // code here 7 | // 1 3 5 8 | // 3 9 | for (int i = 2; n >= i; i++) 10 | { 11 | if (n % i == 0) 12 | return false; 13 | // how many will remove 14 | int removed = n / i; 15 | // rest 16 | n -= removed; 17 | } 18 | return true; 19 | } 20 | }; -------------------------------------------------------------------------------- /july/JULY_09_Smallest_possitive_missing_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to find the smallest positive number missing from the array. 5 | int missingNumber(int arr[], int n) 6 | { 7 | //-10 -20 0 1 3 8 | sort(arr, arr + n); 9 | int missing = 1; 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (arr[i] == missing) 13 | missing++; 14 | } 15 | return missing; 16 | } 17 | }; -------------------------------------------------------------------------------- /july/JULY_10_Transpose_of_matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to find transpose of a matrix. 5 | void transpose(vector> &matrix, int n) 6 | { 7 | // code here 8 | int a = 0; 9 | for (int i = a; i < n; i++) 10 | { 11 | for (int j = a; j < n; j++) 12 | { 13 | swap(matrix[i][j], matrix[j][i]); 14 | } 15 | a++; 16 | } 17 | } 18 | }; -------------------------------------------------------------------------------- /august/AUG_14_Non_Repeating_Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector singleNumber(vector nums) 5 | { 6 | // Code here. 7 | map map; 8 | for (int i : nums) 9 | { 10 | map[i]++; 11 | } 12 | vector ans; 13 | for (auto i : map) 14 | { 15 | if (i.second == 1) 16 | ans.push_back(i.first); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /august/AUG_13_Nth_Fibonacci_Number.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | class Solution 3 | { 4 | public: 5 | int nthFibonacci(int n) 6 | { 7 | // code here 8 | if (n <= 1) 9 | return n; 10 | int a = 0, b = 1, fib; 11 | int mod = 1e9 + 7; 12 | 13 | for (int i = 2; i <= n; i++) 14 | { 15 | fib = (a + b) % mod; 16 | a = b; 17 | b = fib; 18 | } 19 | return fib; 20 | } 21 | }; -------------------------------------------------------------------------------- /september/SEP_12_Perfect_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int isPerfectNumber(long long N) 5 | { 6 | // code here 7 | long long sum = 0; 8 | for (int i = 1; i <= sqrt(N); i++) 9 | { 10 | if (N % i == 0) 11 | { 12 | sum += i; 13 | if (i * i != N) 14 | { 15 | sum += N / i; 16 | } 17 | } 18 | } 19 | return sum / 2 == N; 20 | } 21 | }; -------------------------------------------------------------------------------- /august/AUG_05_Chocolate_Distribution_Problem.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long findMinDiff(vector a, long long n, long long m) 5 | { 6 | // code 7 | sort(a.begin(), a.end()); 8 | // 1 3 4 7 9 9 12 56 9 | // 0 1 2 3 4 5 6 7 8 10 | long long ans = INT_MAX; 11 | for (int i = 0; i < n - (m - 1); i++) 12 | { 13 | long long diff = a[i + m - 1] - a[i]; 14 | ans = min(ans, diff); 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /july/JULY_20_Non_Repeating_character.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to find the first non-repeating character in a string. 5 | char nonrepeatingCharacter(string S) 6 | { 7 | // Your code here 8 | unordered_map map; 9 | for (char ch : S) 10 | { 11 | map[ch]++; 12 | } 13 | for (char ch : S) 14 | { 15 | if (map[ch] == 1) 16 | return ch; 17 | } 18 | return '$'; 19 | } 20 | }; -------------------------------------------------------------------------------- /september/SEP_17_Print_first_n_Fibonacci_Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to return list containing first n fibonacci numbers. 5 | vector printFibb(int n) 6 | { 7 | //code here 8 | if(n == 1) return {1}; 9 | if(n == 2) return {1, 1}; 10 | 11 | vector dp(n); 12 | dp[0] = 1; 13 | dp[1] = 1; 14 | for(int i = 2; i rev(queue q) 8 | { 9 | // add code here. 10 | stack stack; 11 | while (!q.empty()) 12 | { 13 | stack.push(q.front()); 14 | q.pop(); 15 | } 16 | while (!stack.empty()) 17 | { 18 | q.push(stack.top()); 19 | stack.pop(); 20 | } 21 | return q; 22 | } 23 | }; -------------------------------------------------------------------------------- /september/SEP_24_Find_duplicates_in_an_array.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector duplicates(int arr[], int n) { 4 | // code here 5 | unordered_map freq; 6 | vector ans; 7 | 8 | for(int i = 0; i 1) 14 | ans.push_back(i); 15 | } 16 | 17 | if(ans.empty()) 18 | return {-1}; 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /september/SEP_16_Count_number_of_hops.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int mod = 1e9+7; 4 | vector dp; 5 | long long solve(int n){ 6 | if(n == 0) return 1; 7 | if(n < 0) return 0; 8 | if(dp[n] != -1) return dp[n]; 9 | 10 | return dp[n] = ((solve(n-1) + solve(n-2))%mod + solve(n-3)) %mod; 11 | } 12 | public: 13 | //Function to count the number of ways in which frog can reach the top. 14 | long long countWays(int n) 15 | { 16 | dp.resize(n+1, -1); 17 | return solve(n)%mod; 18 | } 19 | }; -------------------------------------------------------------------------------- /august/AUG_04_Reverse_a_Stack.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | 3 | class Solution 4 | { 5 | stack aux; 6 | void copyInAux(stack &St) 7 | { 8 | if (St.empty()) 9 | return; 10 | int top = St.top(); 11 | St.pop(); 12 | copyInAux(St); 13 | aux.push(top); 14 | } 15 | 16 | public: 17 | void Reverse(stack &St) 18 | { 19 | copyInAux(St); 20 | while (!aux.empty()) 21 | { 22 | St.push(aux.top()); 23 | aux.pop(); 24 | } 25 | } 26 | }; -------------------------------------------------------------------------------- /july/JULY_15_Delete_middle_element_of_a_stack.cpp: -------------------------------------------------------------------------------- 1 | // User function template for C++ 2 | 3 | class Solution 4 | { 5 | public: 6 | // Function to delete middle element of a stack. 7 | 8 | void deleteMid(stack &s, int sizeOfStack) 9 | { 10 | // code here.. 11 | // if it's a middle element 12 | if (sizeOfStack <= 1) 13 | { 14 | s.pop(); 15 | return; 16 | } 17 | int poped = s.top(); 18 | s.pop(); 19 | sizeOfStack -= 2; 20 | deleteMid(s, sizeOfStack); 21 | s.push(poped); 22 | } 23 | }; -------------------------------------------------------------------------------- /september/SEP_21_Stickler_Thief.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | vector dp; 4 | int solve(int i, int arr[], int n){ 5 | if(i >= n) return 0; 6 | 7 | //aplying dp 8 | if(dp[i] != -1) return dp[i]; 9 | 10 | int looted = arr[i] + solve(i+2, arr, n);//looted 11 | int skiped = solve(i+1, arr, n);//skiped 12 | 13 | return dp[i] = max(looted, skiped); 14 | } 15 | 16 | public: 17 | 18 | int FindMaxSum(int arr[], int n) 19 | { 20 | dp.resize(n+1, -1); 21 | return max(solve(0, arr, n), solve(1, arr, n)); 22 | } 23 | }; -------------------------------------------------------------------------------- /august/AUG_18_Leader_in_an_array.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution 4 | { 5 | // Function to find the leaders in the array. 6 | public: 7 | vector leaders(int a[], int n) 8 | { 9 | // Code here 10 | vector ans; 11 | stack stack; 12 | for (int i = n - 1; i >= 0; i--) 13 | { 14 | if (stack.empty() || stack.top() <= a[i]) 15 | stack.push(a[i]); 16 | } 17 | while (!stack.empty()) 18 | { 19 | ans.push_back(stack.top()); 20 | stack.pop(); 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /september/SEP_20_Rotate_Bit.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector rotate(int n, int d) { 5 | d %= 16; 6 | // Right rotation 7 | int rightBits = n & ((1 << d) - 1); 8 | int remains = n >> d; 9 | int x = (rightBits << (16 - d)) | remains; 10 | 11 | 12 | 13 | // Left rotation 14 | // D exclude 15 | //___ _____________ 16 | //101 1100100000100 = 0000000000000101 17 | int leftBits = n >> (16 - d); 18 | remains = n & ((1 << (16 - d)) - 1); 19 | int y = (remains << d) | leftBits; 20 | 21 | return {y, x}; 22 | } 23 | }; -------------------------------------------------------------------------------- /september/SEP_13_Largest_number_possible.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string findLargest(int N, int S) 5 | { 6 | if (S == 0 && N != 1) 7 | return "-1"; 8 | if ((9 * N) < S) 9 | return "-1"; 10 | 11 | string str(N, '0'); 12 | int i = 0; 13 | while (S) 14 | { 15 | if (S - 9 >= 0) 16 | { 17 | str[i++] = '9'; 18 | S -= 9; 19 | } 20 | else 21 | { 22 | str[i++] = S + '0'; 23 | S = 0; 24 | } 25 | } 26 | return str; 27 | } 28 | }; -------------------------------------------------------------------------------- /september/SEP_23_Equilibrium_Point.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // Function to find equilibrium point in the array. 4 | // a: input array 5 | // n: size of array 6 | int equilibriumPoint(long long arr[], int n) { 7 | 8 | // Your code here 9 | long long sum = 0; 10 | for(int i = 0; i &a, int n, long long k) 5 | { 6 | int count = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | long long product = 1; 10 | for (int j = i; j < n; j++) 11 | { 12 | product *= a[j]; 13 | if (product < k) 14 | { 15 | count++; 16 | } 17 | else 18 | { 19 | break; 20 | } 21 | } 22 | } 23 | return count; 24 | } 25 | }; -------------------------------------------------------------------------------- /july/JULY_28_Lowest_Common_Ancestor_in_a_BST.cpp: -------------------------------------------------------------------------------- 1 | // Function to find the lowest common ancestor in a BST. 2 | class Solution 3 | { 4 | 5 | public: 6 | Node *LCA(Node *root, int n1, int n2) 7 | { 8 | // code here 9 | if (root->data == n1 || root->data == n2) 10 | return root; 11 | else if (n1 < root->data && n2 > root->data || n1 > root->data && n2 < root->data) 12 | return root; 13 | else if (n1 < root->data && n2 < root->data) 14 | LCA(root->left, n1, n2); 15 | else if (n1 > root->data && n1 > root->data) 16 | LCA(root->right, n1, n2); 17 | // both should be in different direction 18 | } 19 | }; -------------------------------------------------------------------------------- /october/OCT_11_Check_for_Balanced_Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | pair solve(Node *root){ 4 | if(!root) return {true, 0}; 5 | 6 | pair left = solve(root->left); 7 | pair right = solve(root->right); 8 | 9 | pair p; 10 | 11 | p.second = 1 + max(left.second, right.second);//calculating height 12 | p.first = left.first && right.first && abs(left.second - right.second) <= 1; 13 | return p; 14 | } 15 | public: 16 | //Function to check whether a binary tree is balanced or not. 17 | bool isBalanced(Node *root) 18 | { 19 | return solve(root).first; 20 | } 21 | }; -------------------------------------------------------------------------------- /june/JUNE_28_Maximum_Depth_Of_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | /* The Node structure is 2 | struct Node 3 | { 4 | int data; 5 | Node* left; 6 | Node* right; 7 | }; */ 8 | 9 | class Solution{ 10 | public: 11 | // int depth = 0; 12 | int max(int x,int y){ 13 | return (x>y)?x:y; 14 | } 15 | /*You are required to complete this method*/ 16 | int maxDepth(Node *root) { 17 | // Your code here 18 | if(root == NULL){ 19 | return 0; 20 | } 21 | int leftDepth = 1, rightDepth = 1; 22 | leftDepth += maxDepth(root->left); 23 | rightDepth += maxDepth(root->right); 24 | return max(leftDepth,rightDepth); 25 | } 26 | }; -------------------------------------------------------------------------------- /october/OCT_08_Insert_in_a_Sorted_List.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // Should return head of the modified linked list 4 | Node *sortedInsert(struct Node* head, int data) { 5 | // Code here 6 | Node *newnode = new Node(data); 7 | if(data < head->data){ 8 | newnode->next = head; 9 | head = newnode; 10 | return head; 11 | } 12 | 13 | Node *prev = head, *curr = head->next; 14 | while(curr && data > curr->data){ 15 | prev = curr; 16 | curr = curr->next; 17 | } 18 | 19 | prev->next = newnode; 20 | newnode->next = curr; 21 | 22 | return head; 23 | } 24 | }; -------------------------------------------------------------------------------- /september/SEP_10_insert_node_in_BST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | struct Node { 3 | int data; 4 | Node* left; 5 | Node* right; 6 | 7 | Node(int val) { 8 | data = val; 9 | left = right = NULL; 10 | } 11 | }; 12 | */ 13 | 14 | class Solution 15 | { 16 | public: 17 | Node *insert(Node *node, int data) 18 | { 19 | 20 | // Your code goes here 21 | if (!node) 22 | { 23 | return new Node(data); 24 | } 25 | if (data < node->data) 26 | { 27 | node->left = insert(node->left, data); 28 | } 29 | else if (data > node->data) 30 | { 31 | node->right = insert(node->right, data); 32 | } 33 | return node; 34 | } 35 | }; -------------------------------------------------------------------------------- /june/JUNE_30_Is_Binary_Number_Multiple_of_3.cpp: -------------------------------------------------------------------------------- 1 | //User function template for C++ 2 | class Solution{ 3 | public: 4 | int isDivisible(string s){ 5 | //complete the function here 6 | int oddSum = 0; 7 | int evenSum = 0; 8 | 9 | for(int i = 0; i < s.length(); i++){ 10 | //even position 11 | if((i+1) % 2 == 0){ 12 | if(s[i] == '1'){ 13 | evenSum ++; 14 | } 15 | } 16 | //else odd postion 17 | else{ 18 | if(s[i] == '1'){ 19 | oddSum ++; 20 | } 21 | } 22 | } 23 | if(abs(evenSum - oddSum) % 3 == 0){ 24 | return 1; 25 | } 26 | return 0; 27 | } 28 | 29 | }; -------------------------------------------------------------------------------- /october/OCT_23_Maximum_sum_increasing_subsequence.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | int N; 4 | vector dp; 5 | int solve(int i,int sum, int arr[]){ 6 | if(i >= N) return 0; 7 | if(dp[i] != -1) return dp[i]; 8 | int ans = 0; 9 | for(int j = i+1; j < N;j++){ 10 | if(arr[i] < arr[j]) 11 | ans = max(ans, arr[j] + solve(j, sum + arr[j], arr)); 12 | } 13 | return dp[i] = ans; 14 | } 15 | public: 16 | int maxSumIS(int arr[], int n) 17 | { 18 | // Your code goes here 19 | this->N =n; 20 | int ans = 0; 21 | dp.resize(n, -1); 22 | for(int i = 0; inext != NULL) 20 | { 21 | if (temp->next->data == temp->data) 22 | { 23 | Node *toDelete = temp->next; 24 | temp->next = toDelete->next; 25 | delete (toDelete); 26 | } 27 | else 28 | temp = temp->next; 29 | } 30 | return head; 31 | } -------------------------------------------------------------------------------- /august/AUG_06_String_Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Complete this function 5 | void permute(string str, int start, vector &ans) 6 | { 7 | if (start == str.size() - 1) 8 | { 9 | ans.push_back(str); 10 | return; 11 | } 12 | for (int i = start; i < str.size(); i++) 13 | { 14 | swap(str[start], str[i]); 15 | permute(str, start + 1, ans); 16 | } 17 | } 18 | 19 | vector permutation(string S) 20 | { 21 | // Your code here 22 | vector ans; 23 | permute(S, 0, ans); 24 | // lexicographically increasing order 25 | sort(ans.begin(), ans.end()); 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /august/AUG_12_Longest_Increasing_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution 4 | { 5 | public: 6 | // Function to find length of longest increasing subsequence. 7 | int longestSubsequence(int n, int a[]) 8 | { 9 | // your code here 10 | vector ans; 11 | ans.push_back(a[0]); 12 | 13 | for (int i = 1; i < n; i++) 14 | { 15 | if (a[i] > ans.back()) 16 | { 17 | ans.push_back(a[i]); 18 | } 19 | else 20 | { 21 | int ind = lower_bound(ans.begin(), ans.end(), a[i]) - ans.begin(); 22 | ans[ind] = a[i]; 23 | } 24 | } 25 | // 0 26 | return ans.size(); 27 | } 28 | }; -------------------------------------------------------------------------------- /september/SEP_02_Leaf_under_budget.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // Return True if the given trees are isomotphic. Else return False. 4 | bool isIsomorphic(Node *root1,Node *root2) 5 | { 6 | //add code here. 7 | if(!root1 && !root2) return true; 8 | if(!root1 || !root2) return false; 9 | 10 | if(root1->data == root2->data){ 11 | return ( 12 | isIsomorphic(root1->left, root2->left) && 13 | isIsomorphic(root1->right, root2->right) 14 | || 15 | isIsomorphic(root1->left, root2->right) && 16 | isIsomorphic(root1->right, root2->left) 17 | ); 18 | } 19 | else 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /september/SEP_09_Kth_largest_element_in_BST.cpp: -------------------------------------------------------------------------------- 1 | /*The Node structure is defined as 2 | struct Node { 3 | int data; 4 | Node *left; 5 | Node *right; 6 | 7 | Node(int val) { 8 | data = val; 9 | left = right = NULL; 10 | } 11 | }; 12 | */ 13 | 14 | // return the Kth largest element in the given BST rooted at 'root' 15 | class Solution 16 | { 17 | vector arr; 18 | void inorder(Node *root) 19 | { 20 | if (!root) 21 | return; 22 | inorder(root->right); 23 | arr.push_back(root->data); 24 | inorder(root->left); 25 | } 26 | 27 | public: 28 | int kthLargest(Node *root, int K) 29 | { 30 | // Your code here 31 | inorder(root); 32 | return arr[K - 1]; 33 | } 34 | }; -------------------------------------------------------------------------------- /september/SEP_03_Check_if_Tree_is_Isomorphic.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // Return True if the given trees are isomotphic. Else return False. 4 | bool isIsomorphic(Node *root1,Node *root2) 5 | { 6 | //add code here. 7 | if(!root1 && !root2) return true; 8 | if(!root1 || !root2) return false; 9 | 10 | if(root1->data == root2->data){ 11 | return ( 12 | isIsomorphic(root1->left, root2->left) && 13 | isIsomorphic(root1->right, root2->right) 14 | || 15 | isIsomorphic(root1->left, root2->right) && 16 | isIsomorphic(root1->right, root2->left) 17 | ); 18 | } 19 | else 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /october/OCT_17_Transitive_closure_of_a_Graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector> transitiveClosure(int N, vector> graph) 4 | { 5 | // code here 6 | for(int k = 0; k j 15 | 16 | graph[i][j] = 1; 17 | } 18 | } 19 | } 20 | } 21 | return graph; 22 | } 23 | }; -------------------------------------------------------------------------------- /september/SEP_27_Find_the_closest_pair_from_two_arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector printClosest(int arr[], int brr[], int n, int m, int x) { 4 | //code herej 5 | int left = 0 , right = m-1; 6 | int diff = INT_MAX; 7 | vector pair(2); 8 | while(left < n && right >= 0){ 9 | int sum = arr[left] + brr[right]; 10 | 11 | if(abs(sum - x) < diff){ 12 | diff = abs(sum - x); 13 | pair[0] = arr[left]; 14 | pair[1] = brr[right]; 15 | } 16 | 17 | if(sum < x){ 18 | left++; 19 | } 20 | else{ 21 | right--; 22 | } 23 | } 24 | return pair; 25 | } 26 | is 27 | }; -------------------------------------------------------------------------------- /august/AUG_19_Subarray_with_given_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to find a continuous sub-array which adds up to a given number. 5 | vector subarraySum(vector arr, int n, long long s) 6 | { 7 | // Your code here 8 | int sum = 0; 9 | vector ans; 10 | for (int i = 0, j = 0; j < n; j++) 11 | { 12 | sum += arr[j]; 13 | while (sum > s) 14 | { 15 | sum -= arr[i++]; 16 | } 17 | if (sum == s && i <= j) 18 | { 19 | ans.push_back(i + 1); 20 | ans.push_back(j + 1); 21 | return ans; 22 | } 23 | } 24 | ans.push_back(-1); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /october/OCT_02_Number_of_distinct_subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | int mod = 1e9+7; 3 | public: 4 | int distinctSubsequences(string s) 5 | { 6 | // Your code goes here 7 | int n = s.size(), dp[n+1]; 8 | 9 | unordered_map lastSeen; 10 | dp[0] = 1; 11 | for(int i = 1; i<=n; i++){ 12 | char ch = s[i-1]; 13 | 14 | dp[i] = (dp[i-1] * 2) % mod; 15 | 16 | if(lastSeen.find(ch) != lastSeen.end()){ 17 | int lastIndex = lastSeen[ch]; 18 | // + mod is used to avoid -ve resultant (+mod) % mod = cancelled 19 | dp[i] = (dp[i] - dp[lastIndex-1] + mod) % mod; 20 | } 21 | 22 | //last seen of ch at index i 23 | lastSeen[ch] = i; 24 | } 25 | return dp[n]; 26 | } 27 | }; -------------------------------------------------------------------------------- /october/OCT_27_Minimum_Deletions.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int minimumNumberOfDeletions(string str) { 4 | // code here 5 | string rev(str); 6 | reverse(rev.begin(), rev.end()); 7 | 8 | int n = str.size(); 9 | int dp[n+1][n+1]; 10 | 11 | for(int i = 0; i map; 8 | for (int i = 0; i < n; i++) 9 | { 10 | map[arr[i]]++; 11 | } 12 | unordered_set set; 13 | for (auto value : map) 14 | { 15 | // checking if same frequency is already present or not before inserting 16 | if (set.find(value.second) != set.end() && *set.find(value.second) == value.second) 17 | return false; 18 | 19 | set.insert(value.second); // inserting frequency 20 | // cout< x) 22 | { 23 | j--; 24 | freq--; 25 | } 26 | if (arr[i] == x && arr[j] == x) 27 | break; 28 | } 29 | return freq; 30 | } 31 | }; -------------------------------------------------------------------------------- /august/AUG_26_Longest_K_unique_characters_substring.cpp: -------------------------------------------------------------------------------- 1 | // User function template for C++ 2 | 3 | class Solution 4 | { 5 | public: 6 | int longestKSubstr(string s, int k) 7 | { 8 | // your code here 9 | if (k > s.size()) 10 | return -1; 11 | 12 | unordered_map map; 13 | int maxLength = -1, l = 0; 14 | 15 | for (int r = 0; r < s.size(); r++) 16 | { 17 | map[s[r]]++; 18 | 19 | while (map.size() > k) 20 | { 21 | map[s[l]]--; 22 | if (map[s[l]] == 0) 23 | map.erase(s[l]); 24 | l++; 25 | } 26 | if (map.size() == k) 27 | maxLength = max(maxLength, (r + 1) - l); 28 | } 29 | 30 | return maxLength; 31 | } 32 | }; -------------------------------------------------------------------------------- /july/JULY_31_BFS_of_graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to return Breadth First Traversal of given graph. 5 | vector bfsOfGraph(int V, vector adj[]) 6 | { 7 | // Code here 8 | vector ans; 9 | vector traversed(V, false); 10 | queue q; 11 | 12 | q.push(0); 13 | traversed[0] = true; 14 | 15 | while (!q.empty()) 16 | { 17 | int curr = q.front(); 18 | ans.push_back(curr); 19 | q.pop(); 20 | for (int i : adj[curr]) 21 | { 22 | if (!traversed[i]) 23 | { 24 | q.push(i); 25 | traversed[i] = true; 26 | } 27 | } 28 | } 29 | 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /september/SEP_06_Mother_Vertex.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to find a Mother Vertex in the Graph. 5 | void dfs(int i, set &v2, vector &vis, vector adj[]) 6 | { 7 | vis[i] = 1; 8 | v2.insert(i); 9 | for (auto adjNode : adj[i]) 10 | { 11 | if (v2.find(adjNode) == v2.end()) 12 | dfs(adjNode, v2, vis, adj); 13 | } 14 | } 15 | int findMotherVertex(int V, vector adj[]) 16 | { 17 | // Code here 18 | vector vis(V, 0); 19 | for (int i = 0; i < V; i++) 20 | { 21 | set v2; 22 | if (!vis[i]) 23 | { 24 | dfs(i, v2, vis, adj); 25 | } 26 | if (v2.size() == V) 27 | return i; 28 | } 29 | return -1; 30 | } 31 | }; -------------------------------------------------------------------------------- /august/AUG_01_DFS_of_Graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to return a list containing the DFS traversal of the graph. 5 | vector dfsOfGraph(int V, vector adj[]) 6 | { 7 | // Code here 8 | vector visited(V, false); 9 | vector ans; 10 | for (int i = 0; i < V; i++) 11 | { 12 | if (!visited[i]) 13 | bfs(adj, i, visited, ans); 14 | } 15 | return ans; 16 | } 17 | void bfs(vector adj[], int source, vector &visited, vector &ans) 18 | { 19 | visited[source] = true; 20 | ans.push_back(source); 21 | 22 | // now visited all adjacent of source 23 | for (int i : adj[source]) 24 | { 25 | if (!visited[i]) 26 | bfs(adj, i, visited, ans); 27 | } 28 | } 29 | }; -------------------------------------------------------------------------------- /october/OCT_07_Pairwise_swap_elements_of_a_linked_list.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | Node* pairWiseSwap(struct Node* head) 5 | { 6 | if(!head || !head->next) return head; 7 | 8 | Node *prev = head, *curr = head->next; 9 | head = curr; 10 | while(curr){ 11 | Node *nextNode = curr->next; 12 | curr->next = prev; 13 | 14 | if(!nextNode){ 15 | prev->next = NULL; 16 | break; 17 | } 18 | 19 | //odd 20 | if(!nextNode->next){ 21 | prev->next = nextNode; 22 | break; 23 | } 24 | 25 | prev->next = nextNode->next; 26 | prev = nextNode; 27 | curr = prev->next; 28 | } 29 | 30 | return head; 31 | } 32 | }; -------------------------------------------------------------------------------- /august/AUG_30_Delete_a_Node_in_Single_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* Link list Node 4 | struct Node 5 | { 6 | int data; 7 | struct Node* next; 8 | 9 | Node(int x){ 10 | data = x; 11 | next = NULL; 12 | } 13 | }; 14 | */ 15 | 16 | /*You are required to complete below method*/ 17 | Node *deleteNode(Node *head, int x) 18 | { 19 | // Your code here 20 | if (!head) 21 | return NULL; 22 | 23 | Node *toDelete = NULL; 24 | if (x == 1) 25 | { 26 | toDelete = head; 27 | head = head->next; 28 | delete (toDelete); 29 | return head; 30 | } 31 | Node *temp = head; 32 | 33 | for (int i = 1; i < x - 1; i++, temp = temp->next) 34 | ; 35 | 36 | toDelete = temp->next; 37 | temp->next = toDelete->next; 38 | delete (toDelete); 39 | return head; 40 | } -------------------------------------------------------------------------------- /october/OCT_01_Boundary_traversal_of_matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to return list of integers that form the boundary 5 | //traversal of the matrix in a clockwise manner. 6 | vector boundaryTraversal(vector > matrix, int n, int m) 7 | { 8 | // code here 9 | vector ans; 10 | for(int i = 0; i=0; i--){ 21 | ans.push_back(matrix[n-1][i]); 22 | } 23 | for(int i = n-2; i>0; i--){ 24 | ans.push_back(matrix[i][0]); 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /july/JULY_18_Longest_Repeating_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int LongestRepeatingSubsequence(string str) 5 | { 6 | // Code here 7 | int n = str.size(); 8 | int dp[n + 1][n + 1]; 9 | 10 | for (int i = 0; i < n + 1; i++) 11 | { 12 | for (int j = 0; j < n + 1; j++) 13 | { 14 | if (i == 0 || j == 0) 15 | { 16 | dp[i][j] = 0; 17 | } 18 | else if (str[i - 1] == str[j - 1] && j != i) 19 | { 20 | dp[i][j] = 1 + dp[i - 1][j - 1]; 21 | } 22 | else 23 | { 24 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 25 | } 26 | } 27 | } 28 | 29 | return dp[n][n]; 30 | } 31 | }; -------------------------------------------------------------------------------- /october/OCT_14_Find_Common_Nodes_in_two_BSTs.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | unordered_set set; 5 | vector ans; 6 | void inorder1(Node *root){ 7 | if(!root) return; 8 | 9 | inorder1(root->left); 10 | set.insert(root->data); 11 | inorder1(root->right); 12 | } 13 | 14 | void inorder2(Node *root){ 15 | 16 | if(!root) return; 17 | 18 | 19 | inorder2(root->left); 20 | if(set.find(root->data) != set.end()) 21 | ans.push_back(root->data); 22 | inorder2(root->right); 23 | } 24 | public: 25 | //Function to find the nodes that are common in both BST. 26 | vector findCommon(Node *root1, Node *root2) 27 | { 28 | //Your code here 29 | inorder1(root1); 30 | inorder2(root2); 31 | return ans; 32 | } 33 | }; 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /october/OCT_05_Count_number_of_substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int upto(string &str, int k){ 4 | int left = 0, right = 0, distinct = 0, ans = 0; 5 | vector map(26, 0); 6 | 7 | while(right < str.size()){ 8 | int i = str[right] - 'a'; 9 | map[i]++; 10 | 11 | if(map[i] == 1) 12 | distinct++; 13 | 14 | while(distinct > k){ 15 | int i = str[left++] - 'a'; 16 | map[i]--; 17 | 18 | if(map[i] == 0) 19 | distinct--; 20 | } 21 | 22 | ans += right - left + 1; 23 | right++; 24 | } 25 | return ans; 26 | } 27 | 28 | public: 29 | long long int substrCount (string s, int k) { 30 | return upto(s, k) - upto(s, k-1); 31 | } 32 | }; -------------------------------------------------------------------------------- /october/OCT_19_Level_of_Nodes.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to find the level of node X. 5 | int nodeLevel(int V, vector adj[], int X) 6 | { 7 | // code here 8 | queue q; 9 | vector visited(V, false); 10 | 11 | q.push(0); 12 | visited[0] = true; 13 | int level = 0; 14 | 15 | while(!q.empty()){ 16 | int size = q.size(); 17 | 18 | while(size--){ 19 | 20 | int curr = q.front(); 21 | q.pop(); 22 | 23 | if(curr == X) return level; 24 | for(auto v : adj[curr]){ 25 | if(!visited[v]){ 26 | visited[v] = true; 27 | q.push(v); 28 | } 29 | } 30 | 31 | } 32 | level++; 33 | } 34 | return -1; 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /september/SEP_14_Perfect_Sum_Problem.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int n, mod = 1e9 + 7; 4 | vector> dp; 5 | int solve(int i, int target, int arr[]) 6 | { 7 | // Base cases 8 | if (i >= n) 9 | { 10 | if (target == 0) 11 | return 1; 12 | return 0; 13 | } 14 | // target became -ve 15 | if (target < 0) 16 | return 0; 17 | 18 | if (dp[i][target] != -1) 19 | return dp[i][target]; 20 | 21 | int include = solve(i + 1, target - arr[i], arr) % mod; 22 | int exclude = solve(i + 1, target, arr) % mod; 23 | 24 | return dp[i][target] = (include + exclude) % mod; 25 | } 26 | 27 | public: 28 | int perfectSum(int arr[], int n, int sum) 29 | { 30 | this->n = n; 31 | dp.resize(n + 1, vector(sum + 1, -1)); 32 | return solve(0, sum, arr); 33 | } 34 | }; -------------------------------------------------------------------------------- /september/SEP_01_Leftmost_and_rightmost_nodes_of_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | /* Function to print corner node at each level */ 2 | 3 | /* 4 | Structure of a node is as following 5 | struct Node 6 | { 7 | int data; 8 | struct Node* left; 9 | struct Node* right; 10 | }; 11 | */ 12 | void printCorner(Node *root) 13 | { 14 | 15 | // Your code goes here 16 | queue q; 17 | q.push(root); 18 | 19 | while(!q.empty()){ 20 | int size = q.size(); 21 | bool leftmost = true; 22 | Node *curr; 23 | for(int i = 0; i < size; i++){ 24 | curr = q.front(); 25 | q.pop(); 26 | if(leftmost){ 27 | leftmost = false; 28 | cout<data<<" "; 29 | } 30 | 31 | if(curr->left) q.push(curr->left); 32 | if(curr->right) q.push(curr->right); 33 | } 34 | if(size > 1) 35 | cout<data<<" "; 36 | } 37 | } -------------------------------------------------------------------------------- /october/OCT_22_Number_of_paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long pow(long long x, int y, long long mod) { 5 | long long res = 1; 6 | while (y > 0) { 7 | if (y % 2 != 0) 8 | res = (res * x) % mod; 9 | 10 | y = y / 2; 11 | x = (x * x) % mod; 12 | } 13 | return res; 14 | } 15 | 16 | long long modInv(long long n, long long mod) { 17 | return pow(n, static_cast(mod - 2), mod); 18 | } 19 | 20 | 21 | long long numberOfPaths(int m, int n) 22 | { 23 | long long ans = 1; 24 | long long mod = 1000000007LL; // Use 'LL' to indicate a long long literal 25 | for (long long i = n; i <= (n + m - 2); i++) { 26 | ans = (ans * i) % mod; 27 | long long invert = modInv(i - n + 1, mod); 28 | ans = (ans * invert) % mod; 29 | } 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /october/OCT_25_Knapsack_with_Duplicate_Items.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | int n; 3 | vector> dp; 4 | int solve(int i, int capacity, int val[], int wt[]){ 5 | //not fit in bag || outbound 6 | if(capacity <= 0 || i >= n){ 7 | return 0; 8 | } 9 | 10 | //memoization 11 | if(dp[i][capacity] != -1) return dp[i][capacity]; 12 | 13 | //take 14 | int take = 0; 15 | if(capacity >= wt[i]) 16 | take = val[i] + solve(i, capacity - wt[i], val, wt); 17 | //note take 18 | int notTake = solve(i+1, capacity, val, wt); 19 | 20 | return dp[i][capacity] = max(take, notTake); 21 | } 22 | 23 | public: 24 | int knapSack(int N, int W, int val[], int wt[]) 25 | { 26 | // code here 27 | this->n = N; 28 | dp.resize(N+1, vector(W+1, -1)); 29 | return solve(0, W, val, wt); 30 | } 31 | }; -------------------------------------------------------------------------------- /june/JUNE_29_Next_Happy_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | //1234 4 | //1*1 + 2*2 + 3*3 + 4*4 5 | int sumOfSquare(int num){ 6 | int sum = 0; 7 | while(num){ 8 | int lastDigit = num % 10; 9 | sum += lastDigit*lastDigit; 10 | num /= 10; 11 | } 12 | return sum; 13 | } 14 | bool isHappy(int N){ 15 | while(true){ 16 | N = sumOfSquare(N); 17 | if(N == 20){ 18 | return false; 19 | } 20 | else if(N == 1){ 21 | return true; 22 | } 23 | } 24 | } 25 | int nextHappy(int N){ 26 | // code here 27 | N++; 28 | while(true) 29 | { 30 | if(isHappy(N)) 31 | { 32 | return N; 33 | } 34 | else 35 | { 36 | N++; 37 | } 38 | } 39 | } 40 | }; -------------------------------------------------------------------------------- /august/AUG_15_Flip_Bits.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution 4 | { 5 | public: 6 | int maxOnes(int a[], int n) 7 | { 8 | // kadan's algo 9 | // make 0 -> 1 10 | // 1 -> -1 11 | // 1 0 0 1 0 0 1 12 | //-1 1 1 -1 1 1 -1 13 | 14 | // 1 15 | int maxSub = INT_MIN; 16 | int sum = 0, ones = 0; 17 | for (int i = 0; i < n; i++) 18 | { 19 | int bit; 20 | if (a[i] == 0) 21 | bit = 1; 22 | else 23 | { 24 | bit = -1; 25 | ones++; 26 | } 27 | if (sum + bit >= bit) 28 | sum += bit; 29 | else 30 | sum = bit; 31 | maxSub = max(maxSub, sum); 32 | } 33 | // edge case 1 1 1 1 1 34 | // -1 -1 -1 -1 -1 35 | 36 | if (maxSub < 0) 37 | return ones; 38 | return ones + maxSub; 39 | } 40 | }; -------------------------------------------------------------------------------- /september/SEP_08_Binary_Tree_to_BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | vector arr; 4 | 5 | public: 6 | // The given root is the root of the Binary Tree 7 | // Return the root of the generated BST 8 | void inorder(Node *root) 9 | { 10 | if (!root) 11 | return; 12 | arr.push_back(root->data); 13 | inorder(root->left); 14 | inorder(root->right); 15 | } 16 | Node *insert(Node *root, int data) 17 | { 18 | if (!root) 19 | return new Node(data); 20 | if (data < root->data) 21 | root->left = insert(root->left, data); 22 | if (data > root->data) 23 | root->right = insert(root->right, data); 24 | return root; 25 | } 26 | Node *binaryTreeToBST(Node *root) 27 | { 28 | // Your code goes here 29 | inorder(root); 30 | Node *newRoot = NULL; 31 | for (auto data : arr) 32 | newRoot = insert(newRoot, data); 33 | 34 | return newRoot; 35 | } 36 | }; -------------------------------------------------------------------------------- /july/JULY_29_Median_of_BST.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | Structure of the binary Search Tree is as 5 | struct Node { 6 | int data; 7 | Node *left; 8 | Node *right; 9 | 10 | Node(int val) { 11 | data = val; 12 | left = right = NULL; 13 | } 14 | }; 15 | */ 16 | // your task is to complete the Function 17 | // Function should return median of the BST 18 | void traverse(struct Node *root, vector &arr) 19 | { 20 | if (root == NULL) 21 | return; 22 | traverse(root->left, arr); 23 | arr.push_back(root->data); 24 | traverse(root->right, arr); 25 | } 26 | float findMedian(struct Node *root) 27 | { 28 | // Code here 29 | vector arr; 30 | traverse(root, arr); 31 | int n = arr.size(); 32 | float ans; 33 | if ((n & 1) == 0) 34 | ans = (arr[n / 2 - 1] + arr[n / 2]) / 2.0; 35 | else 36 | ans = arr[n / 2]; 37 | // for(i : arr) 38 | // printf("%d ",i); 39 | // cout< > &matrix) 7 | { 8 | // 1 0 0 0 9 | // 1 0 0 0 10 | // 1 0 0 0 11 | // 1 0 1 0 12 | // 0 0 0 0 13 | 14 | vector row(matrix.size(), false), col(matrix[0].size(), false); 15 | 16 | for(int i = 0; i &prices) 5 | { 6 | // code here 7 | long long profit, buy; 8 | profit = 0; 9 | buy = prices[0]; 10 | for (int current = 1; current < n - 1; current++) 11 | { 12 | int pre = current - 1; 13 | int post = current + 1; 14 | // when to sell 15 | if (prices[current] > prices[pre] && prices[current] >= prices[post]) 16 | { 17 | profit += prices[current] - buy; 18 | buy = 0; 19 | } 20 | // when to buy 21 | if (prices[current] <= prices[pre] && prices[current] < prices[post]) 22 | { 23 | buy = prices[current]; 24 | } 25 | } 26 | // check if there any stock remains if so sel 27 | if (buy) 28 | { 29 | profit += prices[n - 1] - buy; 30 | } 31 | return (profit < 0) ? 0 : profit; 32 | } 33 | }; -------------------------------------------------------------------------------- /july/JULY_06_Quick_sort.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Function to sort an array using quick sort algorithm. 5 | void quickSort(int arr[], int low, int high) 6 | { 7 | // code here 8 | if (low > high) 9 | { 10 | return; 11 | } 12 | int pivot = partition(arr, low, high); 13 | quickSort(arr, low, pivot - 1); 14 | quickSort(arr, pivot + 1, high); 15 | } 16 | 17 | public: 18 | int partition(int arr[], int low, int high) 19 | { 20 | // Your code here 21 | int pivot = high; 22 | while (low < high) 23 | { 24 | while (arr[low] <= arr[pivot]) 25 | { 26 | low++; 27 | } 28 | while (arr[high] > arr[pivot]) 29 | { 30 | high--; 31 | } 32 | if (low < high) 33 | { 34 | swap(arr[low], arr[high]); 35 | } 36 | } 37 | swap(arr[pivot], arr[high]); 38 | return high; 39 | } 40 | }; -------------------------------------------------------------------------------- /august/AUG_10_Longest_Common_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | // function to find longest common subsequence 2 | 3 | class Solution 4 | { 5 | 6 | public: 7 | // Function to find the length of longest common subsequence in two strings. 8 | int lcs(int n, int m, string s1, string s2) 9 | { 10 | // your code here 11 | // E C B D E B D 12 | // B 0 0 0 0 0 0 0 0 13 | // C 0 14 | // A 0 15 | // C 0 16 | // C 0 17 | // B 0 18 | // D 0 19 | // A 0 20 | 21 | int dp[n + 1][m + 1]; 22 | for (int i = 0; i < (n + 1); i++) 23 | { 24 | for (int j = 0; j < (m + 1); j++) 25 | { 26 | if (i == 0 || j == 0) 27 | dp[i][j] = 0; 28 | else if (s1[i - 1] != s2[j - 1]) 29 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 30 | else 31 | dp[i][j] = 1 + dp[i - 1][j - 1]; 32 | } 33 | } 34 | return dp[n][m]; 35 | } 36 | }; -------------------------------------------------------------------------------- /july/JULY_08_Find_triplets_with_zero_sum.cpp: -------------------------------------------------------------------------------- 1 | /* You are required to complete the function below 2 | * arr[]: input array 3 | * n: size of array 4 | */ 5 | class Solution 6 | { 7 | public: 8 | // Function to find triplets with zero sum. 9 | bool findTriplets(int arr[], int n) 10 | { 11 | // Your code here 12 | sort(arr, arr + n); 13 | for (int i = 0; i < n; i++) 14 | { 15 | int j = i + 1; 16 | int k = n - 1; 17 | while (j < k) 18 | { 19 | int sum = arr[i] + arr[j] + arr[k]; 20 | // printf("(%d)+(%d)+(%d) = %d\n",arr[i],arr[j],arr[k],sum); 21 | if (sum > 0) 22 | { 23 | --k; 24 | } 25 | else if (sum < 0) 26 | { 27 | ++j; 28 | } 29 | else if (sum == 0) 30 | { 31 | return true; 32 | } 33 | } 34 | } 35 | // cout<= size) 44 | return -1; 45 | return arr[top2++]; 46 | } 47 | }; -------------------------------------------------------------------------------- /october/OCT_15_Normal_BST_to_Balanced_BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | vector list; 3 | Node *head = NULL; 4 | 5 | void inorder(Node *root){ 6 | if(!root) return; 7 | inorder(root->left); 8 | list.push_back(root->data); 9 | inorder(root->right); 10 | } 11 | 12 | Node *insert(Node *root, int data){ 13 | if(!root) return new Node(data); 14 | 15 | if(data < root->data){ 16 | root->left = insert(root->left, data); 17 | } 18 | else if(data > root->data){ 19 | root->right = insert(root->right, data); 20 | } 21 | return root; 22 | } 23 | 24 | void builtTree(int left, int right){ 25 | if(left > right) return; 26 | 27 | int mid = left + (right - left)/2; 28 | head = insert(head, list[mid]); 29 | 30 | builtTree(left, mid-1); 31 | builtTree(mid+1, right); 32 | } 33 | public: 34 | Node* buildBalancedTree(Node* root) 35 | { 36 | inorder(root); 37 | builtTree(0, list.size()-1); 38 | return head; 39 | } 40 | }; -------------------------------------------------------------------------------- /september/SEP_07_Minimum_Multiplications_to_reach_End.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int mod = 1e5; 4 | 5 | public: 6 | int minimumMultiplications(vector &arr, int start, int end) 7 | { 8 | // code here 9 | if (start == end) 10 | return 0; 11 | queue q; 12 | q.push(start); 13 | int level = 0; 14 | vector dp(mod, false); 15 | dp[start] = true; 16 | 17 | while (!q.empty()) 18 | { 19 | int size = q.size(); 20 | while (size--) 21 | { 22 | int x = q.front(); 23 | q.pop(); 24 | if (x == end) 25 | return level; 26 | 27 | for (auto i : arr) 28 | { 29 | int product = ((x % mod) * (i % mod)) % mod; 30 | if (!dp[product]) 31 | { 32 | q.push(product); 33 | dp[product] = true; 34 | } 35 | } 36 | } 37 | level++; 38 | } 39 | return -1; 40 | } 41 | }; -------------------------------------------------------------------------------- /july/JULY_22_Remove_duplicates_from_an_unsorted_linked_list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | The structure of linked list is the following 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | Node(int x) { 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | */ 13 | 14 | class Solution 15 | { 16 | Node *head; 17 | 18 | // p t 19 | // 5 2 2 4 1 5 20 | // . 21 | public: 22 | // Function to remove duplicates from unsorted linked list. 23 | Node *removeDuplicates(Node *head) 24 | { 25 | // your code goes here 26 | this->head = head; 27 | Node *temp = head; 28 | unordered_map map; //{data, frequency} 29 | Node *prev = NULL; 30 | while (temp != NULL) 31 | { 32 | int data = temp->data; 33 | map[data]++; 34 | 35 | if (map[data] > 1) 36 | { 37 | prev->next = prev->next->next; 38 | delete (temp); 39 | temp = prev; 40 | } 41 | prev = temp; 42 | temp = temp->next; 43 | } 44 | 45 | return this->head; 46 | } 47 | }; -------------------------------------------------------------------------------- /october/OCT_18_Eventual_Safe_States.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | //jo jo cycle ke part hai vo safe node nhi ho skta 3 | vector cyclicNode, visited, backtrack; 4 | 5 | bool isCyclic(int source, auto adj[]){ 6 | visited[source] = true; 7 | backtrack[source] = true; 8 | 9 | for(auto u : adj[source]){ 10 | if(visited[u] && backtrack[u]) return true; 11 | else if(!visited[u] && isCyclic(u, adj)) 12 | return cyclicNode[u] = true; 13 | } 14 | 15 | backtrack[source] = false; 16 | return false; 17 | } 18 | public: 19 | vector eventualSafeNodes(int V, vector adj[]) { 20 | // code here 21 | visited.resize(V, false); 22 | backtrack.resize(V, false); 23 | cyclicNode.resize(V, false); 24 | 25 | for(int i = 0; i ans; 31 | for(int i = 0; i> matrix, int n) 8 | { 9 | // code here 10 | // r1 6 c1 8 11 | // r2 9 c2 6 12 | // r3 6 c3 7 13 | 14 | vector row, col; 15 | int maxsum = INT_MIN; 16 | 17 | for (int i = 0; i < matrix.size(); i++) 18 | { 19 | 20 | int rowsum = 0, colsum = 0; 21 | for (int j = 0; j < matrix[0].size(); j++) 22 | { 23 | rowsum += matrix[i][j]; 24 | colsum += matrix[j][i]; 25 | } 26 | 27 | maxsum = max(maxsum, max(rowsum, colsum)); 28 | row.push_back(rowsum); 29 | col.push_back(colsum); 30 | } 31 | 32 | int ans = 0; 33 | for (int i = 0; i < row.size(); i++) 34 | { 35 | row[i] = abs(maxsum - row[i]); 36 | col[i] = abs(maxsum - col[i]); 37 | ans += col[i]; 38 | } 39 | 40 | return ans; 41 | } 42 | }; -------------------------------------------------------------------------------- /july/JULY_21_Reverse_a_linked_list_in_groups_of_given_size.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse a linked list 3 | The input list will have at least one element 4 | Return the node which points to the head of the new LinkedList 5 | Node is defined as 6 | struct node 7 | { 8 | int data; 9 | struct node* next; 10 | 11 | node(int x){ 12 | data = x; 13 | next = NULL; 14 | } 15 | 16 | }*head; 17 | */ 18 | 19 | class Solution 20 | { 21 | public: 22 | struct node *reverse(struct node *head, int k) 23 | { 24 | // Complete this method 25 | if (head == NULL || k == 1) 26 | return head; 27 | struct node *prev, *curr, *next; 28 | curr = head; 29 | prev = next = NULL; 30 | 31 | int temp = k; 32 | while (temp-- && curr != NULL) 33 | { 34 | next = curr->next; 35 | curr->next = prev; 36 | prev = curr; 37 | curr = next; 38 | } 39 | // h n 40 | // p c 41 | // N<-1<-2<-2<-4 5->6->7->8->N; 42 | head->next = reverse(curr, k); 43 | return prev; 44 | } 45 | }; -------------------------------------------------------------------------------- /september/SEP_15_Partition_Equal_subset_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int arraySum, totalPaths = 0; 4 | vector> dp; 5 | int solve(int i, int target, int arr[], int N) 6 | { 7 | if (totalPaths == 2) 8 | return 0; 9 | 10 | if (i >= N) 11 | { 12 | if (target == 0) 13 | { 14 | totalPaths++; 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | if (target < 0) 20 | return 0; 21 | if (dp[i][target] != -1) 22 | return dp[i][target]; 23 | 24 | int path1 = solve(i + 1, target - arr[i], arr, N); 25 | int path2 = solve(i + 1, target, arr, N); 26 | return dp[i][target] = path1 + path2; 27 | } 28 | 29 | public: 30 | int equalPartition(int N, int arr[]) 31 | { 32 | arraySum = accumulate(arr, arr + N, 0); 33 | // not possible is arraySum is Odd 34 | if ((arraySum & 1) == 1) 35 | return 0; 36 | 37 | int i = 0, target = arraySum / 2; 38 | dp.resize(N + 1, vector(target + 1, -1)); 39 | int paths = solve(i, target, arr, N); 40 | // cout<= 2); 42 | } 43 | }; -------------------------------------------------------------------------------- /july/JULY_23_sort_linked_list_of_0s_1s_and_2s.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Node is defined as 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | Node(int x) { 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | */ 14 | class Solution 15 | { 16 | public: 17 | // Function to sort a linked list of 0s, 1s and 2s. 18 | Node *segregate(Node *head) 19 | { 20 | 21 | // Add code here 22 | Node *temp = head; 23 | int zero = 0; 24 | int two = 0; 25 | int one = 0; 26 | while (temp != NULL) 27 | { 28 | if (temp->data == 1) 29 | one++; 30 | else if (temp->data == 2) 31 | two++; 32 | else 33 | zero++; 34 | temp = temp->next; 35 | } 36 | temp = head; 37 | while (zero--) 38 | { 39 | temp->data = 0; 40 | temp = temp->next; 41 | } 42 | while (one--) 43 | { 44 | temp->data = 1; 45 | temp = temp->next; 46 | } 47 | while (two--) 48 | { 49 | temp->data = 2; 50 | temp = temp->next; 51 | } 52 | return head; 53 | } 54 | }; -------------------------------------------------------------------------------- /november/NOV_3_Pythagorean_Triplet.cpp: -------------------------------------------------------------------------------- 1 | class solution{ 2 | public: 3 | bool checkTriplet(int arr[], int n) { 4 | // code here 5 | 6 | //APPROACH 1 not gonna accepted 7 | sort(arr, arr+n); 8 | for(int i = 0; i= 0; i--){ 12 | int targetSum = arr[i]; 13 | int left = 0, right = i-1; 14 | 15 | //now the question requice to 'FIND PAIR EQUL TO TARGET SUM' 16 | while(left < right){ 17 | int sum = arr[left] + arr[right]; 18 | 19 | if(sum == targetSum){ 20 | return true; 21 | } 22 | else if(sum > targetSum){ 23 | right--; 24 | } 25 | else{ 26 | left++; 27 | } 28 | } 29 | } 30 | return false; 31 | 32 | 33 | // APPROACH 2 34 | 35 | unordered_set set; 36 | for(int i = 0; i postMax[n - i]) 29 | { 30 | postMax[n - 1 - i] = arr[n - 1 - i]; 31 | } 32 | else 33 | { 34 | postMax[n - 1 - i] = postMax[n - i]; 35 | } 36 | } 37 | int ans = 0, i = 0, j = 0; 38 | while (i < n && j < n) 39 | { 40 | if (postMax[j] >= preMin[i]) 41 | { 42 | ans = max(ans, j - i); 43 | j++; 44 | } 45 | else 46 | { 47 | i++; 48 | } 49 | } 50 | return ans; 51 | } 52 | }; -------------------------------------------------------------------------------- /august/AUG_29_Delete_nodes_having_greater_value_on_right.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The structure of linked list is the following 4 | 5 | struct Node 6 | { 7 | int data; 8 | Node* next; 9 | 10 | Node(int x){ 11 | data = x; 12 | next = NULL; 13 | } 14 | }; 15 | */ 16 | class Solution 17 | { 18 | void reverse(Node *&head) 19 | { 20 | Node *prev = NULL, *curr = head; 21 | while (curr) 22 | { 23 | Node *nextnode = curr->next; 24 | curr->next = prev; 25 | prev = curr; 26 | curr = nextnode; 27 | } 28 | head = prev; 29 | } 30 | void deleteNode(Node *prev, Node *toDelete) 31 | { 32 | prev->next = toDelete->next; 33 | delete (toDelete); 34 | } 35 | 36 | public: 37 | Node *compute(Node *head) 38 | { 39 | // your code goes here 40 | 41 | reverse(head); 42 | // c 43 | // 3->2->6->5->11->10->15->12 44 | Node *curr = head; 45 | while (curr) 46 | { 47 | if (curr->next && curr->next->data < curr->data) 48 | { 49 | deleteNode(curr, curr->next); 50 | } 51 | else 52 | curr = curr->next; 53 | } 54 | reverse(head); 55 | return head; 56 | } 57 | }; -------------------------------------------------------------------------------- /november/NOV_2_Minimum_distance_between_two_numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int minDist(int a[], int n, int x, int y) { 4 | // code here 5 | // unordered_map lastSeen; 6 | 7 | // int ans = INT_MAX; 8 | // for(int i = 0; inext){ 6 | tail->next = newNode; 7 | } 8 | else{ 9 | struct Node *nextNode = tail->next; 10 | tail->next = newNode; 11 | newNode->next = nextNode; 12 | } 13 | } 14 | public: 15 | void rearrange(struct Node *odd) 16 | { 17 | 18 | //10->4->9->1->3->5->9->4 ->4 19 | // 4 1 5 4 alt nodes 20 | 21 | //10->9->3->9 22 | 23 | //reverse alt and append to the back 24 | //10->9->3->9->4->5->1->4 25 | 26 | if(!odd || !odd->next) return; 27 | 28 | tail = odd; 29 | while(tail->next != NULL){ 30 | tail = tail->next; 31 | } 32 | 33 | struct Node *curr = odd->next, *prev = odd; 34 | int i = 2; 35 | 36 | while(curr != tail){ 37 | if(i % 2 == 0){ 38 | prev->next = curr->next; 39 | curr->next = NULL; 40 | 41 | push_back(curr); 42 | curr = prev; 43 | } 44 | 45 | prev = curr; 46 | curr = curr->next; 47 | i++; 48 | } 49 | } 50 | }; -------------------------------------------------------------------------------- /july/JULY_24_Right_view_of_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | /* A binary tree node has data, pointer to left child 2 | and a pointer to right child 3 | struct Node 4 | { 5 | int data; 6 | struct Node* left; 7 | struct Node* right; 8 | 9 | Node(int x){ 10 | data = x; 11 | left = right = NULL; 12 | } 13 | }; */ 14 | 15 | // Should return right view of tree 16 | class Solution 17 | { 18 | unordered_map map; 19 | int level = 1; 20 | void helper(Node *root) 21 | { 22 | if (root == NULL) 23 | { 24 | level--; 25 | return; 26 | } 27 | // if no element is present at such level 28 | if (map.find(level) == map.end()) 29 | { 30 | map[level] = root->data; 31 | } 32 | // printf("%d %d\n",level,root->data); 33 | level++; 34 | helper(root->right); 35 | level++; 36 | helper(root->left); 37 | level--; 38 | } 39 | 40 | public: 41 | // Function to return list containing elements of right view of binary tree. 42 | vector rightView(Node *root) 43 | { 44 | // Your Code here 45 | helper(root); 46 | vector ans; 47 | for (int i = 1; i <= map.size(); i++) 48 | { 49 | ans.push_back(map[i]); 50 | } 51 | return ans; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /august/AUG_08_Fraction_pairs_with_sum1.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | class Solution 3 | { 4 | int getGCD(int x, int y) 5 | { 6 | if (x % y == 0) 7 | return y; 8 | return getGCD(y % x, x); 9 | } 10 | 11 | public: 12 | void print(int arr[], int n) 13 | { 14 | for (int i = 0; i < n; i++) 15 | cout << arr[i] << " "; 16 | cout << endl; 17 | } 18 | // hash class 19 | 20 | int countFractions(int n, int numerator[], int denominator[]) 21 | { 22 | // STEP 1 (SIMPLIFY) 23 | // simplify like 4/8 = 1/2; 8/12 = 2/3; 24 | for (int i = 0; i < n; i++) 25 | { 26 | int gcd = getGCD(numerator[i], denominator[i]); 27 | numerator[i] /= gcd; 28 | denominator[i] /= gcd; 29 | } 30 | // print(numerator,n); 31 | // print(denominator,n); 32 | 33 | // NEXT TO DO 34 | 35 | map, int> map; 36 | int ans = 0; 37 | 38 | for (int i = 0; i < n; i++) 39 | { 40 | int n = numerator[i], d = denominator[i]; 41 | // if {2:3} then need {1:3} more to be one 42 | int needToBeOne = d - n; 43 | pair p = {needToBeOne, d}; 44 | if (map.find(p) != map.end()) 45 | ans += map[p]; 46 | 47 | map[{n, d}]++; 48 | } 49 | return ans; 50 | } 51 | }; -------------------------------------------------------------------------------- /october/OCT_13_Floor_in_BST.cpp: -------------------------------------------------------------------------------- 1 | // ------------------ Brute Force Approach ------------------------ 2 | 3 | // Function to search a node in BST. 4 | // class Solution{ 5 | // vector arr; 6 | // void inorder(Node *root){ 7 | // if(!root)return; 8 | 9 | // inorder(root->left); 10 | // arr.push_back(root->data); 11 | // inorder(root->right); 12 | // } 13 | // public: 14 | // int floor(Node* root, int x) { 15 | // // Code here 16 | // inorder(root); 17 | // int ans = -1; 18 | // for(auto i : arr){ 19 | // if(i > x){ 20 | // return ans; 21 | // } 22 | // ans = i; 23 | // } 24 | // return ans; 25 | // } 26 | // }; 27 | 28 | 29 | class Solution{ 30 | void find(Node *root, int x,int &ans){ 31 | if(!root) return; 32 | 33 | if(root->data > x){ 34 | //then look for another subTree 35 | find(root->left, x, ans); 36 | } 37 | else if(root->data < x){ 38 | //it may be an ans 39 | ans = root->data; 40 | find(root->right, x, ans); 41 | } 42 | else{ 43 | ans = root->data; 44 | return; 45 | } 46 | } 47 | public: 48 | int floor(Node* root, int x) { 49 | // Code here 50 | int ans = -1; 51 | find(root, x, ans); 52 | return ans; 53 | } 54 | }; -------------------------------------------------------------------------------- /september/SEP_29_Number_Of_Enclaves.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void dfs(int i, int j, vector> &grid){ 3 | if(i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size() || grid[i][j] == 0) 4 | return; 5 | 6 | //marked as visited 7 | grid[i][j] = 0; 8 | 9 | dfs(i-1, j, grid);//up 10 | dfs(i+1, j, grid);//down 11 | dfs(i, j-1, grid);//left 12 | dfs(i, j+1, grid);//right 13 | } 14 | public: 15 | int numberOfEnclaves(vector> &grid) { 16 | int n = grid.size(), m = grid[0].size(); 17 | 18 | //traversing horizontal boundary 19 | for(int i = 0; i arr[largest]) 16 | largest = left; 17 | if (right < n && arr[right] > arr[largest]) 18 | largest = right; 19 | 20 | if (i == largest) 21 | return; 22 | swap(arr[i], arr[largest]); 23 | heapify(arr, n, largest); 24 | } 25 | 26 | public: 27 | // Function to build a Heap from array. 28 | void buildHeap(int arr[], int n) 29 | { 30 | // Your Code Here 31 | // last child will be at n-1 32 | // parent(n-1) = ((n-1) - 1)/2 33 | for (int i = ((n - 1) - 1) / 2; i >= 0; i--) 34 | { 35 | heapify(arr, n, i); 36 | } 37 | } 38 | 39 | public: 40 | // Function to sort an array using Heap Sort. 41 | void heapSort(int arr[], int n) 42 | { 43 | // code here 44 | buildHeap(arr, n); 45 | for (int i = n - 1; i > 0; i--) 46 | { 47 | swap(arr[0], arr[i]); 48 | heapify(arr, i, 0); 49 | } 50 | } 51 | }; -------------------------------------------------------------------------------- /august/AUG_24_Multiply_two_strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | /*You are required to complete below function */ 5 | string multiplyStrings(string s1, string s2) 6 | { 7 | // poping minus 8 | bool minus = false; 9 | if (s1[0] == '-' && s2[0] != '-') 10 | { 11 | s1.erase(s1.begin()); 12 | minus = true; 13 | } 14 | if (s1[0] != '-' && s2[0] == '-') 15 | { 16 | s2.erase(s2.begin()); 17 | minus = true; 18 | } 19 | if (s1[0] == '-' && s2[0] == '-') 20 | { 21 | s1.erase(s1.begin()); 22 | s2.erase(s2.begin()); 23 | } 24 | 25 | vector product(s1.size() + s2.size()); 26 | for (int i = s1.size() - 1; i >= 0; i--) 27 | { 28 | int x = s1[i] - '0'; 29 | for (int j = s2.size() - 1; j >= 0; j--) 30 | { 31 | product[i + j + 1] += x * (s2[j] - '0'); 32 | product[i + j] += product[i + j + 1] / 10; 33 | product[i + j + 1] %= 10; 34 | } 35 | } 36 | 37 | int i = 0; 38 | while (i < product.size() && product[i] == 0) 39 | i++; 40 | 41 | string result; 42 | if (minus) 43 | result.push_back('-'); 44 | 45 | while (i < product.size()) 46 | { 47 | result.push_back(product[i] + '0'); 48 | i++; 49 | } 50 | 51 | return result; 52 | } 53 | }; -------------------------------------------------------------------------------- /october/OCT_10_ Nodes_at_given_distance_in_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | private: 4 | int T, K, foundAt = -1; 5 | void collect(Node *root, int d, vector &ans) 6 | { 7 | if(!root) return; 8 | // if(d <= 0) return; 9 | 10 | if(d == 0){ 11 | ans.push_back(root->data); 12 | return; 13 | } 14 | 15 | collect(root->left, d-1, ans); 16 | collect(root->right, d-1, ans); 17 | } 18 | bool find(Node *root, vector &ans, int level) 19 | { 20 | if (!root) 21 | return false; 22 | 23 | if (root->data == T) 24 | { 25 | foundAt = level; 26 | collect(root, K, ans); 27 | return true; 28 | } 29 | 30 | if (find(root->left, ans, level+1)) 31 | { 32 | int d = foundAt - level; 33 | if(d == K) ans.push_back(root->data); 34 | else collect(root->right,K - d - 1, ans);//-1 for right 35 | return true; 36 | } 37 | 38 | if (find(root->right, ans, level+1)) 39 | { 40 | int d = foundAt - level; 41 | if(d == K) ans.push_back(root->data); 42 | else collect(root->left,K - d - 1, ans);//-1 for ->left 43 | return true; 44 | } 45 | return false; 46 | } 47 | 48 | public: 49 | vector KDistanceNodes(Node *root, int target, int k) 50 | { 51 | vector ans; 52 | this->T = target; 53 | this->K = k; 54 | 55 | find(root, ans, 0); 56 | sort(ans.begin(), ans.end()); 57 | return ans; 58 | } 59 | }; -------------------------------------------------------------------------------- /july/JULY_11_Find_kth_element_of_spiral_matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | /*You are required to complete this method*/ 5 | int findK(int a[MAX][MAX], int n, int m, int k) 6 | { 7 | // Your code goes here. 8 | int count = 0; 9 | for (int i = 0; i <= n / 2; i++) 10 | { 11 | //---->> 12 | for (int J = i; J < m - i; J++) 13 | { 14 | count++; 15 | if (count == k) 16 | return a[i][J]; 17 | // cout<= i; L--) 33 | { 34 | count++; 35 | if (count == k) 36 | return a[n - (1 + i)][L]; 37 | // cout< i; M--) 43 | { 44 | count++; 45 | if (count == k) 46 | return a[M][i]; 47 | // cout<data == node) 21 | { 22 | nodeAt = currLevel; 23 | --currLevel; 24 | return; 25 | } 26 | int currNode = root->data; 27 | traverse(root->left, ++currLevel, k, node, nodeAt, ans); 28 | if (currLevel == (nodeAt - k) and ans == -1) 29 | ans = currNode; 30 | 31 | traverse(root->right, ++currLevel, k, node, nodeAt, ans); 32 | if (currLevel == (nodeAt - k) and ans == -1) 33 | ans = currNode; 34 | 35 | --currLevel; 36 | } 37 | int kthAncestor(Node *root, int k, int node) 38 | { 39 | // Code here 40 | 41 | // step1 node ko find kro 42 | // let, this is the path of recursive call 43 | // root 44 | // \ 45 | // \ 46 | // / 47 | // \ 48 | // \ 49 | // key 50 | 51 | // ab lautenge bhi isi path se 52 | // lautate samay ki ky ap kth ancesstor ho yani is currleve == (keyAt - k) 53 | 54 | int currLevel = 1, valueAt = -1, nodeAt = -1, ans = -1; 55 | traverse(root, currLevel, k, node, nodeAt, ans); 56 | // printf("nodeAt %d\nancesstor %d\n",nodeAt, nodeAt-k); 57 | return ans; 58 | } -------------------------------------------------------------------------------- /july/JULY_17_First_non_repeating_character_in_a_stream.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | // get first single frequency 4 | char getfsf(string str, int size) 5 | { 6 | unordered_map map; 7 | for (int i = 0; i <= size; i++) 8 | { 9 | // searching single frequncy char by char 10 | if (map[str[i]] == 1) 11 | { 12 | return str[i]; 13 | } 14 | } 15 | return '#'; 16 | } 17 | 18 | public: 19 | string FirstNonRepeating(string A) 20 | { 21 | // Code here 22 | string result; 23 | // APPROCH 1 24 | // for(int i = 0; i freqCounter(26, 0); 30 | queue q; 31 | for (auto i : A) 32 | { 33 | // mentain freq 34 | // 'a' - 'a' = 0; 35 | // 'b' - 'a' = 1; 36 | // 'c' - 'a' = 3; 37 | // and so on..... 38 | freqCounter[i - 'a']++; 39 | 40 | // containing only valid char 41 | if (freqCounter[i - 'a'] == 1) 42 | q.push(i); 43 | 44 | // jabtak queue ke front ke element ka freq > 1 then nikal do 45 | while (!q.empty() && freqCounter[q.front() - 'a'] > 1) 46 | { 47 | q.pop(); 48 | } 49 | 50 | if (!q.empty()) 51 | result += q.front(); 52 | else 53 | result += '#'; 54 | } 55 | return result; 56 | } 57 | }; -------------------------------------------------------------------------------- /july/JULY_30_Inorder_Successor_in_BST.cpp: -------------------------------------------------------------------------------- 1 | /*The structure of Node 2 | 3 | struct Node { 4 | int data; 5 | Node *left; 6 | Node *right; 7 | 8 | Node(int val) { 9 | data = val; 10 | left = right = NULL; 11 | } 12 | }; 13 | */ 14 | 15 | class Solution 16 | { 17 | Node *ans = NULL; 18 | bool inRight = false, ancestor = false; 19 | 20 | void traverse(Node *root, Node *x) 21 | { 22 | if (root == NULL) 23 | { 24 | return; 25 | } 26 | 27 | if (root == x) 28 | { 29 | // ai. in case right NULL ho tab inorder direct apne ancestor ke pas jata hai 30 | if (root->right == NULL) 31 | { 32 | ancestor = true; 33 | return; 34 | } 35 | // bi. iska mtlb successor right sub tree me present hai 36 | else if (root->right != NULL) 37 | { 38 | inRight = true; 39 | traverse(root->right, x); 40 | } 41 | } 42 | 43 | traverse(root->left, x); 44 | 45 | if (inRight && ans == NULL) 46 | { 47 | ans = root; 48 | inRight = false; 49 | } 50 | if (ancestor && ans == NULL) 51 | { 52 | ans = root; 53 | ancestor = false; 54 | } 55 | 56 | traverse(root->right, x); 57 | } 58 | 59 | public: 60 | // returns the inorder successor of the Node x in BST (rooted at 'root') 61 | Node *inOrderSuccessor(Node *root, Node *x) 62 | { 63 | // Your code here 64 | traverse(root, x); 65 | return ans; 66 | } 67 | }; -------------------------------------------------------------------------------- /september/SEP_04_Replace_Os_with_Xs.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | void dfs(int i, int j, vector> &mat){ 3 | //handling out bound 4 | if(i < 0 || j < 0 || i >= mat.size() || j >= mat[0].size()) 5 | return; 6 | 7 | //base case 8 | if(mat[i][j] != 'O') 9 | return; 10 | 11 | //mark as visited (INVALID) as it coming from boundaries 12 | mat[i][j] = 'I'; 13 | 14 | dfs(i-1, j, mat);//top 15 | dfs(i+1, j, mat);//bottom 16 | dfs(i, j-1, mat);//left 17 | dfs(i, j+1, mat);//right 18 | } 19 | public: 20 | vector> fill(int n, int m, vector> mat) 21 | { 22 | // code here 23 | //enter from boundary and start traversing 24 | 25 | //traversing the boundaris 26 | //and entering from outer most opening 27 | for(int i = 0; i> fourSum(vector& arr, int k) { 4 | sort(arr.begin(), arr.end()); 5 | vector> ans; 6 | int n = arr.size(); 7 | 8 | for(int i = 0; i < n - 3; i++) { 9 | //we can't check next to prevent duplicate 10 | if(i > 0 && arr[i] == arr[i-1]) 11 | continue;//preventing duplication 12 | 13 | for(int j = i+1; j < n - 2; j++) { 14 | if(j > i+1 && arr[j] == arr[j-1]) 15 | continue;//preventing duplication 16 | 17 | int left = j+1; 18 | int right = n-1; 19 | int targetSum = k - (arr[i] + arr[j]); 20 | 21 | while(left < right){ 22 | int sum = arr[left] + arr[right]; 23 | if(sum == targetSum){ 24 | ans.push_back({arr[i], arr[j], arr[left], arr[right]}); 25 | 26 | //preventing duplication 27 | while(left < right && arr[left] == arr[left+1]) 28 | left++; 29 | while(left < right && arr[right] == arr[right-1]) 30 | right--; 31 | 32 | left++; 33 | right--; 34 | } 35 | else if(sum < targetSum){ 36 | left++; 37 | } 38 | else{ 39 | right--; 40 | } 41 | } 42 | } 43 | } 44 | 45 | return ans; 46 | } 47 | }; -------------------------------------------------------------------------------- /september/SEP_22_First_and_last_occurrences_of_x.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //lower bound is the first occurance of number (x == arr[i]) 4 | //or if a number is greater (x > arr[i]) {if x not present, it could be the ans} 5 | //both could be possibe to be ans, so condition will be 💡(arr[i] >= x) 6 | int lowerBound(int arr[], int n, int x){ 7 | int ans = n; 8 | int low = 0, high = n-1; 9 | while(low <= high){ 10 | int mid = (low + high)/2; 11 | 12 | if(arr[mid] >= x){ 13 | ans = mid; 14 | high = mid - 1; 15 | } 16 | else{ 17 | low = mid + 1; 18 | } 19 | } 20 | return ans; 21 | } 22 | 23 | 24 | 25 | //lower bound is the number greater than x 26 | //so the only condition will be 💡(arr[i] ) 27 | 28 | int upperBound(int arr[], int n, int x){ 29 | int ans = n; 30 | int low = 0, high = n-1; 31 | while(low <= high){ 32 | int mid = (low + high)/2; 33 | 34 | if(arr[mid] > x){ 35 | ans = mid; 36 | high = mid - 1; 37 | } 38 | else{ 39 | low = mid + 1; 40 | } 41 | } 42 | return ans; 43 | } 44 | 45 | 46 | //only difference in those function is the sign (>=), (>) 47 | public: 48 | vector find(int arr[], int n , int x ) 49 | { 50 | //💡 it's about finding the lowerBound(x) and upperBound(x), isn't it 51 | int l = lowerBound(arr, n, x); 52 | int u = upperBound(arr, n, x); 53 | 54 | if(arr[l] != x)// !found 55 | return {-1, -1}; 56 | 57 | 58 | return {l, u-1}; 59 | } 60 | }; -------------------------------------------------------------------------------- /august/AUG_11_Coin_Change.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | // map , int> dp; 4 | long long int solve(int need, int coins[], int i, auto &dp) 5 | { 6 | // BASE CASES 7 | if (i < 0 || need < 0) 8 | return 0; 9 | // need pura hua 10 | if (need == 0) 11 | return 1; 12 | 13 | // pair p = {need, i}; 14 | // if(dp.find(p) != dp.end()) return dp[p]; 15 | 16 | if (dp[need][i] != -1) 17 | return dp[need][i]; 18 | // coin taken 19 | long long int taken = solve(need - coins[i], coins, i, dp); 20 | // coin not taken 21 | long long int notTaken = solve(need, coins, i - 1, dp); 22 | 23 | return dp[need][i] = taken + notTaken; 24 | } 25 | 26 | public: 27 | long long int count(int coins[], int N, int sum) 28 | { 29 | 30 | // code here. 31 | // vector> dp(sum + 1, vector(N+1, -1)); 32 | // return solve(sum, coins, N-1, dp); 33 | 34 | vector dp(sum + 1, 0); 35 | dp[0] = 1; 36 | // PERMUTATION EXIST LIKE:- FOR 3 {1 : 2}, {2 : 1} 37 | 38 | // for(int i = 1; i< sum + 1; i++){ 39 | // for(int j = 0; j> &levelAndData, int &level, int &heightOfTree) 19 | { 20 | if (root == NULL) 21 | { 22 | --level; 23 | return; 24 | } 25 | heightOfTree = max(heightOfTree, level); 26 | levelAndData.push_back({level, root->data}); 27 | traverse(root->left, levelAndData, ++level, heightOfTree); 28 | traverse(root->right, levelAndData, ++level, heightOfTree); 29 | --level; 30 | } 31 | bool cmp(pair x, pair y) 32 | { 33 | return x.first < y.first; 34 | } 35 | vector findSpiral(Node *root) 36 | { 37 | // Your code here 38 | int heightOfTree = -1; 39 | vector> levelAndData; 40 | vector ans; 41 | int level = 0; 42 | 43 | traverse(root, levelAndData, level, heightOfTree); 44 | 45 | stack stack; 46 | for (int i = 0; i <= heightOfTree; i++) 47 | { 48 | for (auto j : levelAndData) 49 | { 50 | 51 | if (i == j.first) 52 | { 53 | if (i % 2 == 0) 54 | { 55 | stack.push(j.second); 56 | } 57 | else 58 | { 59 | while (!stack.empty()) 60 | { 61 | ans.push_back(stack.top()); 62 | stack.pop(); 63 | } 64 | ans.push_back(j.second); 65 | } 66 | } 67 | } 68 | } 69 | while (!stack.empty()) 70 | { 71 | ans.push_back(stack.top()); 72 | stack.pop(); 73 | } 74 | return ans; 75 | } -------------------------------------------------------------------------------- /august/AUG_17_Next_Smallest_palindrome.cpp: -------------------------------------------------------------------------------- 1 | // User function template for C++ 2 | class Solution 3 | { 4 | int carry = 0; 5 | void add(int num[], int val, int n) 6 | { 7 | for (int i = n; i >= 0; i--) 8 | { 9 | num[i] += carry + val; 10 | carry = val = 0; 11 | carry = num[i] / 10; 12 | num[i] %= 10; 13 | } 14 | } 15 | 16 | public: 17 | vector generateNextPalindrome(int num[], int n) 18 | { 19 | // code here 20 | bool solved = false; 21 | vector ans; 22 | for (int i = n / 2; i < n; i++) 23 | { 24 | int j = n - 1 - i; 25 | if (num[i] < num[j]) 26 | { 27 | num[i++] = num[j--]; 28 | while (i < n) 29 | { 30 | num[i++] = num[j--]; 31 | } 32 | solved = true; 33 | break; 34 | } 35 | else if (num[i] > num[j]) 36 | break; 37 | } 38 | 39 | // agr upar ke else se break hua hai tab abhi solve krna baki hai 40 | if (solved) 41 | { 42 | for (int i = 0; i < n; i++) 43 | { 44 | ans.push_back(num[i]); 45 | } 46 | return ans; 47 | } 48 | // odd or even dono isi me cover ho jayega 49 | else 50 | { 51 | int mid = (n - 1) / 2; 52 | add(num, 1, mid); 53 | for (int i = mid + 1; i < n; i++) 54 | { 55 | num[i] = num[n - 1 - i]; 56 | } 57 | // 9 9 9 9 9 58 | // + 59 | // 0 0 0 9 9 carry = 1 60 | // 1 0 0 0 9 9 num 61 | // 1 0 0 0 0 0 ans 62 | if (carry) 63 | ans.push_back(carry); 64 | for (int i = 0; i < n; i++) 65 | { 66 | ans.push_back(num[i]); 67 | } 68 | if (carry) 69 | ans[n] = 1; 70 | return ans; 71 | } 72 | } 73 | }; -------------------------------------------------------------------------------- /october/OCT_12_Duplicate_subtree_in_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | /*The structure of the Binary Tree Node is 2 | struct Node 3 | { 4 | char data; 5 | struct Node* left; 6 | struct Node* right; 7 | };*/ 8 | 9 | // ---------------- Brouth Forch Approach ----------------- 10 | //Time Complexity N² 11 | //Auxillary Space N² 12 | 13 | class Solution { 14 | void inorder(Node *root, vector &vec){ 15 | if(!root) return; 16 | 17 | inorder(root->left, vec); 18 | vec.push_back(root->data); 19 | inorder(root->right, vec); 20 | } 21 | 22 | void solve(Node *root, vector> &vecs){ 23 | if(!root) return; 24 | 25 | vector vec; 26 | inorder(root, vec); 27 | 28 | if(vec.size() > 1) 29 | vecs.push_back(vec); 30 | 31 | solve(root->left, vecs); 32 | solve(root->right, vecs); 33 | } 34 | public: 35 | int dupSub(Node *root) { 36 | vector> vecs; 37 | //at each node we traverse(inorder) whole sub tree N² 38 | solve(root, vecs); 39 | 40 | //N² to find dublicate vec 41 | for(int i = 0; i map; 59 | 60 | string solve(Node *root){ 61 | if(!root) return ""; 62 | 63 | string left = solve(root->left); 64 | string right = solve(root->right); 65 | string ans = left + to_string(root->data) + right; 66 | 67 | if(root->left || root->right) 68 | map[ans]++; 69 | 70 | return ans; 71 | } 72 | 73 | public: 74 | int dupSub(Node *root) { 75 | solve(root); 76 | 77 | for(auto p : map){ 78 | if(p.second > 1) 79 | return 1; 80 | } 81 | return 0; 82 | } 83 | }; -------------------------------------------------------------------------------- /august/AUG_02_Shortest_Source_to_Destination_Path.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | 3 | class Solution { 4 | int N, M; 5 | bool isValid(int i, int j){ 6 | if(i<0 || j < 0 || i >= N || j >= M) 7 | return false; 8 | return true; 9 | } 10 | public: 11 | int shortestDistance(int N, int M, vector> A, int X, int Y) { 12 | // code here 13 | //base case 14 | this->N = N; 15 | this->M = M; 16 | if(A[0][0] == 0) 17 | return -1; 18 | 19 | vector> visited(N, vector(M, false)); 20 | queue> q; 21 | q.push({0,0}); 22 | visited[0][0] = true; 23 | int steps = 0; 24 | 25 | while(!q.empty()){ 26 | //now q containg the vertices of first level 27 | //while !traversed all the vertices of first level 28 | int size = q.size(); 29 | while(size--){ 30 | int i, j; 31 | i = q.front().first; 32 | j = q.front().second; 33 | q.pop(); 34 | 35 | //now visite all the adjacent of current 36 | if(i == X && j == Y) 37 | return steps; 38 | 39 | //left 40 | if(isValid(i, j-1) && !visited[i][j-1] && A[i][j-1] == 1){ 41 | visited[i][j-1] = true; 42 | q.push({i, j-1}); 43 | } 44 | //right 45 | if(isValid(i, j+1) && !visited[i][j+1] && A[i][j+1] == 1){ 46 | visited[i][j+1] = true; 47 | q.push({i, j+1}); 48 | } 49 | //up 50 | if(isValid(i-1, j) && !visited[i-1][j] && A[i-1][j] == 1){ 51 | visited[i-1][j] = true; 52 | q.push({i-1, j}); 53 | } 54 | //down 55 | if(isValid(i+1, j) && !visited[i+1][j] && A[i+1][j] == 1){ 56 | visited[i+1][j] = true; 57 | q.push({i+1, j}); 58 | } 59 | } 60 | steps++; 61 | } 62 | return -1; 63 | } 64 | }; -------------------------------------------------------------------------------- /october/OCT_24_Palindromic_Partitioning.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int palindromicPartition(string str) 4 | { 5 | int n = str.size(); 6 | vector> isPalindrome(n, vector(n, false)); 7 | 8 | for(int gap = 0; gap < n; gap++){ 9 | for(int i = 0, j = gap; j < n; i++, j++){ 10 | if(gap == 0){ 11 | isPalindrome[i][j] = true; 12 | } 13 | else if(gap == 1 && str[i] == str[j]){ 14 | isPalindrome[i][j] = true; 15 | } 16 | else if(str[i] == str[j] && isPalindrome[i+1][j-1]){ 17 | isPalindrome[i][j] = true; 18 | } 19 | } 20 | } 21 | 22 | if(isPalindrome[0][n-1]) return 0; 23 | 24 | // vector> dp(n, vector(n, 0)); 25 | 26 | // for(int gap = 0; gap < n; gap++){ 27 | // for(int i = 0, j = gap; j < n; i++, j++){ 28 | // if(isPalindrome[i][j]){ 29 | // dp[i][j] = 0; 30 | // } 31 | // else if(gap == 1){ 32 | // dp[i][j] = 1; 33 | // } 34 | // else{ 35 | // int mn = INT_MAX; 36 | // for(int k = i; k < j; k++){ 37 | // mn = min(mn, dp[i][k] + dp[k+1][j]); 38 | // } 39 | // dp[i][j] = mn + 1; 40 | // } 41 | // } 42 | // } 43 | // return dp[0][n-1]; 44 | 45 | int dp[n]; 46 | dp[0] = 0; 47 | 48 | for(int i = 1; i=1; j--){ 55 | 56 | //........ |........| 57 | // j plndn i 58 | if(isPalindrome[j][i]){ 59 | mn = min(mn, dp[j-1]); 60 | } 61 | } 62 | dp[i] = mn + 1; 63 | } 64 | } 65 | return dp[n-1]; 66 | } 67 | }; -------------------------------------------------------------------------------- /august/AUG_21_Surrounded_the_1s.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | public: 5 | int Count(vector> &matrix) 6 | { 7 | // Code here 8 | int n = matrix.size(), m = matrix[0].size(), ans = 0; 9 | for (int i = 0; i < n; i++) 10 | { 11 | for (int j = 0; j < m; j++) 12 | { 13 | int zero = 0; 14 | if (matrix[i][j] == 1) 15 | { 16 | // left 17 | if (j - 1 >= 0 && matrix[i][j - 1] == 0) 18 | { 19 | zero++; 20 | } 21 | // right 22 | if (j + 1 < m && matrix[i][j + 1] == 0) 23 | { 24 | zero++; 25 | } 26 | // top 27 | if (i - 1 >= 0 && matrix[i - 1][j] == 0) 28 | { 29 | zero++; 30 | } 31 | // bottom 32 | if (i + 1 < n && matrix[i + 1][j] == 0) 33 | { 34 | zero++; 35 | } 36 | // top-left 37 | if (i - 1 >= 0 && j - 1 >= 0 && matrix[i - 1][j - 1] == 0) 38 | { 39 | zero++; 40 | } 41 | // top-right 42 | if (i - 1 >= 0 && j + 1 < m && matrix[i - 1][j + 1] == 0) 43 | { 44 | zero++; 45 | } 46 | // bottom-left 47 | if (i + 1 < n && j - 1 >= 0 && matrix[i + 1][j - 1] == 0) 48 | { 49 | zero++; 50 | } 51 | // bottom-right 52 | if (i + 1 < n && j + 1 < m && matrix[i + 1][j + 1] == 0) 53 | { 54 | zero++; 55 | } 56 | if (zero != 0 && zero % 2 == 0) 57 | { 58 | ans++; 59 | } 60 | } 61 | } 62 | } 63 | return ans; 64 | } 65 | }; -------------------------------------------------------------------------------- /august/AUG_03_Shortest_path_in_Directed_Acyclic_Graph.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | class Solution 3 | { 4 | void dfs(int s, vector &visited, auto adj[], stack &stack) 5 | { 6 | visited[s] = true; 7 | // ADJ LIST 8 | //[0] {1 2} {4 1} 9 | //[1] {2 3} 10 | //[2] {3 6} 11 | //[3] 12 | //[4] {5 4} {2 2} 13 | //[5] {3 1} 14 | 15 | // adj[s] retpresent a vector of pair 16 | // i represent each pair of the vector 17 | 18 | for (auto i : adj[s]) 19 | { 20 | if (!visited[i.first]) 21 | dfs(i.first, visited, adj, stack); 22 | } 23 | // backtraking 24 | stack.push(s); 25 | } 26 | 27 | public: 28 | vector shortestPath(int N, int M, vector> &edges) 29 | { 30 | // code here 31 | vector> adj[N]; 32 | 33 | // making adjacency list 34 | for (int i = 0; i < M; i++) 35 | { 36 | int u = edges[i][0]; 37 | int v = edges[i][1]; 38 | int w = edges[i][2]; 39 | adj[u].push_back({v, w}); 40 | } 41 | 42 | // topo sort 43 | vector visited(N, false); 44 | stack stack; 45 | vector topoSort; 46 | 47 | for (int i = 0; i < N; i++) 48 | { 49 | if (!visited[i]) 50 | dfs(i, visited, adj, stack); 51 | } 52 | 53 | while (!stack.empty()) 54 | { 55 | topoSort.push_back(stack.top()); 56 | stack.pop(); 57 | } 58 | // 0 4 5 1 2 3 59 | int max_value = 1e9; 60 | vector distance(N, max_value); 61 | distance[0] = 0; // 0 will be the source 62 | 63 | // topoSort ke har ek element ko uthana hai first uske adjasent se compare krna hai 64 | for (int i : topoSort) 65 | { 66 | for (auto j : adj[i]) 67 | { 68 | int v = j.first; // adjacent 69 | int w = j.second; // weight 70 | distance[v] = min(distance[v], distance[i] + w); 71 | } 72 | } 73 | 74 | for (int i = 0; i < N; i++) 75 | { 76 | if (distance[i] == max_value) 77 | distance[i] = -1; 78 | } 79 | return distance; 80 | } 81 | }; -------------------------------------------------------------------------------- /october/OCT_16_Making_A_Large_Island.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int n; 4 | int dfs(int i, int j, vector> &grid, int &index){ 5 | //handling outBound cases 6 | if(i < 0 || j < 0 || i >= n || j >= n) return 0; 7 | 8 | //Base case 9 | if(grid[i][j] != 1) return 0; 10 | 11 | //marked as visited and giving a flag to every island 12 | grid[i][j] = index; 13 | 14 | int area = 1; 15 | area += dfs(i-1, j, grid, index); //move up 16 | area += dfs(i+1, j, grid, index); //move down 17 | area += dfs(i, j-1, grid, index); //move left 18 | area += dfs(i, j+1, grid, index); //move right 19 | 20 | return area; 21 | } 22 | public: 23 | int largestIsland(vector>& grid) 24 | { 25 | // Your code goes here. 26 | 27 | 28 | this->n = grid.size(); 29 | int index = 2, maxArea = 0; //uisng 2 to differentiate b/w 0 and 1 30 | unordered_map map; //{index, area} 31 | 32 | for(int i = 0; i set; 49 | 50 | //outBoundCond. 51 | if(i >= 1) set.insert(grid[i-1][j]); 52 | if(i < n-1) set.insert(grid[i+1][j]); 53 | if(j >= 1) set.insert(grid[i][j-1]); 54 | if(j < n-1) set.insert(grid[i][j+1]); 55 | 56 | int area = 1; 57 | for(auto neighbourIsland : set){ 58 | //incluidng area of neighbours after treaty 59 | area += map[neighbourIsland]; 60 | } 61 | 62 | maxArea = max(maxArea, area); 63 | } 64 | 65 | } 66 | } 67 | 68 | return maxArea; 69 | } 70 | }; -------------------------------------------------------------------------------- /august/AUG_07_Solve_the_Sudoku.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | bool isFix(int row, int col, int val, int grid[N][N]) 4 | { 5 | for (int i = 0; i < N; i++) 6 | { 7 | if (grid[i][col] == val) 8 | return false; 9 | if (grid[row][i] == val) 10 | return false; 11 | } 12 | // there are three subrow and three subcol 13 | 14 | //(0 1 2) / 3 GIVES 0 th subgrid 15 | //(3 4 5) / 3 GIVES 1 th subgrid 16 | //(6 7 9) / 3 GIVES 2 th subgrid 17 | 18 | // ith subgrid * 3 gives starting index of subgrid 19 | 20 | int subrow, subcol; 21 | subrow = (row / 3) * 3; 22 | subcol = (col / 3) * 3; 23 | for (int i = subrow; i < subrow + 3; i++) 24 | { 25 | for (int j = subcol; j < subcol + 3; j++) 26 | { 27 | if (grid[i][j] == val) 28 | return false; 29 | } 30 | } 31 | 32 | return true; 33 | } 34 | 35 | public: 36 | // Function to find a solved Sudoku. 37 | bool SolveSudoku(int grid[N][N]) 38 | { 39 | // Your code here 40 | for (int i = 0; i < N; i++) 41 | { 42 | for (int j = 0; j < N; j++) 43 | { 44 | 45 | // empty cell 46 | if (grid[i][j] == 0) 47 | { 48 | for (int val = 1; val <= 9; val++) 49 | { 50 | 51 | if (isFix(i, j, val, grid)) 52 | { 53 | grid[i][j] = val; 54 | // agr solve kr ke de deta hai to return kr do true... 55 | if (SolveSudoku(grid)) 56 | return true; 57 | else 58 | { 59 | // backtrace 60 | //..nhi to jaisa tha vaisa hi chhor do 61 | grid[i][j] = 0; 62 | } 63 | } 64 | } 65 | // sara val try krne ke bad bhi true return nhi hua mtlb 66 | return false; 67 | } 68 | } 69 | } 70 | // kahi koi cell empty ho hi na 71 | return true; 72 | } 73 | 74 | // Function to print grids of the Sudoku. 75 | void printGrid(int grid[N][N]) 76 | { 77 | // Your code here 78 | for (int i = 0; i < N; i++) 79 | { 80 | for (int j = 0; j < N; j++) 81 | cout << grid[i][j] << " "; 82 | } 83 | } 84 | }; -------------------------------------------------------------------------------- /october/OCT_04_Roman_Number_to_Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToDecimal(string &str) { 4 | // code here 5 | // MMMCXLV 6 | //1000 + 1000 + 1000 + 100 + 10 + 50 + 5 7 | int sum = 0; 8 | for(int i = 0; i> &grid) 8 | { 9 | int n = word.size(), k; 10 | 11 | // moving right 12 | for (k = 0; k < n && j + k < M; k++) 13 | { 14 | if (grid[i][j + k] == word[k]) 15 | continue; 16 | else 17 | break; 18 | } 19 | if (k >= n) 20 | return true; 21 | 22 | // moving left 23 | for (k = 0; k < n && j - k >= 0; k++) 24 | { 25 | if (grid[i][j - k] == word[k]) 26 | continue; 27 | else 28 | break; 29 | } 30 | if (k >= n) 31 | return true; 32 | // moving top 33 | for (k = 0; k < n && i - k >= 0; k++) 34 | { 35 | if (grid[i - k][j] == word[k]) 36 | continue; 37 | else 38 | break; 39 | } 40 | if (k >= n) 41 | return true; 42 | 43 | // moving bottom 44 | for (k = 0; k < n && i + k < N; k++) 45 | { 46 | if (grid[i + k][j] == word[k]) 47 | continue; 48 | else 49 | break; 50 | } 51 | if (k >= n) 52 | return true; 53 | // moving top left 54 | for (k = 0; k < n && i - k >= 0 && j - k >= 0; k++) 55 | { 56 | if (grid[i - k][j - k] == word[k]) 57 | continue; 58 | else 59 | break; 60 | } 61 | if (k >= n) 62 | return true; 63 | 64 | // moving top right 65 | for (k = 0; k < n && i - k >= 0 && j + k < M; k++) 66 | { 67 | if (grid[i - k][j + k] == word[k]) 68 | continue; 69 | else 70 | break; 71 | } 72 | if (k >= n) 73 | return true; 74 | // moving bottom left 75 | for (k = 0; k < n && i + k < N && j - k >= 0; k++) 76 | { 77 | if (grid[i + k][j - k] == word[k]) 78 | continue; 79 | else 80 | break; 81 | } 82 | if (k >= n) 83 | return true; 84 | 85 | // moving bottom right 86 | for (k = 0; k < n && i + k < N && j + k < M; k++) 87 | { 88 | if (grid[i + k][j + k] == word[k]) 89 | continue; 90 | else 91 | break; 92 | } 93 | if (k >= n) 94 | return true; 95 | 96 | return false; 97 | } 98 | 99 | public: 100 | vector> searchWord(vector> grid, string word) 101 | { 102 | // Code here 103 | vector> ans; 104 | 105 | this->N = grid.size(); 106 | this->M = grid[0].size(); 107 | 108 | for (int i = 0; i < N; i++) 109 | { 110 | for (int j = 0; j < M; j++) 111 | { 112 | // this step will call the isFound() only when is there any possibility 113 | if (grid[i][j] == word[0]) 114 | { 115 | if (isFound(i, j, word, grid)) 116 | ans.push_back({i, j}); 117 | } 118 | } 119 | } 120 | return ans; 121 | } 122 | }; -------------------------------------------------------------------------------- /august/AUG_31_AVL_Tree_Deletion.cpp: -------------------------------------------------------------------------------- 1 | /* Node is as follows: 2 | 3 | struct Node 4 | { 5 | int data, height; 6 | Node *left, *right; 7 | Node(int x) 8 | { 9 | data = x; 10 | height = 1; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | */ 16 | 17 | int heightOf(Node *root) 18 | { 19 | if (!root) 20 | return 0; 21 | else 22 | return root->height; 23 | } 24 | 25 | int getBalanceFactor(Node *root) 26 | { 27 | if (!root) 28 | return 0; 29 | return heightOf(root->left) - heightOf(root->right); 30 | } 31 | void updateHeight(Node *node) 32 | { 33 | node->height = max(heightOf(node->left), heightOf(node->right)) + 1; 34 | } 35 | Node *leftRotation(Node *root) 36 | { 37 | Node *temp = root->right; 38 | root->right = temp->left; 39 | temp->left = root; 40 | 41 | updateHeight(root); 42 | updateHeight(temp); 43 | 44 | return temp; 45 | } 46 | 47 | Node *rightRotation(Node *root) 48 | { 49 | Node *temp = root->left; 50 | root->left = temp->right; 51 | temp->right = root; 52 | 53 | updateHeight(root); 54 | updateHeight(temp); 55 | 56 | return temp; 57 | } 58 | 59 | int rightMin(Node *root) 60 | { 61 | root = root->right; 62 | while (root->left != NULL) 63 | { 64 | root = root->left; 65 | } 66 | return root->data; 67 | } 68 | Node *deleteNode(Node *root, int data) 69 | { 70 | // add code here, 71 | if (!root) 72 | return NULL; 73 | 74 | if (data < root->data) 75 | { 76 | root->left = deleteNode(root->left, data); 77 | } 78 | else if (data > root->data) 79 | { 80 | root->right = deleteNode(root->right, data); 81 | } 82 | else 83 | { 84 | // leaf node 85 | Node *toDelete = root; 86 | if (!root->left && !root->right) 87 | { 88 | delete (root); 89 | return NULL; 90 | } 91 | // node having one child 92 | else if (root->left && !root->right) 93 | { 94 | Node *left = root->left; 95 | delete (toDelete); 96 | return left; 97 | } 98 | else if (!root->left && root->right) 99 | { 100 | Node *right = root->right; 101 | delete (toDelete); 102 | return right; 103 | } 104 | 105 | // node having one child 106 | // node having both children 107 | else 108 | { 109 | int newData = rightMin(root); 110 | root->data = newData; 111 | root->right = deleteNode(root->right, newData); 112 | } 113 | } 114 | 115 | updateHeight(root); 116 | int balanceFactor = getBalanceFactor(root); 117 | 118 | // left left 119 | if (balanceFactor > 1 && getBalanceFactor(root->left) >= 0) 120 | { 121 | return rightRotation(root); 122 | } 123 | // left right 124 | else if (balanceFactor > 1 && getBalanceFactor(root->left) < 0) 125 | { 126 | root->left = leftRotation(root->left); 127 | return rightRotation(root); 128 | } 129 | // right left 130 | else if (balanceFactor < -1 && getBalanceFactor(root->right) > 0) 131 | { 132 | root->right = rightRotation(root->right); 133 | return leftRotation(root); 134 | } 135 | // right right 136 | else if (balanceFactor < -1 && getBalanceFactor(root->right) <= 0) 137 | { 138 | return leftRotation(root); 139 | } 140 | 141 | return root; 142 | } --------------------------------------------------------------------------------