├── 2 ├── 2_Two_sum └── 2_Two_sum.cpp ├── 4 ├── 4_Median_of_two_sorted_arrays └── 4_Median_of_two_sorted_arrays.cpp ├── 12 ├── Integer_to_roman └── Integer_to_roman.cpp ├── 19 ├── 19_optimal ├── 19_remove_nth_node_from_end_of_list ├── 19_remove_nth_node_from_end_of_list.cpp └── 19_optimal.cpp ├── 20 ├── 20_Valid_Parentheses └── 20_Valid_Parentheses.cpp ├── 25 ├── 25_reverse_Nodes_in_k-group └── 25_reverse_Nodes_in_k-group.cpp ├── 26 └── 26_Remove_Duplicates_from_Sorted_Array.cpp ├── 33 └── 33_Search_in_Rotated_Sorted_Array.cpp ├── 34 ├── 34_Find_First_and_Last_Position_of_Element_in_Sorted_Array └── 34_Find_First_and_Last_Position_of_Element_in_Sorted_Array.cpp ├── 35 ├── 35_Search_Insert_Position └── 35_Search_Insert_Position.cpp ├── 42 ├── 42_Trapping_Rainwater_Optimal ├── 42_Trapping_Rain_Water_Brute_Force ├── 42_Trapping_Rainwater_Optimal.cpp └── 42_Trapping_Rain_Water_Brute_Force.cpp ├── 50 ├── 50_MyPow └── 50_MyPow.cpp ├── 61 ├── Rotate_list └── Rotate_list.cpp ├── 69 ├── 69_mySqrt └── 69_mySqrt.cpp ├── 74 ├── 74_Search_in_a_2D_matrix └── 74_Search_in_a_2D_matrix.cpp ├── 75 ├── 75_sort_colors └── 75_sort_colors.cpp ├── 78 ├── Subsets └── Subsets.cpp ├── 81 ├── 81_Search_in_rotated_sorted_array_with_dup_II └── 81_Search_in_rotated_sorted_array_with_dup_II.cpp ├── 94 ├── 94_In_order_Traversal_of_Binary_Tree └── 94_In_order_Traversal_of_Binary_Tree.cpp ├── 98 ├── 98_Validate_Binary_Search_Tree └── 98_Validate_Binary_Search_Tree.cpp ├── 104 ├── 104_Max_Depth_Of_BInary_Tree └── 104_Max_Depth_Of_BInary_Tree.cpp ├── 125 ├── 125_Valid_Palindrome └── 125_Valid_Palindrome.cpp ├── 136 └── 136_Single_Number.cpp ├── 138 ├── 138_Copy_List_with_random_pointer └── 138_Copy_List_with_random_pointer.cpp ├── 141 ├── 141_Linked_list_cycle └── 141_Linked_list_cycle.cpp ├── 142 ├── 142_Linked_list_cycle_II └── 142_Linked_list_cycle_II.cpp ├── 144 ├── 144_Pre_Order-Traversal_of_Binary_Tree └── 144_Pre_Order-Traversal_of_Binary_Tree.cpp ├── 145 ├── 145_Post_Order_Traversal_Of_Binary_trees └── 145_Post_Order_Traversal_Of_Binary_trees.cpp ├── 148 ├── 148_Brute ├── 148_Sort_List ├── 148_Brute.cpp └── 148_Sort_List.cpp ├── 153 └── 153_Find_minimum_in_rotated_sorted_array.cpp ├── 155 ├── Min_Stack └── Min_Stack.cpp ├── 160 ├── 160_Intersection_of_two_LL └── 160_Intersection_of_two_LL.cpp ├── 162 ├── 162_Find_Peak_Element └── 162_Find_Peak_Element.cpp ├── 204 ├── 204_Count_Primes └── 204_Count_Primes.cpp ├── 206 ├── 206_Reverse_Linked_List ├── 206_Reverse_LL_using_optimal_approach ├── 206_Reverse_Linked_List.cpp └── 206_Reverse_LL_using_optimal_approach.cpp ├── 225 ├── 225_Implement_Stack_Using_Queue └── 225_Implement_Stack_Using_Queue.cpp ├── 231 ├── 231_Power_of_two └── 231_Power_of_two.cpp ├── 232 ├── 232_Implement_queue_using_stacks └── 232_Implement_queue_using_stacks.cpp ├── 234 ├── 234_Palindrone_Linked_List └── 234_Palindrone_Linked_List.cpp ├── 236 ├── 236_Lowest_Common_Ancestor_in_a_Binary_Tree └── 236_Lowest_Common_Ancestor_in_a_Binary_Tree.cpp ├── 237 ├── 237_Delete_Node_in_a_linked_list └── 237_Delete_Node_in_a_linked_list.cpp ├── 240 ├── 240_Search_a_2D_Matrix_II └── 240_Search_a_2D_Matrix_II.cpp ├── 328 ├── 328_Odd_even_LL └── 328_Odd_even_LL.cpp ├── 387 ├── 387_First_Unique_Character_in_a_String └── 387_First_Unique_Character_in_a_String.cpp ├── 410 ├── 410_Split_array_largest_sum └── 410_Split_array_largest_sum.cpp ├── 496 ├── 496_Next_Greater_Int_I └── 496_Next_Greater_Int_I.cpp ├── 503 ├── 503_Next_Greater_Int_II └── 503_Next_Greater_Int_II.cpp ├── 509 ├── 509_Fibonacci_Number └── 509_Fibonacci_Number.cpp ├── 540 ├── 540_Single_element_in_sorted_array └── 540_Single_element_in_sorted_array.cpp ├── 584 └── 584_Find_Customer_Referee.sql ├── 595 └── 595_Big_Countries.sql ├── 622 ├── 622_Circular_Queue └── 622_Circular_Queue.cpp ├── 704 ├── 704_Binary_Search └── 704_Binary_Search.c ├── 875 ├── 875_Koko_Eating_bananas └── 875_Koko_Eating_bananas.cpp ├── 876 ├── 876_Middle_of_the_Linked_list └── 876_Middle_of_the_Linked_list.cpp ├── 907 ├── 907_Sum_of_Subarray_minimums └── 907_Sum_of_Subarray_minimums.cpp ├── 1011 ├── 1011_Capacity_to_ship_packages_within_D_days └── 1011_Capacity_to_ship_packages_within_D_days.cpp ├── 1021 ├── 1021_Remove_Outermost_Parentheses └── 1021_Remove_Outermost_Parentheses.cpp ├── 1108 ├── 1108_Defanging_an_IP_Address └── 1108_Defanging_an_IP_Address.cpp ├── 1148 └── 1148_Article_views_I.sql ├── 1283 ├── 1283_Find_the_smallest_divisor_given_a_threshold └── 1283_Find_the_smallest_divisor_given_a_threshold.cpp ├── 1431 ├── 1431_Kids_With_the_Greatest_Number_of_Candies └── 1431_Kids_With_the_Greatest_Number_of_Candies.cpp ├── 1470 ├── 1470_Shuffle_the_array └── 1470_Shuffle_the_array.cpp ├── 1482 ├── 1482_Minimum_number_of_days_to_make_m_bouquets └── 1482_Minimum_number_of_days_to_make_m_bouquets.cpp ├── 1512 └── 1512_Number_of_Good_Pairs.cpp ├── 1539 ├── 1539_kth_missing_positive_number └── 1539_kth_missing_positive_number.cpp ├── 1672 ├── 1672_Richest_Customer_Wealth └── 1672_Richest_Customer_Wealth.cpp ├── 1678 ├── 1678_Goal_Parser_Interpretation └── 1678_Goal_Parser_Interpretation.cpp ├── 1683 └── 1683_Invalid_Tweets.sql ├── 1684 ├── 1684_Count_the_Number_of_Consistent_Strings └── 1684_Count_the_Number_of_Consistent_Strings.cpp ├── 1752 ├── 1752._Check_if_Array_Is_Sorted_and_Rotated └── 1752._Check_if_Array_Is_Sorted_and_Rotated.cpp ├── 1757 └── Recyclable_and_Low_Fat_Products.sql ├── 1894 ├── 1894_Chalk_replacer └── 1894_Chalk_replacer.cpp ├── 1920 ├── 1920_Build_array_from_permutation └── 1920_Build_array_from_permutation.cpp ├── 2011 ├── 2011_Final_Value_of_Variable_After_Performing_Operations └── 2011_Final_Value_of_Variable_After_Performing_Operations.cpp ├── 2095 ├── 2095_Delete_the_middle_node_of_a_LL └── 2095_Delete_the_middle_node_of_a_LL.cpp ├── 2181 ├── 2181_Merge_Nodes_in_Between_Zeros └── 2181_Merge_Nodes_in_Between_Zeros.cpp ├── 2220 ├── 2220_Minimum_Bit_Flips_to_Convert_Number └── 2220_Minimum_Bit_Flips_to_Convert_Number.cpp ├── 2413 ├── 2413_Smallest_Even_Multiple └── 2413_Smallest_Even_Multiple.cpp ├── 2469 ├── 2469_Convert_the_Temperature └── 2469_Convert_the_Temperature.cpp ├── 2769 ├── 2769_Find_the_maximum_achievable_number └── 2769_Find_the_maximum_achievable_number.cpp ├── 2798 ├── 2798_Number_of_Employees_Who_Met_the_Target └── 2798_Number_of_Employees_Who_Met_the_Target.cpp ├── 2807 ├── 2807_Insert_Greatest_Common_Divisors_in_Linked_List └── 2807_Insert_Greatest_Common_Divisors_in_Linked_List.cpp ├── 2894 ├── 2894_Divisible_and_Non-divisible_Sums_Difference └── 2894_Divisible_and_Non-divisible_Sums_Difference.cpp ├── 2942 ├── 2942_Find_Words_Containing_Character └── 2942_Find_Words_Containing_Character.cpp ├── 3110 ├── 3110_Score_of_a_String └── 3110_Score_of_a_String.cpp ├── 3146 └── 3146_Permutation_Difference_between_Two_Strings.cpp ├── 3190 ├── 3190_Find_Minimum_Operations_to_Make_All_Elements_Divisible_by_Three └── 3190_Find_Minimum_Operations_to_Make_All_Elements_Divisible_by_Three.cpp ├── 3289 ├── 3289_The_Two_Sneaky_Numbers_of_Digitville └── 3289_The_Two_Sneaky_Numbers_of_Digitville.cpp ├── .gitignore ├── 895_Own_Approach ├── 895_Max_Freq_Stack └── 895_Max_Freq_Stack.cpp ├── 503_Own_Approach ├── 503_Next_Greater_Int_II_Own_approach └── 503_Next_Greater_Int_II_Own_approach.cpp ├── 2820-return-length-of-arguments-passed ├── 2820-return-length-of-arguments-passed.js └── README.md ├── 0104-maximum-depth-of-binary-tree ├── 0104-maximum-depth-of-binary-tree.cpp └── README.md ├── 0094-binary-tree-inorder-traversal ├── 0094-binary-tree-inorder-traversal.cpp └── README.md ├── stats.json ├── 0543-diameter-of-binary-tree ├── 0543-diameter-of-binary-tree.cpp └── README.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /2/2_Two_sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2/2_Two_sum -------------------------------------------------------------------------------- /50/50_MyPow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/50/50_MyPow -------------------------------------------------------------------------------- /69/69_mySqrt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/69/69_mySqrt -------------------------------------------------------------------------------- /78/Subsets: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/78/Subsets -------------------------------------------------------------------------------- /148/148_Brute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/148/148_Brute -------------------------------------------------------------------------------- /155/Min_Stack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/155/Min_Stack -------------------------------------------------------------------------------- /19/19_optimal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/19/19_optimal -------------------------------------------------------------------------------- /61/Rotate_list: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/61/Rotate_list -------------------------------------------------------------------------------- /148/148_Sort_List: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/148/148_Sort_List -------------------------------------------------------------------------------- /1683/1683_Invalid_Tweets.sql: -------------------------------------------------------------------------------- 1 | SELECT tweet_id 2 | FROM tweets 3 | WHERE LENGTH(content) > 15; -------------------------------------------------------------------------------- /75/75_sort_colors: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/75/75_sort_colors -------------------------------------------------------------------------------- /12/Integer_to_roman: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/12/Integer_to_roman -------------------------------------------------------------------------------- /328/328_Odd_even_LL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/328/328_Odd_even_LL -------------------------------------------------------------------------------- /204/204_Count_Primes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/204/204_Count_Primes -------------------------------------------------------------------------------- /231/231_Power_of_two: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/231/231_Power_of_two -------------------------------------------------------------------------------- /622/622_Circular_Queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/622/622_Circular_Queue -------------------------------------------------------------------------------- /704/704_Binary_Search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/704/704_Binary_Search -------------------------------------------------------------------------------- /125/125_Valid_Palindrome: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/125/125_Valid_Palindrome -------------------------------------------------------------------------------- /1894/1894_Chalk_replacer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1894/1894_Chalk_replacer -------------------------------------------------------------------------------- /20/20_Valid_Parentheses: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/20/20_Valid_Parentheses -------------------------------------------------------------------------------- /509/509_Fibonacci_Number: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/509/509_Fibonacci_Number -------------------------------------------------------------------------------- /584/584_Find_Customer_Referee.sql: -------------------------------------------------------------------------------- 1 | SELECT name 2 | FROM Customer 3 | WHERE referee_id != 2 IS NOT FALSE; -------------------------------------------------------------------------------- /141/141_Linked_list_cycle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/141/141_Linked_list_cycle -------------------------------------------------------------------------------- /1470/1470_Shuffle_the_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1470/1470_Shuffle_the_array -------------------------------------------------------------------------------- /162/162_Find_Peak_Element: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/162/162_Find_Peak_Element -------------------------------------------------------------------------------- /206/206_Reverse_Linked_List: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/206/206_Reverse_Linked_List -------------------------------------------------------------------------------- /3110/3110_Score_of_a_String: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/3110/3110_Score_of_a_String -------------------------------------------------------------------------------- /496/496_Next_Greater_Int_I: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/496/496_Next_Greater_Int_I -------------------------------------------------------------------------------- /503/503_Next_Greater_Int_II: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/503/503_Next_Greater_Int_II -------------------------------------------------------------------------------- /74/74_Search_in_a_2D_matrix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/74/74_Search_in_a_2D_matrix -------------------------------------------------------------------------------- /875/875_Koko_Eating_bananas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/875/875_Koko_Eating_bananas -------------------------------------------------------------------------------- /142/142_Linked_list_cycle_II: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/142/142_Linked_list_cycle_II -------------------------------------------------------------------------------- /240/240_Search_a_2D_Matrix_II: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/240/240_Search_a_2D_Matrix_II -------------------------------------------------------------------------------- /35/35_Search_Insert_Position: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/35/35_Search_Insert_Position -------------------------------------------------------------------------------- /104/104_Max_Depth_Of_BInary_Tree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/104/104_Max_Depth_Of_BInary_Tree -------------------------------------------------------------------------------- /160/160_Intersection_of_two_LL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/160/160_Intersection_of_two_LL -------------------------------------------------------------------------------- /1757/Recyclable_and_Low_Fat_Products.sql: -------------------------------------------------------------------------------- 1 | SELECT product_id 2 | FROM Products 3 | WHERE low_fats = 'Y' AND recyclable = 'Y'; -------------------------------------------------------------------------------- /234/234_Palindrone_Linked_List: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/234/234_Palindrone_Linked_List -------------------------------------------------------------------------------- /2413/2413_Smallest_Even_Multiple: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2413/2413_Smallest_Even_Multiple -------------------------------------------------------------------------------- /25/25_reverse_Nodes_in_k-group: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/25/25_reverse_Nodes_in_k-group -------------------------------------------------------------------------------- /4/4_Median_of_two_sorted_arrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/4/4_Median_of_two_sorted_arrays -------------------------------------------------------------------------------- /410/410_Split_array_largest_sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/410/410_Split_array_largest_sum -------------------------------------------------------------------------------- /42/42_Trapping_Rainwater_Optimal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/42/42_Trapping_Rainwater_Optimal -------------------------------------------------------------------------------- /595/595_Big_Countries.sql: -------------------------------------------------------------------------------- 1 | SELECT name, population, area 2 | FROM World 3 | WHERE area >= 3000000 OR population >= 25000000; -------------------------------------------------------------------------------- /907/907_Sum_of_Subarray_minimums: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/907/907_Sum_of_Subarray_minimums -------------------------------------------------------------------------------- /1108/1108_Defanging_an_IP_Address: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1108/1108_Defanging_an_IP_Address -------------------------------------------------------------------------------- /1148/1148_Article_views_I.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT author_id AS id 2 | FROM views 3 | WHERE author_id = viewer_id 4 | ORDER BY id; -------------------------------------------------------------------------------- /1672/1672_Richest_Customer_Wealth: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1672/1672_Richest_Customer_Wealth -------------------------------------------------------------------------------- /2469/2469_Convert_the_Temperature: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2469/2469_Convert_the_Temperature -------------------------------------------------------------------------------- /876/876_Middle_of_the_Linked_list: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/876/876_Middle_of_the_Linked_list -------------------------------------------------------------------------------- /98/98_Validate_Binary_Search_Tree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/98/98_Validate_Binary_Search_Tree -------------------------------------------------------------------------------- /138/138_Copy_List_with_random_pointer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/138/138_Copy_List_with_random_pointer -------------------------------------------------------------------------------- /1539/1539_kth_missing_positive_number: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1539/1539_kth_missing_positive_number -------------------------------------------------------------------------------- /1678/1678_Goal_Parser_Interpretation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1678/1678_Goal_Parser_Interpretation -------------------------------------------------------------------------------- /225/225_Implement_Stack_Using_Queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/225/225_Implement_Stack_Using_Queue -------------------------------------------------------------------------------- /232/232_Implement_queue_using_stacks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/232/232_Implement_queue_using_stacks -------------------------------------------------------------------------------- /237/237_Delete_Node_in_a_linked_list: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/237/237_Delete_Node_in_a_linked_list -------------------------------------------------------------------------------- /42/42_Trapping_Rain_Water_Brute_Force: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/42/42_Trapping_Rain_Water_Brute_Force -------------------------------------------------------------------------------- /895_Own_Approach/895_Max_Freq_Stack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/895_Own_Approach/895_Max_Freq_Stack -------------------------------------------------------------------------------- /1021/1021_Remove_Outermost_Parentheses: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1021/1021_Remove_Outermost_Parentheses -------------------------------------------------------------------------------- /19/19_remove_nth_node_from_end_of_list: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/19/19_remove_nth_node_from_end_of_list -------------------------------------------------------------------------------- /1920/1920_Build_array_from_permutation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1920/1920_Build_array_from_permutation -------------------------------------------------------------------------------- /2181/2181_Merge_Nodes_in_Between_Zeros: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2181/2181_Merge_Nodes_in_Between_Zeros -------------------------------------------------------------------------------- /540/540_Single_element_in_sorted_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/540/540_Single_element_in_sorted_array -------------------------------------------------------------------------------- /94/94_In_order_Traversal_of_Binary_Tree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/94/94_In_order_Traversal_of_Binary_Tree -------------------------------------------------------------------------------- /144/144_Pre_Order-Traversal_of_Binary_Tree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/144/144_Pre_Order-Traversal_of_Binary_Tree -------------------------------------------------------------------------------- /206/206_Reverse_LL_using_optimal_approach: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/206/206_Reverse_LL_using_optimal_approach -------------------------------------------------------------------------------- /2095/2095_Delete_the_middle_node_of_a_LL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2095/2095_Delete_the_middle_node_of_a_LL -------------------------------------------------------------------------------- /2942/2942_Find_Words_Containing_Character: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2942/2942_Find_Words_Containing_Character -------------------------------------------------------------------------------- /387/387_First_Unique_Character_in_a_String: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/387/387_First_Unique_Character_in_a_String -------------------------------------------------------------------------------- /145/145_Post_Order_Traversal_Of_Binary_trees: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/145/145_Post_Order_Traversal_Of_Binary_trees -------------------------------------------------------------------------------- /2769/2769_Find_the_maximum_achievable_number: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2769/2769_Find_the_maximum_achievable_number -------------------------------------------------------------------------------- /1752/1752._Check_if_Array_Is_Sorted_and_Rotated: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1752/1752._Check_if_Array_Is_Sorted_and_Rotated -------------------------------------------------------------------------------- /2220/2220_Minimum_Bit_Flips_to_Convert_Number: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2220/2220_Minimum_Bit_Flips_to_Convert_Number -------------------------------------------------------------------------------- /236/236_Lowest_Common_Ancestor_in_a_Binary_Tree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/236/236_Lowest_Common_Ancestor_in_a_Binary_Tree -------------------------------------------------------------------------------- /3289/3289_The_Two_Sneaky_Numbers_of_Digitville: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/3289/3289_The_Two_Sneaky_Numbers_of_Digitville -------------------------------------------------------------------------------- /1011/1011_Capacity_to_ship_packages_within_D_days: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1011/1011_Capacity_to_ship_packages_within_D_days -------------------------------------------------------------------------------- /1684/1684_Count_the_Number_of_Consistent_Strings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1684/1684_Count_the_Number_of_Consistent_Strings -------------------------------------------------------------------------------- /2798/2798_Number_of_Employees_Who_Met_the_Target: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2798/2798_Number_of_Employees_Who_Met_the_Target -------------------------------------------------------------------------------- /81/81_Search_in_rotated_sorted_array_with_dup_II: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/81/81_Search_in_rotated_sorted_array_with_dup_II -------------------------------------------------------------------------------- /1431/1431_Kids_With_the_Greatest_Number_of_Candies: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1431/1431_Kids_With_the_Greatest_Number_of_Candies -------------------------------------------------------------------------------- /1482/1482_Minimum_number_of_days_to_make_m_bouquets: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1482/1482_Minimum_number_of_days_to_make_m_bouquets -------------------------------------------------------------------------------- /1283/1283_Find_the_smallest_divisor_given_a_threshold: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/1283/1283_Find_the_smallest_divisor_given_a_threshold -------------------------------------------------------------------------------- /2894/2894_Divisible_and_Non-divisible_Sums_Difference: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2894/2894_Divisible_and_Non-divisible_Sums_Difference -------------------------------------------------------------------------------- /503_Own_Approach/503_Next_Greater_Int_II_Own_approach: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/503_Own_Approach/503_Next_Greater_Int_II_Own_approach -------------------------------------------------------------------------------- /2807/2807_Insert_Greatest_Common_Divisors_in_Linked_List: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2807/2807_Insert_Greatest_Common_Divisors_in_Linked_List -------------------------------------------------------------------------------- /2011/2011_Final_Value_of_Variable_After_Performing_Operations: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/2011/2011_Final_Value_of_Variable_After_Performing_Operations -------------------------------------------------------------------------------- /34/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/34/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array -------------------------------------------------------------------------------- /3190/3190_Find_Minimum_Operations_to_Make_All_Elements_Divisible_by_Three: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VatsalyaGautam/Leetcode/HEAD/3190/3190_Find_Minimum_Operations_to_Make_All_Elements_Divisible_by_Three -------------------------------------------------------------------------------- /2820-return-length-of-arguments-passed/2820-return-length-of-arguments-passed.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {...(null|boolean|number|string|Array|Object)} args 3 | * @return {number} 4 | */ 5 | var argumentsLength = function(...args) { 6 | return args.length 7 | }; 8 | 9 | /** 10 | * argumentsLength(1, 2, 3); // 3 11 | */ -------------------------------------------------------------------------------- /2413/2413_Smallest_Even_Multiple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int smallestEvenMultiple(int n) { 7 | if (n % 2 == 0) return n; 8 | else return n * 2; 9 | } 10 | }; 11 | 12 | // Driver program 13 | int main() { 14 | Solution solution; 15 | int n = 5; 16 | cout << solution.smallestEvenMultiple(n) << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /2769/2769_Find_the_maximum_achievable_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int theMaximumAchievableX(int num, int t) 8 | { 9 | return num + (2 * t); 10 | } 11 | }; 12 | 13 | // Driver program to test the solution 14 | int main() 15 | { 16 | Solution solution; 17 | int num, t; 18 | 19 | cout << "Enter the value of num: "; 20 | cin >> num; 21 | 22 | cout << "Enter the value of t: "; 23 | cin >> t; 24 | 25 | int result = solution.theMaximumAchievableX(num, t); 26 | cout << "The maximum achievable X is: " << result << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /2798/2798_Number_of_Employees_Who_Met_the_Target.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int numberOfEmployeesWhoMetTarget(vector& hours, int target) { 7 | int count = 0; 8 | for (int i = 0; i < hours.size(); i++) { 9 | if (hours[i] >= target) { 10 | count++; 11 | } 12 | } 13 | return count; 14 | } 15 | }; 16 | 17 | // Driver program 18 | int main() { 19 | Solution solution; 20 | vector hours = {8, 10, 5, 7, 9}; 21 | int target = 8; 22 | cout << solution.numberOfEmployeesWhoMetTarget(hours, target) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /509/509_Fibonacci_Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int fib(int n) 8 | { 9 | if (n == 0) 10 | { 11 | return 0; 12 | } 13 | else if (n == 1) 14 | { 15 | return 1; 16 | } 17 | return fib(n - 1) + fib(n - 2); 18 | } 19 | }; 20 | 21 | int main() 22 | { 23 | Solution sol; 24 | 25 | // Test cases 26 | int test_cases[] = {0, 1, 2, 3, 4, 5, 10, 20}; // Add more test cases as needed 27 | for (int n : test_cases) 28 | { 29 | cout << "fib(" << n << ") = " << sol.fib(n) << endl; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int maxDepth(TreeNode* root) { 15 | if(!root)return 0; 16 | int lh= maxDepth(root->left); 17 | int rh= maxDepth(root->right); 18 | return 1+max(lh,rh); 19 | } 20 | }; -------------------------------------------------------------------------------- /3110/3110_Score_of_a_String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int scoreOfString(string s) 10 | { 11 | int ans = 0; 12 | for (int i = 1; i < s.size(); i++) 13 | { 14 | ans += abs((int)s[i - 1] - (int)s[i]); 15 | } 16 | return ans; 17 | } 18 | }; 19 | 20 | // Driver program to test the solution 21 | int main() 22 | { 23 | Solution solution; 24 | string input; 25 | 26 | cout << "Enter a string: "; 27 | cin >> input; 28 | 29 | int result = solution.scoreOfString(input); 30 | cout << "Score of the string is: " << result << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /387/387_First_Unique_Character_in_a_String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int firstUniqChar(string s) { 7 | unordered_map mpp; 8 | for (char ch : s) { 9 | mpp[ch]++; 10 | } 11 | for (int i = 0; i < s.size(); i++) { 12 | if (mpp[s[i]] == 1) return i; 13 | } 14 | return -1; 15 | } 16 | }; 17 | 18 | // Driver Program 19 | int main() { 20 | Solution sol; 21 | vector testCases = {"leetcode", "loveleetcode", "aabb"}; 22 | 23 | for (const string& s : testCases) { 24 | cout << "Input: " << s << " -> Output: " << sol.firstUniqChar(s) << endl; 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /1672/1672_Richest_Customer_Wealth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maximumWealth(vector>& accounts) { 7 | int wealth = INT_MIN; 8 | for (int i = 0; i < accounts.size(); i++) { 9 | int sum = 0; 10 | for (int j = 0; j < accounts[0].size(); j++) { 11 | sum += accounts[i][j]; 12 | } 13 | wealth = max(wealth, sum); 14 | } 15 | return wealth; 16 | } 17 | }; 18 | 19 | // Driver program 20 | int main() { 21 | Solution solution; 22 | vector> accounts = {{1, 2, 3}, {3, 2, 1}, {4, 5, 6}}; 23 | cout << solution.maximumWealth(accounts) << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /50/50_MyPow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | double myPow(double x, int n) { 7 | long long num = n; 8 | double ans = 1.0; 9 | 10 | if (num < 0) { 11 | x = 1 / x; 12 | num = -num; 13 | } 14 | 15 | while (num > 0) { 16 | if (num % 2 == 1) { 17 | ans *= x; 18 | } 19 | x *= x; 20 | num /= 2; 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | 27 | // Driver Program 28 | int main() { 29 | Solution sol; 30 | double x; 31 | int n; 32 | cin >> x >> n; // Example inputs 33 | cout << fixed << setprecision(5) << sol.myPow(x, n) << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /204/204_Count_Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int countPrimes(int n) 8 | { 9 | if (n <= 2) 10 | return 0; 11 | vector vec(n, 1); 12 | vec[0] = vec[1] = 0; 13 | 14 | for (int i = 2; i * i < n; i++) 15 | { 16 | if (vec[i]) 17 | { 18 | for (int j = i * i; j < n; j += i) 19 | { 20 | vec[j] = 0; 21 | } 22 | } 23 | } 24 | return count(vec.begin(), vec.end(), 1); 25 | } 26 | }; 27 | 28 | // Driver Program 29 | int main() 30 | { 31 | Solution sol; 32 | int n; 33 | cin >> n; // Example input 34 | cout << sol.countPrimes(n) << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /2469/2469_Convert_the_Temperature.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | vector convertTemperature(double celsius) 9 | { 10 | double Kelvin = celsius + 273.15; 11 | double Fahrenheit = celsius * 1.80 + 32.00; 12 | return {Kelvin, Fahrenheit}; 13 | } 14 | }; 15 | 16 | // Driver program to test the solution 17 | int main() 18 | { 19 | Solution solution; 20 | double celsius; 21 | 22 | cout << "Enter temperature in Celsius: "; 23 | cin >> celsius; 24 | 25 | vector result = solution.convertTemperature(celsius); 26 | 27 | cout << "Temperature in Kelvin: " << result[0] << endl; 28 | cout << "Temperature in Fahrenheit: " << result[1] << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /1470/1470_Shuffle_the_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector shuffle(vector& nums, int n) { 7 | vector ans; 8 | int mid = n; 9 | for (int i = 0; i < n; i++) { 10 | ans.emplace_back(nums[i]); 11 | ans.emplace_back(nums[mid]); 12 | mid++; 13 | } 14 | return ans; 15 | } 16 | }; 17 | 18 | // Driver Program 19 | int main() { 20 | Solution sol; 21 | int n; 22 | cin >> n; 23 | vector nums(2 * n); 24 | for (int i = 0; i < 2 * n; i++) { 25 | cin >> nums[i]; 26 | } 27 | vector result = sol.shuffle(nums, n); 28 | for (int x : result) { 29 | cout << x << " "; 30 | } 31 | cout << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /1108/1108_Defanging_an_IP_Address.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | string defangIPaddr(string address) 9 | { 10 | for (size_t i = 0; i < address.length(); i++) 11 | { 12 | if (address[i] == '.') 13 | { 14 | address.replace(i, 1, "[.]"); 15 | i += 2; 16 | } 17 | } 18 | return address; 19 | } 20 | }; 21 | 22 | // Driver program to test the solution 23 | int main() 24 | { 25 | Solution solution; 26 | string address; 27 | 28 | cout << "Enter the IP address: "; 29 | cin >> address; 30 | 31 | string result = solution.defangIPaddr(address); 32 | cout << "Defanged IP address: " << result << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /1678/1678_Goal_Parser_Interpretation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string interpret(string command) { 7 | string ans = ""; 8 | for (int i = 0; i < command.size(); i++) { 9 | if (command[i] == 'G') { 10 | ans.append("G"); 11 | } else if (command[i + 1] == 'a') { 12 | ans.append("al"); 13 | i += 3; 14 | } else { 15 | ans.append("o"); 16 | i += 1; 17 | } 18 | } 19 | return ans; 20 | } 21 | }; 22 | 23 | // Driver program 24 | int main() { 25 | Solution solution; 26 | string command; 27 | cin >> command; // Input the command string 28 | cout << solution.interpret(command) << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /104/104_Max_Depth_Of_BInary_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left; 7 | TreeNode *right; 8 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | }; 10 | 11 | class Solution { 12 | public: 13 | int maxDepth(TreeNode* root) { 14 | if (root == nullptr) return 0; 15 | return max(1 + maxDepth(root->left), 1 + maxDepth(root->right)); 16 | } 17 | }; 18 | 19 | // Driver program to test the solution 20 | int main() { 21 | TreeNode* root = new TreeNode(1); 22 | root->left = new TreeNode(2); 23 | root->right = new TreeNode(3); 24 | root->left->left = new TreeNode(4); 25 | root->left->right = new TreeNode(5); 26 | 27 | Solution sol; 28 | cout << sol.maxDepth(root) << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /2894/2894_Divisible_and_Non-divisible_Sums_Difference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int differenceOfSums(int n, int m) 8 | { 9 | int num1 = 0, num2 = 0; 10 | for (int i = 1; i <= n; i++) 11 | { 12 | if (i % m != 0) 13 | num1 += i; 14 | else 15 | num2 += i; 16 | } 17 | return num1 - num2; 18 | } 19 | }; 20 | 21 | // Driver program to test the solution 22 | int main() 23 | { 24 | Solution solution; 25 | int n, m; 26 | 27 | cout << "Enter value of n: "; 28 | cin >> n; 29 | cout << "Enter value of m: "; 30 | cin >> m; 31 | 32 | int result = solution.differenceOfSums(n, m); 33 | cout << "Difference of sums: " << result << endl; 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /136/136_Single_Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int singleNumber(vector &nums) 9 | { 10 | int ans = 0; 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | ans = ans ^ nums[i]; 14 | } 15 | return ans; 16 | } 17 | }; 18 | 19 | // Driver program to test the solution 20 | int main() 21 | { 22 | Solution solution; 23 | int n; 24 | 25 | cout << "Enter number of elements in the array: "; 26 | cin >> n; 27 | 28 | vector nums(n); 29 | cout << "Enter elements of the array: "; 30 | for (int i = 0; i < n; i++) 31 | { 32 | cin >> nums[i]; 33 | } 34 | 35 | int result = solution.singleNumber(nums); 36 | cout << "The single number is: " << result << endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /69/69_mySqrt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int mySqrt(int x) { 7 | if (x == 0 || x == 1) 8 | return x; 9 | int l = 1, r = x; 10 | while (l <= r) { 11 | long long mid = l + (r - l) / 2; 12 | long long mid_squared = mid * mid; 13 | if (mid_squared <= x && (mid + 1) * (mid + 1) > x) 14 | return mid; 15 | if (mid_squared > x) { 16 | r = mid - 1; 17 | } else { 18 | l = mid + 1; 19 | } 20 | } 21 | return 0; 22 | } 23 | }; 24 | 25 | // Driver program to test the solution 26 | int main() { 27 | Solution sol; 28 | cout << sol.mySqrt(8) << endl; 29 | cout << sol.mySqrt(16) << endl; 30 | cout << sol.mySqrt(25) << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), 10 | * right(right) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | void inOrder(TreeNode* root, vector& vec) { 16 | if (!root) 17 | return; 18 | inOrder(root->left, vec); 19 | vec.push_back(root->val); 20 | inOrder(root->right, vec); 21 | } 22 | vector inorderTraversal(TreeNode* root) { 23 | vector ans; 24 | inOrder(root, ans); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /1431/1431_Kids_With_the_Greatest_Number_of_Candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector kidsWithCandies(vector& candies, int extraCandies) { 7 | vector ans; 8 | int maxCandies = *max_element(candies.begin(), candies.end()); 9 | for (int i = 0; i < candies.size(); i++) { 10 | ans.push_back(candies[i] + extraCandies >= maxCandies); 11 | } 12 | return ans; 13 | } 14 | }; 15 | 16 | // Driver program 17 | int main() { 18 | Solution solution; 19 | vector candies = {2, 3, 5, 1, 3}; 20 | int extraCandies = 3; 21 | vector result = solution.kidsWithCandies(candies, extraCandies); 22 | for (bool canHaveMost : result) { 23 | cout << (canHaveMost ? "true" : "false") << " "; 24 | } 25 | cout << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /stats.json: -------------------------------------------------------------------------------- 1 | {"leetcode":{"easy":2,"hard":0,"medium":0,"shas":{"0094-binary-tree-inorder-traversal":{"0094-binary-tree-inorder-traversal.cpp":"63346901e8746771a63a580d15ce8ba98961327f","README.md":"bd34dc7858ef18ca0298bb98d645a48123cf98b8","difficulty":"easy"},"2820-return-length-of-arguments-passed":{"2820-return-length-of-arguments-passed.js":"d46e2fff7f9396bc24f3750dbf51310650f47e9b","README.md":"24812759aa9fcaca7e7df3f765f96c29702f7f32"},"README.md":{"":"18c2d71ab4923fe2363eee6d3620df559e1712ca"},"stats.json":{"":"6449212a9afc9e496caa99b4f7c3dd98cd6b42c0"},"0104-maximum-depth-of-binary-tree":{"0104-maximum-depth-of-binary-tree.cpp":"0f1ed1b9bc97d6836bf17774a0c0850b1a51b99f","README.md":"3e88d40eac7067a0b69cceb90c76020b6f152ae0"},"0543-diameter-of-binary-tree":{"0543-diameter-of-binary-tree.cpp":"fee6be9653101de79a481ed6e07aedded93ca341","README.md":"5ef438e39aa6f6497073598c4bd2c8274ababac7","difficulty":"easy"}},"solved":2}} -------------------------------------------------------------------------------- /155/Min_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MinStack { 5 | public: 6 | stack st, st_min; 7 | 8 | MinStack() {} 9 | 10 | void push(int val) { 11 | st.push(val); 12 | if (st_min.empty() || st_min.top() >= val) st_min.push(val); 13 | } 14 | 15 | void pop() { 16 | if (st.top() == st_min.top()) st_min.pop(); 17 | st.pop(); 18 | } 19 | 20 | int top() { 21 | return st.top(); 22 | } 23 | 24 | int getMin() { 25 | return st_min.top(); 26 | } 27 | }; 28 | 29 | // Driver Program 30 | int main() { 31 | MinStack minStack; 32 | minStack.push(-2); 33 | minStack.push(0); 34 | minStack.push(-3); 35 | cout << minStack.getMin() << endl; // -3 36 | minStack.pop(); 37 | cout << minStack.top() << endl; // 0 38 | cout << minStack.getMin() << endl; // -2 39 | return 0; 40 | } -------------------------------------------------------------------------------- /26/26_Remove_Duplicates_from_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int removeDuplicates(vector &nums) 11 | { 12 | set st; 13 | st.insert(nums.begin(), nums.end()); 14 | nums.clear(); // Clear the original array 15 | nums.insert(nums.begin(), st.begin(), st.end()); 16 | return st.size(); 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | vector nums = {1, 1, 2}; // Example input 23 | Solution solution; 24 | int k = solution.removeDuplicates(nums); // Calls the solution function 25 | 26 | // Output the results 27 | cout << "Number of unique elements: " << k << endl; 28 | cout << "Array after removing duplicates: "; 29 | for (int i = 0; i < k; i++) 30 | { 31 | cout << nums[i] << " "; 32 | } 33 | cout << endl; 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /231/231_Power_of_two.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool isPowerOfTwo(int n) 8 | { 9 | if (n == 0) 10 | return false; 11 | int cnt = 0; 12 | while (n != 0) 13 | { 14 | if ((n & 1) == 1) 15 | { 16 | cnt++; 17 | } 18 | n = n >> 1; 19 | if (cnt == 2) 20 | break; 21 | } 22 | return (cnt == 1); 23 | } 24 | }; 25 | 26 | // Driver program to test the solution 27 | int main() 28 | { 29 | Solution solution; 30 | int testVal; 31 | 32 | cout << "Enter a number: "; 33 | cin >> testVal; 34 | 35 | if (solution.isPowerOfTwo(testVal)) 36 | { 37 | cout << testVal << " is a power of two." << endl; 38 | } 39 | else 40 | { 41 | cout << testVal << " is not a power of two." << endl; 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /3190/3190_Find_Minimum_Operations_to_Make_All_Elements_Divisible_by_Three.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int minimumOperations(vector &nums) 9 | { 10 | int n = nums.size(); 11 | int ans = 0; 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (nums[i] % 3 != 0) 15 | ans++; 16 | } 17 | return ans; 18 | } 19 | }; 20 | 21 | // Driver program to test the solution 22 | int main() 23 | { 24 | Solution solution; 25 | int n; 26 | 27 | cout << "Enter the number of elements: "; 28 | cin >> n; 29 | 30 | vector nums(n); 31 | cout << "Enter the elements: "; 32 | for (int i = 0; i < n; i++) 33 | { 34 | cin >> nums[i]; 35 | } 36 | 37 | int result = solution.minimumOperations(nums); 38 | 39 | cout << "Minimum operations: " << result << endl; 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /704/704_Binary_Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int search(int *nums, int numsSize, int target) 4 | { 5 | int start = 0; 6 | int end = numsSize - 1; 7 | 8 | while (start <= end) 9 | { 10 | int mid = (start + end) / 2; 11 | if (nums[mid] == target) 12 | { 13 | return mid; 14 | } 15 | else if (target > nums[mid]) 16 | { 17 | start = mid + 1; 18 | } 19 | else 20 | { 21 | end = mid - 1; 22 | } 23 | } 24 | return -1; 25 | } 26 | 27 | int main() 28 | { 29 | int nums[] = {1, 3, 5, 7, 9}; 30 | int numsSize = sizeof(nums) / sizeof(nums[0]); 31 | int target = 7; 32 | 33 | int result = search(nums, numsSize, target); 34 | 35 | if (result != -1) 36 | { 37 | printf("Element found at index %d\n", result); 38 | } 39 | else 40 | { 41 | printf("Element not found\n"); 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /1684/1684_Count_the_Number_of_Consistent_Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isAllowed(string allowed, string word) { 7 | for (int i = 0; i < word.size(); i++) { 8 | if (allowed.find(word[i]) == string::npos) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | 15 | int countConsistentStrings(string allowed, vector& words) { 16 | int ans = 0; 17 | for (int i = 0; i < words.size(); i++) { 18 | if (isAllowed(allowed, words[i])) { 19 | ans++; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; 25 | 26 | // Driver program 27 | int main() { 28 | Solution solution; 29 | string allowed = "abc"; 30 | vector words = {"a", "b", "c", "ab", "ac", "bc", "abc"}; 31 | cout << solution.countConsistentStrings(allowed, words) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /1920/1920_Build_array_from_permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | vector buildArray(vector &nums) 9 | { 10 | vector ans(nums.size()); 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | ans[i] = nums[nums[i]]; 14 | } 15 | return ans; 16 | } 17 | }; 18 | 19 | // Driver program to test the solution 20 | int main() 21 | { 22 | Solution solution; 23 | int n; 24 | 25 | cout << "Enter the number of elements: "; 26 | cin >> n; 27 | 28 | vector nums(n); 29 | cout << "Enter the elements: "; 30 | for (int i = 0; i < n; i++) 31 | { 32 | cin >> nums[i]; 33 | } 34 | 35 | vector result = solution.buildArray(nums); 36 | 37 | cout << "The resulting array is: "; 38 | for (int i = 0; i < result.size(); i++) 39 | { 40 | cout << result[i] << " "; 41 | } 42 | cout << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /503/503_Next_Greater_Int_II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector nextGreaterElements(vector& nums) 7 | { 8 | int n = nums.size(); 9 | stack st; 10 | vector ans; 11 | for (int i = (2 * n) - 1; i >= 0; i--) { 12 | int index = i % n; 13 | while (!st.empty() && st.top() <= nums[index]) { 14 | st.pop(); 15 | } 16 | 17 | if (i < n) { 18 | ans.emplace_back(st.empty() ? -1 : st.top()); 19 | } 20 | st.push(nums[index]); 21 | } 22 | reverse(ans.begin(), ans.end()); 23 | return ans; 24 | } 25 | }; 26 | 27 | // Driver program 28 | int main() { 29 | Solution sol; 30 | vector nums = {1, 2, 1}; 31 | vector result = sol.nextGreaterElements(nums); 32 | 33 | for (int val : result) { 34 | cout << val << " "; 35 | } 36 | cout << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /2942/2942_Find_Words_Containing_Character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | vector findWordsContaining(vector &words, char x) 8 | { 9 | vector ans; 10 | for (int i = 0; i < words.size(); i++) 11 | { 12 | if (words[i].find(x) != string::npos) 13 | { 14 | ans.push_back(i); 15 | } 16 | } 17 | return ans; 18 | } 19 | }; 20 | 21 | // Driver Program 22 | int main() 23 | { 24 | Solution sol; 25 | int n; 26 | cin >> n; // Number of words 27 | vector words(n); 28 | for (int i = 0; i < n; i++) 29 | { 30 | cin >> words[i]; // Input words 31 | } 32 | char x; 33 | cin >> x; // Character to search for 34 | vector result = sol.findWordsContaining(words, x); 35 | for (int idx : result) 36 | { 37 | cout << idx << " "; // Output indices of words containing the character 38 | } 39 | cout << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), 10 | * right(right) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | int depth(TreeNode* root) { 16 | if (!root) 17 | return 0; 18 | int lh = depth(root->left); 19 | int rh = depth(root->right); 20 | return 1 + max(lh, rh); 21 | } 22 | void dfs(TreeNode* root, int& maxi) { 23 | if (!root) 24 | return; 25 | maxi = max(maxi, depth(root->left) + depth(root->right)); 26 | dfs(root->left, maxi); 27 | dfs(root->right, maxi); 28 | } 29 | 30 | int diameterOfBinaryTree(TreeNode* root) { 31 | int maxi = INT_MIN; 32 | dfs(root, maxi); 33 | return maxi; 34 | } 35 | }; -------------------------------------------------------------------------------- /20/20_Valid_Parentheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isValid(string s) { 7 | stack st; 8 | for (char ch : s) { 9 | if ((ch == ')' || ch == ']' || ch == '}') && st.empty()) { 10 | return false; 11 | } else if (ch == '(' || ch == '[' || ch == '{') { 12 | st.push(ch); 13 | } else if ((ch == ')' && st.top() == '(') || 14 | (ch == ']' && st.top() == '[') || 15 | (ch == '}' && st.top() == '{')) { 16 | st.pop(); 17 | } else { 18 | return false; 19 | } 20 | } 21 | return st.empty(); 22 | } 23 | }; 24 | 25 | // Driver program 26 | int main() { 27 | Solution solution; 28 | vector testCases = {"()", "()[]{}", "(]", "([)]", "{[]}"}; 29 | for (const auto& testCase : testCases) { 30 | cout << "Input: " << testCase << " => Output: " 31 | << (solution.isValid(testCase) ? "true" : "false") << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /2820-return-length-of-arguments-passed/README.md: -------------------------------------------------------------------------------- 1 |

2820. Return Length of Arguments Passed

Easy


Write a function argumentsLength that returns the count of arguments passed to it. 2 |

 

3 |

Example 1:

4 | 5 |
 6 | Input: args = [5]
 7 | Output: 1
 8 | Explanation:
 9 | argumentsLength(5); // 1
10 | 
11 | One value was passed to the function so it should return 1.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: args = [{}, null, "3"]
18 | Output: 3
19 | Explanation: 
20 | argumentsLength({}, null, "3"); // 3
21 | 
22 | Three values were passed to the function so it should return 3.
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • args is a valid JSON array
  • 30 |
  • 0 <= args.length <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

104. Maximum Depth of Binary Tree

Easy


Given the root of a binary tree, return its maximum depth.

2 | 3 |

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [3,9,20,null,null,15,7]
10 | Output: 3
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [1,null,2]
17 | Output: 2
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [0, 104].
  • 25 |
  • -100 <= Node.val <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /42/42_Trapping_Rainwater_Optimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int trap(vector& height) { 7 | int n = height.size(); 8 | int left = 0, right = n - 1; 9 | int res = 0; 10 | int maxLeft = 0, maxRight = 0; 11 | while (left <= right) { 12 | if (height[left] <= height[right]) { 13 | if (height[left] >= maxLeft) { 14 | maxLeft = height[left]; 15 | } else { 16 | res += maxLeft - height[left]; 17 | } 18 | left++; 19 | } else { 20 | if (height[right] >= maxRight) { 21 | maxRight = height[right]; 22 | } else { 23 | res += maxRight - height[right]; 24 | } 25 | right--; 26 | } 27 | } 28 | return res; 29 | } 30 | }; 31 | 32 | // Driver program 33 | int main() { 34 | vector height = {0,1,0,2,1,0,1,3,2,1,2,1}; 35 | Solution sol; 36 | cout << sol.trap(height) << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /496/496_Next_Greater_Int_I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution { 9 | public: 10 | vector nextGreaterElement(vector& nums1, vector& nums2) { 11 | stack st; 12 | unordered_map mpp; 13 | vector ans; 14 | 15 | for (int i = nums2.size() - 1; i >= 0; i--) { 16 | while (!st.empty() && st.top() <= nums2[i]) 17 | st.pop(); 18 | mpp[nums2[i]] = st.empty() ? -1 : st.top(); 19 | st.push(nums2[i]); 20 | } 21 | 22 | for (int num : nums1) 23 | ans.push_back(mpp[num]); 24 | 25 | return ans; 26 | } 27 | }; 28 | 29 | // **Driver Program** 30 | int main() { 31 | Solution sol; 32 | vector nums1 = {4, 1, 2}; 33 | vector nums2 = {1, 3, 4, 2}; 34 | 35 | vector result = sol.nextGreaterElement(nums1, nums2); 36 | 37 | cout << "Next Greater Elements: "; 38 | for (int num : result) { 39 | cout << num << " "; 40 | } 41 | cout << endl; 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /2011/2011_Final_Value_of_Variable_After_Performing_Operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int finalValueAfterOperations(vector &operations) 10 | { 11 | int n = operations.size(); 12 | int ans = 0; 13 | for (int i = 0; i < n; i++) 14 | { 15 | if (operations[i][1] == '+') 16 | { 17 | ans++; 18 | continue; 19 | } 20 | ans--; 21 | } 22 | return ans; 23 | } 24 | }; 25 | 26 | // Driver program to test the solution 27 | int main() 28 | { 29 | Solution solution; 30 | int n; 31 | cout << "Enter the number of operations: "; 32 | cin >> n; 33 | 34 | vector operations(n); 35 | cout << "Enter the operations (e.g., ++X, X++, --X, X--): " << endl; 36 | for (int i = 0; i < n; i++) 37 | { 38 | cin >> operations[i]; 39 | } 40 | 41 | int result = solution.finalValueAfterOperations(operations); 42 | cout << "Final value after operations: " << result << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /1021/1021_Remove_Outermost_Parentheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | string removeOuterParentheses(string s) 8 | { 9 | string result; 10 | int level = 0; 11 | 12 | for (char c : s) 13 | { 14 | if (c == '(') 15 | { 16 | if (level > 0) 17 | result += c; 18 | level++; 19 | } 20 | else 21 | { 22 | level--; 23 | if (level > 0) 24 | result += c; 25 | } 26 | } 27 | 28 | return result; 29 | } 30 | }; 31 | 32 | // Driver code 33 | int main() 34 | { 35 | Solution sol; 36 | 37 | string s1 = "(()())(())"; 38 | cout << "Input: " << s1 << "\nOutput: " << sol.removeOuterParentheses(s1) << endl; 39 | 40 | string s2 = "(()())(())(()(()))"; 41 | cout << "Input: " << s2 << "\nOutput: " << sol.removeOuterParentheses(s2) << endl; 42 | 43 | string s3 = "()()"; 44 | cout << "Input: " << s3 << "\nOutput: " << sol.removeOuterParentheses(s3) << endl; 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /12/Integer_to_roman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string intToRoman(int num) { 7 | int n = num; 8 | string r = ""; 9 | while (n > 0) { 10 | if (n >= 1000) { r += 'M'; n -= 1000; } 11 | else if (n >= 900) { r += "CM"; n -= 900; } 12 | else if (n >= 500) { r += 'D'; n -= 500; } 13 | else if (n >= 400) { r += "CD"; n -= 400; } 14 | else if (n >= 100) { r += 'C'; n -= 100; } 15 | else if (n >= 90) { r += "XC"; n -= 90; } 16 | else if (n >= 50) { r += 'L'; n -= 50; } 17 | else if (n >= 40) { r += "XL"; n -= 40; } 18 | else if (n >= 10) { r += 'X'; n -= 10; } 19 | else if (n >= 9) { r += "IX"; n -= 9; } 20 | else if (n >= 5) { r += 'V'; n -= 5; } 21 | else if (n >= 4) { r += "IV"; n -= 4; } 22 | else if (n >= 1) { r += 'I'; n -= 1; } 23 | } 24 | return r; 25 | } 26 | }; 27 | 28 | // Driver program to test the solution 29 | int main() { 30 | Solution sol; 31 | cout << sol.intToRoman(1994) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /98/98_Validate_Binary_Search_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left, *right; 7 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | }; 11 | 12 | class Solution { 13 | public: 14 | bool validate(TreeNode* root, long long smaller, long long greater) { 15 | if (!root) return true; 16 | if (root->val > smaller && root->val < greater) 17 | return validate(root->left, smaller, root->val) && validate(root->right, root->val, greater); 18 | return false; 19 | } 20 | 21 | bool isValidBST(TreeNode* root) { 22 | return validate(root, LLONG_MIN, LLONG_MAX); 23 | } 24 | }; 25 | 26 | // Driver program to test the solution 27 | int main() { 28 | TreeNode* root = new TreeNode(2); 29 | root->left = new TreeNode(1); 30 | root->right = new TreeNode(3); 31 | 32 | Solution sol; 33 | cout << (sol.isValidBST(root) ? "true" : "false") << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /2220/2220_Minimum_Bit_Flips_to_Convert_Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int minBitFlips(int start, int goal) 8 | { 9 | int ans = 0; 10 | while (start != 0 && goal != 0) 11 | { 12 | if ((start & 1) != (goal & 1)) 13 | ans++; 14 | start = start >> 1; 15 | goal = goal >> 1; 16 | } 17 | while (start != 0) 18 | { 19 | if ((start & 1) != 0) 20 | ans++; 21 | start = start >> 1; 22 | } 23 | while (goal != 0) 24 | { 25 | if ((goal & 1) != 0) 26 | ans++; 27 | goal = goal >> 1; 28 | } 29 | return ans; 30 | } 31 | }; 32 | 33 | // Driver program to test the solution 34 | int main() 35 | { 36 | Solution solution; 37 | int start, goal; 38 | 39 | cout << "Enter start value: "; 40 | cin >> start; 41 | cout << "Enter goal value: "; 42 | cin >> goal; 43 | 44 | int result = solution.minBitFlips(start, goal); 45 | cout << "Minimum bit flips required: " << result << endl; 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /42/42_Trapping_Rain_Water_Brute_Force.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int trap(vector& height) { 7 | int total = 0; 8 | int n = height.size(); 9 | vector prefixMax(n); 10 | prefixMax[0] = height[0]; 11 | vector suffixMax(n); 12 | suffixMax[n - 1] = height[n - 1]; 13 | 14 | for (int i = 1; i < n; i++) { 15 | prefixMax[i] = max(prefixMax[i - 1], height[i]); 16 | } 17 | 18 | for (int i = n - 2; i >= 0; i--) { 19 | suffixMax[i] = max(suffixMax[i + 1], height[i]); 20 | } 21 | 22 | for (int i = 1; i < height.size() - 1; i++) { 23 | if (height[i] < prefixMax[i] && height[i] < suffixMax[i]) { 24 | total += min(prefixMax[i], suffixMax[i]) - height[i]; 25 | } 26 | } 27 | return total; 28 | } 29 | }; 30 | 31 | // Driver program 32 | int main() { 33 | Solution sol; 34 | vector height = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; 35 | int result = sol.trap(height); 36 | 37 | cout << result << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /1512/1512_Number_of_Good_Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int numIdenticalPairs(vector &nums) 10 | { 11 | int count = 0; 12 | unordered_map mpp; 13 | for (int i = 0; i < nums.size(); i++) 14 | { 15 | if (mpp.find(nums[i]) == mpp.end()) 16 | { 17 | mpp[nums[i]]++; 18 | } 19 | else 20 | { 21 | count += mpp[nums[i]]; 22 | mpp[nums[i]]++; 23 | } 24 | } 25 | return count; 26 | } 27 | }; 28 | 29 | // Driver program to test the solution 30 | int main() 31 | { 32 | Solution solution; 33 | int n; 34 | 35 | cout << "Enter the number of elements in the array: "; 36 | cin >> n; 37 | 38 | vector nums(n); 39 | cout << "Enter the elements of the array: "; 40 | for (int i = 0; i < n; i++) 41 | { 42 | cin >> nums[i]; 43 | } 44 | 45 | int result = solution.numIdenticalPairs(nums); 46 | cout << "Number of good pairs: " << result << endl; 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /503_Own_Approach/503_Next_Greater_Int_II_Own_approach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector nextGreaterElements(vector& nums) { 7 | stack st; 8 | vector ans; 9 | int n = nums.size(); 10 | for (int i = n - 1; i >= 0; i--) { 11 | while (!st.empty() && st.top() <= nums[i]) 12 | st.pop(); 13 | 14 | st.push(nums[i]); 15 | } 16 | for (int i = n - 1; i >= 0; i--) { 17 | while (!st.empty() && st.top() <= nums[i]) 18 | st.pop(); 19 | if (st.empty()) { 20 | ans.emplace_back(-1); 21 | } else { 22 | ans.emplace_back(st.top()); 23 | } 24 | 25 | st.push(nums[i]); 26 | } 27 | reverse(ans.begin(), ans.end()); 28 | return ans; 29 | } 30 | }; 31 | 32 | // Driver program 33 | int main() { 34 | Solution sol; 35 | vector nums = {1, 2, 1}; 36 | vector result = sol.nextGreaterElements(nums); 37 | 38 | for (int val : result) { 39 | cout << val << " "; 40 | } 41 | cout << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /35/35_Search_Insert_Position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int searchInsert(vector &nums, int target) 11 | { 12 | return lower_bound(nums.begin(), nums.end(), target) - nums.begin(); 13 | } 14 | }; 15 | 16 | int main() 17 | { 18 | Solution solution; 19 | 20 | // Test cases 21 | vector nums1 = {1, 3, 5, 6}; 22 | int target1 = 5; 23 | cout << "Insert position for target " << target1 << " in nums1: " << solution.searchInsert(nums1, target1) << endl; // Output: 2 24 | 25 | vector nums2 = {1, 3, 5, 6}; 26 | int target2 = 2; 27 | cout << "Insert position for target " << target2 << " in nums2: " << solution.searchInsert(nums2, target2) << endl; // Output: 1 28 | 29 | vector nums3 = {1, 3, 5, 6}; 30 | int target3 = 7; 31 | cout << "Insert position for target " << target3 << " in nums3: " << solution.searchInsert(nums3, target3) << endl; // Output: 4 32 | 33 | vector nums4 = {1, 3, 5, 6}; 34 | int target4 = 0; 35 | cout << "Insert position for target " << target4 << " in nums4: " << solution.searchInsert(nums4, target4) << endl; // Output: 0 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /3289/3289_The_Two_Sneaky_Numbers_of_Digitville.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | vector getSneakyNumbers(vector &nums) 10 | { 11 | int n = nums.size(); 12 | vector ans; 13 | unordered_map mpp; 14 | for (int i = 0; i < n; i++) 15 | { 16 | mpp[nums[i]]++; 17 | if (mpp[nums[i]] == 2) 18 | { 19 | ans.push_back(nums[i]); 20 | if (ans.size() == 2) 21 | return ans; 22 | } 23 | } 24 | return ans; 25 | } 26 | }; 27 | 28 | // Driver program to test the solution 29 | int main() 30 | { 31 | Solution solution; 32 | 33 | vector nums = {4, 5, 6, 4, 5, 7, 8}; 34 | 35 | cout << "Original array: "; 36 | for (int num : nums) 37 | { 38 | cout << num << " "; 39 | } 40 | cout << endl; 41 | 42 | vector sneakyNumbers = solution.getSneakyNumbers(nums); 43 | 44 | cout << "Sneaky numbers (first two duplicates): "; 45 | for (int num : sneakyNumbers) 46 | { 47 | cout << num << " "; 48 | } 49 | cout << endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /225/225_Implement_Stack_Using_Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class MyStack { 6 | private: 7 | queue q; 8 | 9 | public: 10 | MyStack() {} 11 | void push(int x) { 12 | if (q.empty()) { 13 | q.push(x); 14 | return; 15 | } else { 16 | q.push(x); 17 | for (int i = 0; i < q.size() - 1; i++) { 18 | int a = q.front(); 19 | q.pop(); 20 | q.push(a); 21 | } 22 | } 23 | } 24 | 25 | int pop() { 26 | int a = q.front(); 27 | q.pop(); 28 | return a; 29 | } 30 | 31 | int top() { 32 | return q.front(); 33 | } 34 | 35 | bool empty() { 36 | return q.empty(); 37 | } 38 | }; 39 | 40 | // Driver program 41 | int main() { 42 | MyStack* obj = new MyStack(); 43 | obj->push(1); 44 | obj->push(2); 45 | obj->push(3); 46 | cout << "Top element: " << obj->top() << endl; // Output: 3 47 | cout << "Pop element: " << obj->pop() << endl; // Output: 3 48 | cout << "Top element after pop: " << obj->top() << endl; // Output: 2 49 | cout << "Is stack empty? " << (obj->empty() ? "Yes" : "No") << endl; // Output: No 50 | return 0; 51 | } -------------------------------------------------------------------------------- /240/240_Search_a_2D_Matrix_II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool searchMatrix(vector> &matrix, int target) 8 | { 9 | int n = matrix.size(); 10 | int m = matrix[0].size(); 11 | int row = 0; 12 | int col = m - 1; 13 | 14 | while (row < n && col >= 0) 15 | { 16 | if (matrix[row][col] == target) 17 | { 18 | return true; 19 | } 20 | else if (matrix[row][col] < target) 21 | { 22 | row++; 23 | } 24 | else 25 | { 26 | col--; 27 | } 28 | } 29 | return false; 30 | } 31 | }; 32 | 33 | // Driver code 34 | int main() 35 | { 36 | Solution sol; 37 | 38 | vector> matrix = { 39 | {1, 4, 7, 11, 15}, 40 | {2, 5, 8, 12, 19}, 41 | {3, 6, 9, 16, 22}, 42 | {10, 13, 14, 17, 24}, 43 | {18, 21, 23, 26, 30}}; 44 | 45 | int target = 5; 46 | cout << (sol.searchMatrix(matrix, target) ? "Found" : "Not Found") << endl; 47 | 48 | target = 20; 49 | cout << (sol.searchMatrix(matrix, target) ? "Found" : "Not Found") << endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /3146/3146_Permutation_Difference_between_Two_Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int findPermutationDifference(string s, string t) 10 | { 11 | int n = s.size(); 12 | unordered_map mpp; 13 | int i = 0; 14 | int ans = 0; 15 | 16 | for (char ch : s) 17 | { 18 | mpp[ch] = i; 19 | i++; 20 | } 21 | 22 | i = 0; 23 | for (char ch : t) 24 | { 25 | ans += abs(i - mpp[ch]); 26 | i++; 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | 33 | // Driver program to test the solution 34 | int main() 35 | { 36 | Solution solution; 37 | string s, t; 38 | 39 | cout << "Enter the first string (permutation of letters): "; 40 | cin >> s; 41 | 42 | cout << "Enter the second string (permutation of letters): "; 43 | cin >> t; 44 | 45 | if (s.size() != t.size()) 46 | { 47 | cout << "The strings must be of the same length." << endl; 48 | return 1; 49 | } 50 | 51 | int result = solution.findPermutationDifference(s, t); 52 | cout << "Permutation difference: " << result << endl; 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /1539/1539_kth_missing_positive_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int findKthPositive(vector &arr, int k) 10 | { 11 | int n = arr.size(); 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (arr[i] <= k) 15 | k++; 16 | } 17 | return k; 18 | } 19 | }; 20 | 21 | int main() 22 | { 23 | Solution solution; 24 | 25 | // Test case 1 26 | vector arr1 = {2, 3, 4, 7, 11}; 27 | int k1 = 5; 28 | cout << "Test case 1: The 5th missing positive number is " << solution.findKthPositive(arr1, k1) << endl; 29 | 30 | // Test case 2 31 | vector arr2 = {1, 2, 3, 4}; 32 | int k2 = 2; 33 | cout << "Test case 2: The 2nd missing positive number is " << solution.findKthPositive(arr2, k2) << endl; 34 | 35 | // Test case 3 36 | vector arr3 = {5, 6, 7, 8, 9}; 37 | int k3 = 9; 38 | cout << "Test case 3: The 9th missing positive number is " << solution.findKthPositive(arr3, k3) << endl; 39 | 40 | // Test case 4 41 | vector arr4 = {1, 3, 5, 7}; 42 | int k4 = 3; 43 | cout << "Test case 4: The 3rd missing positive number is " << solution.findKthPositive(arr4, k4) << endl; 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /876/876_Middle_of_the_Linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *middleNode(ListNode *head) 18 | { 19 | ListNode *mover = head; 20 | int n = 1; 21 | while (mover->next != NULL) 22 | { 23 | mover = mover->next; 24 | n++; 25 | } 26 | mover = head; 27 | int i = (n / 2); 28 | while (i--) 29 | { 30 | mover = mover->next; 31 | } 32 | return mover; 33 | } 34 | }; 35 | 36 | // Driver program 37 | int main() 38 | { 39 | Solution sol; 40 | 41 | // Creating a linked list: 1 -> 2 -> 3 -> 4 -> 5 42 | ListNode *head = new ListNode(1); 43 | head->next = new ListNode(2); 44 | head->next->next = new ListNode(3); 45 | head->next->next->next = new ListNode(4); 46 | head->next->next->next->next = new ListNode(5); 47 | 48 | ListNode *middle = sol.middleNode(head); 49 | 50 | cout << "Middle node value: " << middle->val << endl; 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /237/237_Delete_Node_in_a_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | void deleteNode(ListNode *node) 16 | { 17 | int next_val = node->next->val; 18 | ListNode *ptr = node->next->next; 19 | node->val = next_val; 20 | node->next = ptr; 21 | } 22 | }; 23 | 24 | // Helper function to print the linked list 25 | void printList(ListNode *head) 26 | { 27 | ListNode *temp = head; 28 | while (temp != nullptr) 29 | { 30 | cout << temp->val << " "; 31 | temp = temp->next; 32 | } 33 | cout << endl; 34 | } 35 | 36 | // Driver code 37 | int main() 38 | { 39 | // Creating a linked list: 4 -> 5 -> 1 -> 9 40 | ListNode *head = new ListNode(4); 41 | head->next = new ListNode(5); 42 | head->next->next = new ListNode(1); 43 | head->next->next->next = new ListNode(9); 44 | 45 | cout << "Original list: "; 46 | printList(head); 47 | 48 | // Node to be deleted: 5 49 | Solution sol; 50 | sol.deleteNode(head->next); // Deleting node with value 5 51 | 52 | cout << "After deletion: "; 53 | printList(head); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /74/74_Search_in_a_2D_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool searchMatrix(vector> &matrix, int target) 8 | { 9 | int n = matrix.size(); 10 | int m = matrix[0].size(); 11 | int low = 0; 12 | int high = (n * m) - 1; 13 | while (low <= high) 14 | { 15 | int mid = low + (high - low) / 2; 16 | int mid_row = mid / m; 17 | int mid_col = mid % m; 18 | if (matrix[mid_row][mid_col] == target) 19 | { 20 | return true; 21 | } 22 | else if (matrix[mid_row][mid_col] < target) 23 | { 24 | low = mid + 1; 25 | } 26 | else 27 | { 28 | high = mid - 1; 29 | } 30 | } 31 | return false; 32 | } 33 | }; 34 | 35 | // Driver code 36 | int main() 37 | { 38 | Solution sol; 39 | 40 | vector> matrix = { 41 | {1, 3, 5, 7}, 42 | {10, 11, 16, 20}, 43 | {23, 30, 34, 60}}; 44 | int target = 3; 45 | cout << (sol.searchMatrix(matrix, target) ? "Found" : "Not Found") << endl; 46 | 47 | target = 13; 48 | cout << (sol.searchMatrix(matrix, target) ? "Found" : "Not Found") << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /0543-diameter-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

543. Diameter of Binary Tree

Easy


Given the root of a binary tree, return the length of the diameter of the tree.

2 | 3 |

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

4 | 5 |

The length of a path between two nodes is represented by the number of edges between them.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: root = [1,2,3,4,5]
12 | Output: 3
13 | Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: root = [1,2]
20 | Output: 1
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • The number of nodes in the tree is in the range [1, 104].
  • 28 |
  • -100 <= Node.val <= 100
  • 29 |
30 | -------------------------------------------------------------------------------- /125/125_Valid_Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | bool isPalindrome(string s) 9 | { 10 | int start = 0; 11 | int end = s.size() - 1; 12 | while (start <= end) 13 | { 14 | if (!isalnum(s[start])) 15 | { 16 | start++; 17 | continue; 18 | } 19 | if (!isalnum(s[end])) 20 | { 21 | end--; 22 | continue; 23 | } 24 | if (tolower(s[start]) != tolower(s[end])) 25 | return false; 26 | start++; 27 | end--; 28 | } 29 | return true; 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | Solution sol; 36 | 37 | // Test cases 38 | string test1 = "A man, a plan, a canal: Panama"; 39 | string test2 = "race a car"; 40 | string test3 = " "; 41 | string test4 = "No 'x' in Nixon"; 42 | 43 | // Check and print results 44 | cout << "Test 1: " << (sol.isPalindrome(test1) ? "True" : "False") << endl; 45 | cout << "Test 2: " << (sol.isPalindrome(test2) ? "True" : "False") << endl; 46 | cout << "Test 3: " << (sol.isPalindrome(test3) ? "True" : "False") << endl; 47 | cout << "Test 4: " << (sol.isPalindrome(test4) ? "True" : "False") << endl; 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /1752/1752._Check_if_Array_Is_Sorted_and_Rotated.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | bool check(const vector &nums) const 10 | { 11 | int n = nums.size(); 12 | int cnt = 0; 13 | for (int i = 0; i < n - 1; i++) 14 | { 15 | if (nums[i] > nums[i + 1]) 16 | { 17 | cnt++; 18 | } 19 | } 20 | if (nums[n - 1] > nums[0]) 21 | { 22 | cnt++; 23 | } 24 | return cnt <= 1; 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | Solution sol; 31 | 32 | // Test cases 33 | vector> test_cases = { 34 | {1, 2, 3, 4, 5}, // True: Already sorted 35 | {3, 4, 5, 1, 2}, // True: Sorted and rotated 36 | {2, 1, 3, 4, 5}, // False: Not sorted and rotated 37 | {1, 3, 5, 7, 2}, // False: More than one rotation point 38 | {5, 1, 2, 3, 4} // True: Sorted and rotated 39 | }; 40 | 41 | for (const auto &test_case : test_cases) 42 | { 43 | bool result = sol.check(test_case); 44 | cout << "check({"; 45 | for (size_t i = 0; i < test_case.size(); ++i) 46 | { 47 | cout << test_case[i]; 48 | if (i < test_case.size() - 1) 49 | cout << ", "; 50 | } 51 | cout << "}) = " << (result ? "True" : "False") << endl; 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /94/94_In_order_Traversal_of_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | */ 7 | struct TreeNode { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | }; 15 | 16 | class Solution { 17 | public: 18 | vector inorderTraversal(TreeNode* root) { 19 | vector ans; 20 | inOrder(ans, root); 21 | return ans; 22 | } 23 | 24 | void inOrder(vector& vec, TreeNode* node) { 25 | if (node == nullptr) return; 26 | inOrder(vec, node->left); 27 | vec.push_back(node->val); 28 | inOrder(vec, node->right); 29 | } 30 | }; 31 | 32 | // Driver program 33 | int main() { 34 | // Creating a sample tree: 35 | // 1 36 | // \ 37 | // 2 38 | // / 39 | // 3 40 | TreeNode* root = new TreeNode(1); 41 | root->right = new TreeNode(2); 42 | root->right->left = new TreeNode(3); 43 | 44 | Solution sol; 45 | vector result = sol.inorderTraversal(root); 46 | 47 | // Output the inorder traversal 48 | cout << "Inorder Traversal: "; 49 | for (int val : result) { 50 | cout << val << " "; 51 | } 52 | cout << endl; 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /145/145_Post_Order_Traversal_Of_Binary_trees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | */ 7 | struct TreeNode { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | }; 15 | 16 | class Solution { 17 | public: 18 | vector postorderTraversal(TreeNode* root) { 19 | vector ans; 20 | postOrder(ans, root); 21 | return ans; 22 | } 23 | 24 | void postOrder(vector& vec, TreeNode* node) { 25 | if (node == nullptr) return; 26 | postOrder(vec, node->left); 27 | postOrder(vec, node->right); 28 | vec.push_back(node->val); 29 | } 30 | }; 31 | 32 | // Driver program 33 | int main() { 34 | // Creating a sample tree: 35 | // 1 36 | // \ 37 | // 2 38 | // / 39 | // 3 40 | TreeNode* root = new TreeNode(1); 41 | root->right = new TreeNode(2); 42 | root->right->left = new TreeNode(3); 43 | 44 | Solution sol; 45 | vector result = sol.postorderTraversal(root); 46 | 47 | // Output the postorder traversal 48 | cout << "Postorder Traversal: "; 49 | for (int val : result) { 50 | cout << val << " "; 51 | } 52 | cout << endl; 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /144/144_Pre_Order-Traversal_of_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * Definition for a binary tree node. 6 | */ 7 | struct TreeNode { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | }; 15 | 16 | class Solution { 17 | public: 18 | void preOrder(vector& vec, TreeNode* node) { 19 | if (node == nullptr) 20 | return; 21 | vec.push_back(node->val); 22 | preOrder(vec, node->left); 23 | preOrder(vec, node->right); 24 | } 25 | 26 | vector preorderTraversal(TreeNode* root) { 27 | vector ans; 28 | preOrder(ans, root); 29 | return ans; 30 | } 31 | }; 32 | 33 | // Driver program 34 | int main() { 35 | // Creating a sample tree: 36 | // 1 37 | // \ 38 | // 2 39 | // / 40 | // 3 41 | TreeNode* root = new TreeNode(1); 42 | root->right = new TreeNode(2); 43 | root->right->left = new TreeNode(3); 44 | 45 | Solution sol; 46 | vector result = sol.preorderTraversal(root); 47 | 48 | // Output the preorder traversal 49 | cout << "Preorder Traversal: "; 50 | for (int val : result) { 51 | cout << val << " "; 52 | } 53 | cout << endl; 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /75/75_sort_colors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution 5 | { 6 | public: 7 | void sortColors(std::vector &nums) 8 | { 9 | int zero = 0; 10 | int one = 0; 11 | int two = 0; 12 | 13 | // Count occurrences of 0, 1, and 2 14 | for (int i = 0; i < nums.size(); i++) 15 | { 16 | if (nums[i] == 0) 17 | zero++; 18 | else if (nums[i] == 1) 19 | one++; 20 | else 21 | two++; 22 | } 23 | 24 | // Update the array based on counts 25 | for (int i = 0; i < nums.size(); i++) 26 | { 27 | if (zero > 0) 28 | { 29 | nums[i] = 0; 30 | zero--; 31 | continue; 32 | } 33 | else if (one > 0) 34 | { 35 | nums[i] = 1; 36 | one--; 37 | continue; 38 | } 39 | else 40 | { 41 | nums[i] = 2; 42 | two--; 43 | if (two == 0) 44 | break; 45 | } 46 | } 47 | } 48 | }; 49 | 50 | // Driver program 51 | int main() 52 | { 53 | std::vector nums = {2, 0, 2, 1, 1, 0}; 54 | 55 | Solution solution; 56 | solution.sortColors(nums); 57 | 58 | // Output the sorted array 59 | for (int num : nums) 60 | { 61 | std::cout << num << " "; 62 | } 63 | std::cout << std::endl; 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /907/907_Sum_of_Subarray_minimums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector nsi(vector& arr) { 7 | stack st; 8 | int n = arr.size(); 9 | vector ansVec(n, n); 10 | for (int i = n - 1; i >= 0; i--) { 11 | while (!st.empty() && arr[st.top()] > arr[i]) 12 | st.pop(); 13 | ansVec[i] = st.empty() ? n : st.top(); 14 | st.push(i); 15 | } 16 | return ansVec; 17 | } 18 | 19 | vector psi(vector& arr) { 20 | stack st; 21 | int n = arr.size(); 22 | vector ansVec(n, -1); 23 | for (int i = 0; i < n; i++) { 24 | while (!st.empty() && arr[st.top()] >= arr[i]) 25 | st.pop(); 26 | ansVec[i] = st.empty() ? -1 : st.top(); 27 | st.push(i); 28 | } 29 | return ansVec; 30 | } 31 | 32 | int sumSubarrayMins(vector& arr) { 33 | vector prev = psi(arr); 34 | vector next = nsi(arr); 35 | const int MOD = 1000000007; 36 | long long total = 0; 37 | for (int i = 0; i < arr.size(); i++) { 38 | total = (total + (((((i - prev[i]) % MOD) * (next[i] - i) % MOD) % MOD) * 1LL * arr[i]) % MOD) % MOD; 39 | } 40 | return total; 41 | } 42 | }; 43 | 44 | // Driver program 45 | int main() { 46 | vector arr = {3, 1, 2, 4}; 47 | Solution sol; 48 | cout << sol.sumSubarrayMins(arr) << endl; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /1894/1894_Chalk_replacer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int chalkReplacer(vector &chalk, int k) 9 | { 10 | long long total_chalk = 0; 11 | int students = chalk.size(); 12 | for (int i = 0; i < students; i++) 13 | { 14 | total_chalk += chalk[i]; 15 | } 16 | 17 | k = k % total_chalk; 18 | 19 | for (int i = 0; i < students; i++) 20 | { 21 | if (k < chalk[i]) 22 | { 23 | return i; 24 | } 25 | k -= chalk[i]; 26 | } 27 | return -1; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | Solution solution; 34 | 35 | // Test case 1 36 | vector chalk1 = {5, 1, 5}; 37 | int k1 = 22; 38 | cout << "Test case 1: Student who will replace the chalk = " << solution.chalkReplacer(chalk1, k1) << endl; 39 | 40 | // Test case 2 41 | vector chalk2 = {3, 4, 1, 2}; 42 | int k2 = 25; 43 | cout << "Test case 2: Student who will replace the chalk = " << solution.chalkReplacer(chalk2, k2) << endl; 44 | 45 | // Test case 3 46 | vector chalk3 = {1, 1, 1, 1}; 47 | int k3 = 7; 48 | cout << "Test case 3: Student who will replace the chalk = " << solution.chalkReplacer(chalk3, k3) << endl; 49 | 50 | // Test case 4 51 | vector chalk4 = {10}; 52 | int k4 = 100; 53 | cout << "Test case 4: Student who will replace the chalk = " << solution.chalkReplacer(chalk4, k4) << endl; 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /78/Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | vector> subsets(vector &nums) 10 | { 11 | vector> ans; 12 | int s = nums.size(); 13 | int n = pow(2, s); 14 | for (int i = 0; i < n; i++) 15 | { 16 | int num = i; 17 | int index = 0; 18 | vector vec; 19 | while (num != 0) 20 | { 21 | if (num & 1) 22 | { 23 | if (index < s) 24 | vec.push_back(nums[index]); 25 | } 26 | index++; 27 | num = num >> 1; 28 | } 29 | ans.push_back(vec); 30 | } 31 | return ans; 32 | } 33 | }; 34 | 35 | // Driver program to test the solution 36 | int main() 37 | { 38 | Solution solution; 39 | int n; 40 | 41 | cout << "Enter number of elements in the array: "; 42 | cin >> n; 43 | 44 | vector nums(n); 45 | cout << "Enter elements of the array: "; 46 | for (int i = 0; i < n; i++) 47 | { 48 | cin >> nums[i]; 49 | } 50 | 51 | vector> result = solution.subsets(nums); 52 | cout << "The subsets are:" << endl; 53 | for (const auto &subset : result) 54 | { 55 | cout << "{ "; 56 | for (int num : subset) 57 | { 58 | cout << num << " "; 59 | } 60 | cout << "}" << endl; 61 | } 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /2181/2181_Merge_Nodes_in_Between_Zeros.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution { 13 | public: 14 | ListNode* mergeNodes(ListNode* head) { 15 | ListNode* temp = head->next; 16 | ListNode* current = head; 17 | 18 | while (temp) { 19 | int sum = 0; 20 | while (temp && temp->val != 0) { 21 | sum += temp->val; 22 | temp = temp->next; 23 | } 24 | current->val = sum; 25 | if (temp && temp->next) { 26 | current->next = current->next->next; 27 | current = current->next; 28 | } else { 29 | current->next = nullptr; 30 | } 31 | if (temp) temp = temp->next; 32 | } 33 | 34 | return head; 35 | } 36 | }; 37 | 38 | // Driver Program 39 | int main() { 40 | Solution sol; 41 | vector values = {0, 3, 1, 0, 4, 5, 2, 0}; // Example input 42 | ListNode* head = new ListNode(0); 43 | ListNode* temp = head; 44 | for (int i = 1; i < values.size(); i++) { 45 | temp->next = new ListNode(values[i]); 46 | temp = temp->next; 47 | } 48 | head = sol.mergeNodes(head); 49 | temp = head; 50 | while (temp) { 51 | cout << temp->val << " "; 52 | temp = temp->next; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /328/328_Odd_even_LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *oddEvenList(ListNode *head) 18 | { 19 | if (head == NULL || head->next == NULL) 20 | return head; 21 | 22 | ListNode *odd = head; 23 | ListNode *even = head->next; 24 | ListNode *evenhead = even; 25 | 26 | while (odd->next != NULL && even->next != NULL) 27 | { 28 | odd->next = odd->next->next; 29 | even->next = even->next->next; 30 | odd = odd->next; 31 | even = even->next; 32 | } 33 | odd->next = evenhead; 34 | return head; 35 | } 36 | }; 37 | 38 | // Driver program 39 | int main() 40 | { 41 | Solution sol; 42 | 43 | // Create a linked list: 1 -> 2 -> 3 -> 4 -> 5 44 | ListNode *head = new ListNode(1); 45 | head->next = new ListNode(2); 46 | head->next->next = new ListNode(3); 47 | head->next->next->next = new ListNode(4); 48 | head->next->next->next->next = new ListNode(5); 49 | 50 | // Rearrange the linked list to odd-even format 51 | ListNode *newHead = sol.oddEvenList(head); 52 | 53 | // Print the rearranged linked list 54 | while (newHead != NULL) 55 | { 56 | cout << newHead->val << " "; 57 | newHead = newHead->next; 58 | } 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /142/142_Linked_list_cycle_II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | ListNode *detectCycle(ListNode *head) 16 | { 17 | if (head == NULL) 18 | return NULL; 19 | else if (head->next == NULL) 20 | return NULL; 21 | set st; 22 | ListNode *temp = head; 23 | while (temp != NULL) 24 | { 25 | if (st.find(temp) != st.end()) 26 | { 27 | return temp; // Cycle detected 28 | } 29 | st.insert(temp); 30 | temp = temp->next; 31 | } 32 | return NULL; // No cycle 33 | } 34 | }; 35 | 36 | // Driver program 37 | int main() 38 | { 39 | Solution sol; 40 | 41 | // Creating a linked list with a cycle: 1 -> 2 -> 3 -> 4 -> 5 -> 2 (cycle starts at node 2) 42 | ListNode *head = new ListNode(1); 43 | head->next = new ListNode(2); 44 | head->next->next = new ListNode(3); 45 | head->next->next->next = new ListNode(4); 46 | head->next->next->next->next = new ListNode(5); 47 | head->next->next->next->next->next = head->next; // Creating a cycle at node 2 48 | 49 | ListNode *cycleNode = sol.detectCycle(head); 50 | 51 | if (cycleNode != NULL) 52 | { 53 | cout << "Cycle detected at node with value: " << cycleNode->val << endl; 54 | } 55 | else 56 | { 57 | cout << "No cycle detected" << endl; 58 | } 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /206/206_Reverse_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *reverseList(ListNode *head) 18 | { 19 | if (head == NULL) 20 | return NULL; 21 | ListNode *temp = head; 22 | 23 | stack vec; 24 | while (temp != NULL) 25 | { 26 | vec.push(temp->val); 27 | temp = temp->next; 28 | } 29 | 30 | temp = head; 31 | while (temp != NULL) 32 | { 33 | temp->val = vec.top(); 34 | vec.pop(); 35 | temp = temp->next; 36 | } 37 | 38 | return head; 39 | } 40 | }; 41 | 42 | // Driver program 43 | int main() 44 | { 45 | Solution sol; 46 | 47 | // Creating a linked list: 1 -> 2 -> 3 -> 4 -> 5 48 | ListNode *head = new ListNode(1); 49 | head->next = new ListNode(2); 50 | head->next->next = new ListNode(3); 51 | head->next->next->next = new ListNode(4); 52 | head->next->next->next->next = new ListNode(5); 53 | 54 | // Reversing the list 55 | ListNode *reversedHead = sol.reverseList(head); 56 | 57 | // Output reversed list 58 | ListNode *temp = reversedHead; 59 | while (temp != NULL) 60 | { 61 | cout << temp->val << " "; 62 | temp = temp->next; 63 | } 64 | cout << endl; 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /622/622_Circular_Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MyCircularQueue { 5 | public: 6 | vector vec; 7 | int rear, front, k; 8 | 9 | MyCircularQueue(int k) { 10 | this->k = k; 11 | vec.resize(k); 12 | front = -1; 13 | rear = -1; 14 | } 15 | 16 | bool enQueue(int value) { 17 | if (isFull()) return false; 18 | 19 | if (isEmpty()) { 20 | front = rear = 0; 21 | } else { 22 | rear = (rear + 1) % k; 23 | } 24 | vec[rear] = value; 25 | return true; 26 | } 27 | 28 | bool deQueue() { 29 | if (isEmpty()) return false; 30 | 31 | if (front == rear) { 32 | front = rear = -1; 33 | } else { 34 | front = (front + 1) % k; 35 | } 36 | return true; 37 | } 38 | 39 | int Front() { 40 | return isEmpty() ? -1 : vec[front]; 41 | } 42 | 43 | int Rear() { 44 | return isEmpty() ? -1 : vec[rear]; 45 | } 46 | 47 | bool isEmpty() { 48 | return front == -1; 49 | } 50 | 51 | bool isFull() { 52 | return (rear + 1) % k == front; 53 | } 54 | }; 55 | 56 | // Driver program 57 | int main() { 58 | MyCircularQueue q(3); 59 | 60 | cout << q.enQueue(1) << endl; 61 | cout << q.enQueue(2) << endl; 62 | cout << q.enQueue(3) << endl; 63 | cout << q.enQueue(4) << endl; 64 | cout << q.Rear() << endl; 65 | cout << q.isFull() << endl; 66 | cout << q.deQueue() << endl; 67 | cout << q.enQueue(4) << endl; 68 | cout << q.Rear() << endl; 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /206/206_Reverse_LL_using_optimal_approach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *reverseList(ListNode *head) 18 | { 19 | if (head == NULL || head->next == NULL) 20 | return head; 21 | ListNode *back = NULL; 22 | ListNode *current = head; 23 | ListNode *front = head->next; 24 | while (current != NULL) 25 | { 26 | ListNode *temp = front; 27 | current->next = back; 28 | back = current; 29 | current = front; 30 | if (front != NULL) 31 | { 32 | front = front->next; 33 | } 34 | } 35 | return back; 36 | } 37 | }; 38 | 39 | // Driver program 40 | int main() 41 | { 42 | Solution sol; 43 | 44 | // Creating a linked list: 1 -> 2 -> 3 -> 4 -> 5 45 | ListNode *head = new ListNode(1); 46 | head->next = new ListNode(2); 47 | head->next->next = new ListNode(3); 48 | head->next->next->next = new ListNode(4); 49 | head->next->next->next->next = new ListNode(5); 50 | 51 | // Reversing the list 52 | ListNode *reversedHead = sol.reverseList(head); 53 | 54 | // Output reversed list 55 | ListNode *temp = reversedHead; 56 | while (temp != NULL) 57 | { 58 | cout << temp->val << " "; 59 | temp = temp->next; 60 | } 61 | cout << endl; 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /160/160_Intersection_of_two_LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Definition for singly-linked list. 4 | struct ListNode 5 | { 6 | int val; 7 | ListNode *next; 8 | ListNode(int x) : val(x), next(NULL) {} 9 | }; 10 | 11 | class Solution 12 | { 13 | public: 14 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 15 | { 16 | if (headA == nullptr || headB == nullptr) 17 | return nullptr; 18 | 19 | ListNode *tempA = headA; 20 | ListNode *tempB = headB; 21 | 22 | // Traverse both lists, if one pointer reaches the end, switch to the other list 23 | while (tempA != tempB) 24 | { 25 | tempA = (tempA == nullptr) ? headB : tempA->next; 26 | tempB = (tempB == nullptr) ? headA : tempB->next; 27 | } 28 | return tempA; // Either the intersection node or nullptr 29 | } 30 | }; 31 | 32 | // Driver program 33 | int main() 34 | { 35 | // Create two linked lists that intersect 36 | ListNode *common = new ListNode(8); 37 | common->next = new ListNode(10); 38 | 39 | ListNode *headA = new ListNode(4); 40 | headA->next = new ListNode(1); 41 | headA->next->next = common; 42 | 43 | ListNode *headB = new ListNode(5); 44 | headB->next = new ListNode(6); 45 | headB->next->next = new ListNode(1); 46 | headB->next->next->next = common; 47 | 48 | Solution solution; 49 | ListNode *intersection = solution.getIntersectionNode(headA, headB); 50 | 51 | if (intersection) 52 | { 53 | std::cout << "Intersection at node with value: " << intersection->val << std::endl; 54 | } 55 | else 56 | { 57 | std::cout << "No intersection." << std::endl; 58 | } 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /148/148_Brute.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct ListNode 4 | { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | ListNode *sortList(ListNode *head) 16 | { 17 | vector vec; 18 | ListNode *temp = head; 19 | while (temp != nullptr) 20 | { 21 | vec.emplace_back(temp->val); 22 | temp = temp->next; 23 | } 24 | sort(vec.begin(), vec.end()); 25 | temp = head; 26 | int i = 0; 27 | while (temp != nullptr && i < vec.size()) 28 | { 29 | temp->val = vec[i]; 30 | i++; 31 | temp = temp->next; 32 | } 33 | return head; 34 | } 35 | }; 36 | 37 | // Driver program 38 | int main() 39 | { 40 | // Create a sample linked list: 4 -> 2 -> 1 -> 3 41 | ListNode *head = new ListNode(4); 42 | head->next = new ListNode(2); 43 | head->next->next = new ListNode(1); 44 | head->next->next->next = new ListNode(3); 45 | 46 | Solution solution; 47 | ListNode *sortedHead = solution.sortList(head); 48 | 49 | // Print the sorted linked list 50 | ListNode *temp = sortedHead; 51 | while (temp != nullptr) 52 | { 53 | std::cout << temp->val << " "; 54 | temp = temp->next; 55 | } 56 | std::cout << std::endl; 57 | 58 | // Clean up the linked list 59 | while (sortedHead != nullptr) 60 | { 61 | ListNode *toDelete = sortedHead; 62 | sortedHead = sortedHead->next; 63 | delete toDelete; 64 | } 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /153/153_Find_minimum_in_rotated_sorted_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int findMin(vector &nums) 11 | { 12 | int n = nums.size(); 13 | int low = 0, high = n - 1, mid, min = 5001; 14 | while (low <= high) 15 | { 16 | mid = low + (high - low) / 2; 17 | if (nums[mid] < min) 18 | { 19 | min = nums[mid]; 20 | } 21 | if (nums[high] > nums[mid]) 22 | { 23 | high = mid - 1; 24 | } 25 | else 26 | { 27 | if (nums[low] < min) 28 | { 29 | min = nums[low]; 30 | } 31 | low = mid + 1; 32 | } 33 | } 34 | return min; 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | Solution solution; 41 | 42 | // Test case 1 43 | vector nums1 = {3, 4, 5, 1, 2}; 44 | cout << "Test case 1: Min element is " << solution.findMin(nums1) << endl; 45 | 46 | // Test case 2 47 | vector nums2 = {4, 5, 6, 7, 0, 1, 2}; 48 | cout << "Test case 2: Min element is " << solution.findMin(nums2) << endl; 49 | 50 | // Test case 3 51 | vector nums3 = {11, 13, 15, 17}; 52 | cout << "Test case 3: Min element is " << solution.findMin(nums3) << endl; 53 | 54 | // Test case 4 55 | vector nums4 = {2, 2, 2, 0, 1}; 56 | cout << "Test case 4: Min element is " << solution.findMin(nums4) << endl; 57 | 58 | // Test case 5 59 | vector nums5 = {1}; 60 | cout << "Test case 5: Min element is " << solution.findMin(nums5) << endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /141/141_Linked_list_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | bool hasCycle(ListNode *head) 16 | { 17 | if (head == NULL) 18 | return false; 19 | else if (head->next == NULL) 20 | return false; 21 | ListNode *tortoise = head->next; 22 | ListNode *hare = head->next->next; 23 | while (tortoise != NULL && hare != NULL) 24 | { 25 | if (tortoise == hare) 26 | return true; 27 | tortoise = tortoise->next; 28 | hare = hare->next; 29 | if (hare == NULL) 30 | return false; 31 | hare = hare->next; 32 | } 33 | return false; 34 | } 35 | }; 36 | 37 | // Driver program 38 | int main() 39 | { 40 | Solution sol; 41 | 42 | // Creating a linked list with a cycle: 1 -> 2 -> 3 -> 4 -> 5 -> 2 (cycle) 43 | ListNode *head = new ListNode(1); 44 | head->next = new ListNode(2); 45 | head->next->next = new ListNode(3); 46 | head->next->next->next = new ListNode(4); 47 | head->next->next->next->next = new ListNode(5); 48 | head->next->next->next->next->next = head->next; // Creating a cycle 49 | 50 | if (sol.hasCycle(head)) 51 | { 52 | cout << "Cycle detected" << endl; 53 | } 54 | else 55 | { 56 | cout << "No cycle detected" << endl; 57 | } 58 | 59 | // Free allocated memory (not required if there's a cycle) 60 | // Note: In a real scenario, you would need to break the cycle to free the nodes properly. 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /895_Own_Approach/895_Max_Freq_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class FreqStack { 5 | public: 6 | vector vec; 7 | unordered_map>> mpp; 8 | int i; 9 | int maxFreq; 10 | FreqStack() { 11 | i = 0; 12 | maxFreq = 0; 13 | } 14 | 15 | void push(int val) { 16 | vec.push_back(val); 17 | mpp[val].first++; 18 | if (maxFreq < mpp[val].first) { 19 | maxFreq = mpp[val].first; 20 | } 21 | mpp[val].second.push_back(i); 22 | i++; 23 | } 24 | 25 | int pop() { 26 | int temp = -1; 27 | int ans = -1; 28 | for (auto it : mpp) { 29 | if (it.second.first == maxFreq && it.second.second[it.second.second.size() - 1] > temp) { 30 | temp = it.second.second[it.second.second.size() - 1]; 31 | ans = it.first; 32 | } 33 | } 34 | mpp[ans].first--; 35 | mpp[ans].second.pop_back(); 36 | maxFreq = 0; 37 | for (auto it : mpp) { 38 | if (maxFreq < it.second.first) { 39 | maxFreq = it.second.first; 40 | } 41 | } 42 | 43 | auto rit = find(vec.rbegin(), vec.rend(), ans); 44 | if (rit != vec.rend()) { 45 | vec.erase(next(rit).base()); 46 | } 47 | 48 | return ans; 49 | } 50 | }; 51 | 52 | // Driver Program 53 | int main() { 54 | FreqStack fs; 55 | fs.push(5); 56 | fs.push(7); 57 | fs.push(5); 58 | fs.push(7); 59 | fs.push(4); 60 | fs.push(5); 61 | 62 | cout << fs.pop() << "\n"; // 5 63 | cout << fs.pop() << "\n"; // 7 64 | cout << fs.pop() << "\n"; // 5 65 | cout << fs.pop() << "\n"; // 4 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /236/236_Lowest_Common_Ancestor_in_a_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left, *right; 7 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | }; 9 | 10 | class Solution { 11 | public: 12 | bool DFS(TreeNode* root, TreeNode* target, vector& path) { 13 | if (!root) return false; 14 | path.push_back(root); 15 | if (root->val == target->val) return true; 16 | if (DFS(root->left, target, path) || DFS(root->right, target, path)) 17 | return true; 18 | path.pop_back(); 19 | return false; 20 | } 21 | 22 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 23 | vector pathOfP, pathOfQ; 24 | DFS(root, p, pathOfP); 25 | DFS(root, q, pathOfQ); 26 | TreeNode* lastCommon; 27 | for (int i = 0; i < min(pathOfP.size(), pathOfQ.size()); i++) { 28 | if (pathOfP[i]->val == pathOfQ[i]->val) { 29 | lastCommon = pathOfP[i]; 30 | } 31 | } 32 | return lastCommon; 33 | } 34 | }; 35 | 36 | // Driver program to test the solution 37 | int main() { 38 | TreeNode* root = new TreeNode(3); 39 | root->left = new TreeNode(5); 40 | root->right = new TreeNode(1); 41 | root->left->left = new TreeNode(6); 42 | root->left->right = new TreeNode(2); 43 | root->right->left = new TreeNode(0); 44 | root->right->right = new TreeNode(8); 45 | root->left->right->left = new TreeNode(7); 46 | root->left->right->right = new TreeNode(4); 47 | 48 | Solution sol; 49 | TreeNode* lca = sol.lowestCommonAncestor(root, root->left, root->left->right->right); 50 | cout << lca->val << endl; 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /875/875_Koko_Eating_bananas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | long long func(int mid, vector piles) 9 | { 10 | long long n = piles.size(); 11 | long long ans = 0; 12 | for (int i = 0; i < n; i++) 13 | { 14 | ans += ceil((double)piles[i] / (double)mid); 15 | } 16 | return ans; 17 | } 18 | 19 | int minEatingSpeed(vector &piles, int h) 20 | { 21 | long high = *max_element(piles.begin(), piles.end()), low = 1, mid, ans = 1; 22 | while (low <= high) 23 | { 24 | mid = low + (high - low) / 2; 25 | if (func(mid, piles) > h) 26 | { 27 | low = mid + 1; 28 | } 29 | else 30 | { 31 | ans = mid; 32 | high = mid - 1; 33 | } 34 | } 35 | return ans; 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | Solution solution; 42 | 43 | // Test case 1 44 | vector piles1 = {3, 6, 7, 11}; 45 | int h1 = 8; 46 | cout << "Test case 1: Minimum eating speed is " << solution.minEatingSpeed(piles1, h1) << endl; 47 | 48 | // Test case 2 49 | vector piles2 = {30, 11, 23, 4, 20}; 50 | int h2 = 5; 51 | cout << "Test case 2: Minimum eating speed is " << solution.minEatingSpeed(piles2, h2) << endl; 52 | 53 | // Test case 3 54 | vector piles3 = {30, 11, 23, 4, 20}; 55 | int h3 = 6; 56 | cout << "Test case 3: Minimum eating speed is " << solution.minEatingSpeed(piles3, h3) << endl; 57 | 58 | // Test case 4 59 | vector piles4 = {312884470}; 60 | int h4 = 968709470; 61 | cout << "Test case 4: Minimum eating speed is " << solution.minEatingSpeed(piles4, h4) << endl; 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /1283/1283_Find_the_smallest_divisor_given_a_threshold.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int divide(vector vec, int n, int div) 9 | { 10 | int sum = 0; 11 | for (int i = 0; i < n; i++) 12 | { 13 | sum += ceil((double)vec[i] / (double)div); 14 | } 15 | return sum; 16 | } 17 | 18 | int smallestDivisor(vector &nums, int threshold) 19 | { 20 | int low = 1, high = *max_element(nums.begin(), nums.end()), mid, n = nums.size(), ans = -1; 21 | while (low <= high) 22 | { 23 | mid = low + (high - low) / 2; 24 | if (divide(nums, n, mid) > threshold) 25 | { 26 | low = mid + 1; 27 | } 28 | else 29 | { 30 | ans = mid; 31 | high = mid - 1; 32 | } 33 | } 34 | return ans; 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | Solution solution; 41 | 42 | // Test case 1 43 | vector nums1 = {1, 2, 5, 9}; 44 | int threshold1 = 6; 45 | cout << "Test case 1: Smallest divisor is " << solution.smallestDivisor(nums1, threshold1) << endl; 46 | 47 | // Test case 2 48 | vector nums2 = {2, 3, 5, 7, 11}; 49 | int threshold2 = 11; 50 | cout << "Test case 2: Smallest divisor is " << solution.smallestDivisor(nums2, threshold2) << endl; 51 | 52 | // Test case 3 53 | vector nums3 = {19}; 54 | int threshold3 = 5; 55 | cout << "Test case 3: Smallest divisor is " << solution.smallestDivisor(nums3, threshold3) << endl; 56 | 57 | // Test case 4 58 | vector nums4 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 59 | int threshold4 = 10; 60 | cout << "Test case 4: Smallest divisor is " << solution.smallestDivisor(nums4, threshold4) << endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /162/162_Find_Peak_Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int findPeakElement(vector &nums) 10 | { 11 | int n = nums.size(); 12 | if (n == 1) 13 | return 0; 14 | if (nums[0] > nums[1]) 15 | return 0; 16 | if (nums[n - 1] > nums[n - 2]) 17 | return n - 1; 18 | 19 | int low = 1, high = n - 2, mid; 20 | while (low <= high) 21 | { 22 | mid = low + (high - low) / 2; 23 | if (nums[mid] > nums[mid + 1] && nums[mid] > nums[mid - 1]) 24 | { 25 | return mid; 26 | } 27 | else if (nums[mid] < nums[mid + 1]) 28 | { 29 | low = mid + 1; 30 | } 31 | else 32 | { 33 | high = mid - 1; 34 | } 35 | } 36 | return -1; 37 | } 38 | }; 39 | 40 | int main() 41 | { 42 | Solution solution; 43 | 44 | // Test case 1 45 | vector nums1 = {1, 2, 3, 1}; 46 | cout << "Test case 1: Peak element index is " << solution.findPeakElement(nums1) << endl; 47 | 48 | // Test case 2 49 | vector nums2 = {1, 2, 1, 3, 5, 6, 4}; 50 | cout << "Test case 2: Peak element index is " << solution.findPeakElement(nums2) << endl; 51 | 52 | // Test case 3 53 | vector nums3 = {1, 2}; 54 | cout << "Test case 3: Peak element index is " << solution.findPeakElement(nums3) << endl; 55 | 56 | // Test case 4 57 | vector nums4 = {3, 4, 3, 2, 1}; 58 | cout << "Test case 4: Peak element index is " << solution.findPeakElement(nums4) << endl; 59 | 60 | // Test case 5 61 | vector nums5 = {5, 6, 7, 8, 9}; 62 | cout << "Test case 5: Peak element index is " << solution.findPeakElement(nums5) << endl; 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /2095/2095_Delete_the_middle_node_of_a_LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode 5 | { 6 | 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *deleteMiddle(ListNode *head) 18 | { 19 | if (head == NULL || head->next == NULL) 20 | return nullptr; 21 | if (head->next->next == NULL) 22 | { 23 | head->next = NULL; 24 | return head; 25 | } 26 | 27 | ListNode *slow = head; 28 | ListNode *fast = head; 29 | while (fast->next->next != NULL && fast->next->next->next != NULL) 30 | { 31 | slow = slow->next; 32 | fast = fast->next->next; 33 | } 34 | 35 | ListNode *toDelete = slow->next; 36 | slow->next = slow->next->next; 37 | delete toDelete; 38 | return head; 39 | } 40 | }; 41 | 42 | // Driver program 43 | int main() 44 | { 45 | // Create a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 46 | ListNode *head = new ListNode(1); 47 | head->next = new ListNode(2); 48 | head->next->next = new ListNode(3); 49 | head->next->next->next = new ListNode(4); 50 | head->next->next->next->next = new ListNode(5); 51 | 52 | Solution solution; 53 | ListNode *newHead = solution.deleteMiddle(head); 54 | 55 | // Print the modified list 56 | ListNode *temp = newHead; 57 | while (temp != NULL) 58 | { 59 | std::cout << temp->val << " "; 60 | temp = temp->next; 61 | } 62 | std::cout << std::endl; 63 | 64 | // Clean up the linked list 65 | while (newHead != NULL) 66 | { 67 | ListNode *toDelete = newHead; 68 | newHead = newHead->next; 69 | delete toDelete; 70 | } 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /2807/2807_Insert_Greatest_Common_Divisors_in_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* Definition for singly-linked list. */ 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | int gcd(int a, int b) 18 | { 19 | if (a == 0) 20 | return b; 21 | if (b == 0) 22 | return a; 23 | if (a >= b) 24 | { 25 | return gcd(a % b, b); 26 | } 27 | return gcd(a, b % a); 28 | } 29 | 30 | ListNode *insertGreatestCommonDivisors(ListNode *head) 31 | { 32 | ListNode *temp = head; 33 | while (temp->next != nullptr) 34 | { 35 | ListNode *newnode = new ListNode(gcd(temp->val, temp->next->val)); 36 | newnode->next = temp->next; 37 | temp->next = newnode; 38 | temp = temp->next->next; 39 | } 40 | return head; 41 | } 42 | }; 43 | 44 | // Driver program to test the solution 45 | int main() 46 | { 47 | Solution solution; 48 | 49 | // Creating a sample linked list: 12 -> 15 -> 10 -> nullptr 50 | ListNode *head = new ListNode(12); 51 | head->next = new ListNode(15); 52 | head->next->next = new ListNode(10); 53 | 54 | cout << "Original list: "; 55 | ListNode *temp = head; 56 | while (temp != nullptr) 57 | { 58 | cout << temp->val << " -> "; 59 | temp = temp->next; 60 | } 61 | cout << "nullptr" << endl; 62 | 63 | head = solution.insertGreatestCommonDivisors(head); 64 | 65 | cout << "List after inserting GCDs: "; 66 | temp = head; 67 | while (temp != nullptr) 68 | { 69 | cout << temp->val << " -> "; 70 | temp = temp->next; 71 | } 72 | cout << "nullptr" << endl; 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /2/2_Two_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct ListNode 4 | { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) 16 | { 17 | ListNode *dummy = new ListNode(); 18 | ListNode *temp = dummy; 19 | int carry = 0; 20 | int sum = 0; 21 | 22 | while (l1 != nullptr || l2 != nullptr || carry != 0) 23 | { 24 | sum = carry; 25 | if (l1 != nullptr) 26 | { 27 | sum += l1->val; 28 | l1 = l1->next; 29 | } 30 | if (l2 != nullptr) 31 | { 32 | sum += l2->val; 33 | l2 = l2->next; 34 | } 35 | carry = sum / 10; 36 | sum = sum % 10; 37 | ListNode *newNode = new ListNode(sum); 38 | temp->next = newNode; 39 | temp = temp->next; 40 | } 41 | 42 | return dummy->next; 43 | } 44 | }; 45 | 46 | // Driver program 47 | int main() 48 | { 49 | // First linked list: 2 -> 4 -> 3 50 | ListNode *l1 = new ListNode(2); 51 | l1->next = new ListNode(4); 52 | l1->next->next = new ListNode(3); 53 | 54 | // Second linked list: 5 -> 6 -> 4 55 | ListNode *l2 = new ListNode(5); 56 | l2->next = new ListNode(6); 57 | l2->next->next = new ListNode(4); 58 | 59 | Solution solution; 60 | ListNode *result = solution.addTwoNumbers(l1, l2); 61 | 62 | // Print the result: Expected output is 7 -> 0 -> 8 63 | std::cout << "Result: "; 64 | while (result != nullptr) 65 | { 66 | std::cout << result->val; 67 | if (result->next != nullptr) 68 | std::cout << " -> "; 69 | result = result->next; 70 | } 71 | std::cout << std::endl; 72 | return 0; 73 | } -------------------------------------------------------------------------------- /232/232_Implement_queue_using_stacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class MyQueue { 6 | public: 7 | stack st; 8 | stack temp; 9 | 10 | MyQueue() {} 11 | 12 | void push(int x) { 13 | st.push(x); 14 | } 15 | 16 | int pop() { 17 | if (st.empty()) return -1; 18 | 19 | while (!st.empty()) { 20 | temp.push(st.top()); 21 | st.pop(); 22 | } 23 | 24 | int ans = temp.top(); 25 | temp.pop(); 26 | 27 | while (!temp.empty()) { 28 | st.push(temp.top()); 29 | temp.pop(); 30 | } 31 | 32 | return ans; 33 | } 34 | 35 | int peek() { 36 | if (st.empty()) return -1; 37 | 38 | while (!st.empty()) { 39 | temp.push(st.top()); 40 | st.pop(); 41 | } 42 | 43 | int ans = temp.top(); 44 | 45 | while (!temp.empty()) { 46 | st.push(temp.top()); 47 | temp.pop(); 48 | } 49 | 50 | return ans; 51 | } 52 | 53 | bool empty() { 54 | return st.empty(); 55 | } 56 | }; 57 | 58 | // Driver Program 59 | int main() { 60 | MyQueue* obj = new MyQueue(); 61 | 62 | // Pushing elements into the queue 63 | obj->push(1); 64 | obj->push(2); 65 | obj->push(3); 66 | 67 | cout << "Front element (peek): " << obj->peek() << endl; // Should print 1 68 | 69 | cout << "Popped: " << obj->pop() << endl; // Should print 1 70 | cout << "Popped: " << obj->pop() << endl; // Should print 2 71 | 72 | cout << "Front element (peek): " << obj->peek() << endl; // Should print 3 73 | 74 | cout << "Queue empty? " << (obj->empty() ? "Yes" : "No") << endl; // Should print No 75 | 76 | cout << "Popped: " << obj->pop() << endl; // Should print 3 77 | cout << "Queue empty? " << (obj->empty() ? "Yes" : "No") << endl; // Should print Yes 78 | 79 | delete obj; 80 | return 0; 81 | } -------------------------------------------------------------------------------- /540/540_Single_element_in_sorted_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int singleNonDuplicate(vector &nums) 11 | { 12 | int n = nums.size(); 13 | int low = 0, high = n - 1, mid; 14 | if (n == 1) 15 | return nums[0]; 16 | if (nums[0] != nums[1]) 17 | return nums[0]; 18 | else if (nums[n - 2] != nums[n - 1]) 19 | return nums[n - 1]; 20 | while (low <= high) 21 | { 22 | mid = low + (high - low) / 2; 23 | if (nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) 24 | return nums[mid]; 25 | else if ((mid % 2 == 0 && nums[mid + 1] == nums[mid]) || 26 | (mid % 2 == 1 && nums[mid - 1] == nums[mid])) 27 | { 28 | low = mid + 1; 29 | } 30 | else 31 | { 32 | high = mid - 1; 33 | } 34 | } 35 | return -1; 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | Solution solution; 42 | 43 | // Test case 1 44 | vector nums1 = {1, 1, 2, 3, 3, 4, 4, 8, 8}; 45 | cout << "Test case 1: Single non-duplicate element is " << solution.singleNonDuplicate(nums1) << endl; 46 | 47 | // Test case 2 48 | vector nums2 = {3, 3, 7, 7, 10, 11, 11}; 49 | cout << "Test case 2: Single non-duplicate element is " << solution.singleNonDuplicate(nums2) << endl; 50 | 51 | // Test case 3 52 | vector nums3 = {1, 1, 2}; 53 | cout << "Test case 3: Single non-duplicate element is " << solution.singleNonDuplicate(nums3) << endl; 54 | 55 | // Test case 4 56 | vector nums4 = {1, 2, 2}; 57 | cout << "Test case 4: Single non-duplicate element is " << solution.singleNonDuplicate(nums4) << endl; 58 | 59 | // Test case 5 60 | vector nums5 = {1}; 61 | cout << "Test case 5: Single non-duplicate element is " << solution.singleNonDuplicate(nums5) << endl; 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /19/19_remove_nth_node_from_end_of_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct ListNode 4 | { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | ListNode *removeNthFromEnd(ListNode *head, int n) 16 | { 17 | if (head == NULL) 18 | return NULL; 19 | ListNode *dummy = new ListNode(0, head); 20 | ListNode *first = dummy; 21 | ListNode *second = dummy; 22 | 23 | for (int i = 0; i <= n; i++) 24 | { 25 | first = first->next; 26 | } 27 | 28 | while (first != NULL) 29 | { 30 | first = first->next; 31 | second = second->next; 32 | } 33 | 34 | ListNode *toDelete = second->next; 35 | second->next = second->next->next; 36 | delete toDelete; 37 | 38 | ListNode *result = dummy->next; 39 | delete dummy; // Clean up dummy node 40 | return result; 41 | } 42 | }; 43 | 44 | // Driver program 45 | int main() 46 | { 47 | // Create a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 48 | ListNode *head = new ListNode(1); 49 | head->next = new ListNode(2); 50 | head->next->next = new ListNode(3); 51 | head->next->next->next = new ListNode(4); 52 | head->next->next->next->next = new ListNode(5); 53 | 54 | Solution solution; 55 | int n = 2; // Remove the 2nd node from the end 56 | ListNode *newHead = solution.removeNthFromEnd(head, n); 57 | 58 | // Print the modified list 59 | ListNode *temp = newHead; 60 | while (temp != NULL) 61 | { 62 | std::cout << temp->val << " "; 63 | temp = temp->next; 64 | } 65 | std::cout << std::endl; 66 | 67 | // Clean up the linked list 68 | while (newHead != NULL) 69 | { 70 | ListNode *toDelete = newHead; 71 | newHead = newHead->next; 72 | delete toDelete; 73 | } 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/README.md: -------------------------------------------------------------------------------- 1 |

94. Binary Tree Inorder Traversal

Easy


Given the root of a binary tree, return the inorder traversal of its nodes' values.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
7 |

Input: root = [1,null,2,3]

8 | 9 |

Output: [1,3,2]

10 | 11 |

Explanation:

12 | 13 |

14 |
15 | 16 |

Example 2:

17 | 18 |
19 |

Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]

20 | 21 |

Output: [4,2,6,5,7,1,3,9,8]

22 | 23 |

Explanation:

24 | 25 |

26 |
27 | 28 |

Example 3:

29 | 30 |
31 |

Input: root = []

32 | 33 |

Output: []

34 |
35 | 36 |

Example 4:

37 | 38 |
39 |

Input: root = [1]

40 | 41 |

Output: [1]

42 |
43 | 44 |

 

45 |

Constraints:

46 | 47 |
    48 |
  • The number of nodes in the tree is in the range [0, 100].
  • 49 |
  • -100 <= Node.val <= 100
  • 50 |
51 | 52 |

 

53 | Follow up: Recursive solution is trivial, could you do it iteratively? -------------------------------------------------------------------------------- /19/19_optimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct ListNode 4 | { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | ListNode *removeNthFromEnd(ListNode *head, int n) 16 | { 17 | if (head == NULL) 18 | return nullptr; 19 | if (head->next == NULL && n == 1) 20 | return nullptr; 21 | 22 | ListNode *fast = head; 23 | while (n--) 24 | { 25 | fast = fast->next; 26 | if (fast == NULL) 27 | { 28 | head = head->next; 29 | return head; 30 | } 31 | } 32 | 33 | ListNode *slow = head; 34 | while (fast->next != NULL) 35 | { 36 | slow = slow->next; 37 | fast = fast->next; 38 | } 39 | 40 | ListNode *nodeToDelete = slow->next; 41 | slow->next = slow->next->next; 42 | delete nodeToDelete; 43 | return head; 44 | } 45 | }; 46 | 47 | // Driver program 48 | int main() 49 | { 50 | // Create a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 51 | ListNode *head = new ListNode(1); 52 | head->next = new ListNode(2); 53 | head->next->next = new ListNode(3); 54 | head->next->next->next = new ListNode(4); 55 | head->next->next->next->next = new ListNode(5); 56 | 57 | Solution solution; 58 | int n = 2; // Remove the 2nd node from the end 59 | ListNode *newHead = solution.removeNthFromEnd(head, n); 60 | 61 | // Print the modified list 62 | ListNode *temp = newHead; 63 | while (temp != NULL) 64 | { 65 | std::cout << temp->val << " "; 66 | temp = temp->next; 67 | } 68 | std::cout << std::endl; 69 | 70 | // Clean up the linked list 71 | while (newHead != NULL) 72 | { 73 | ListNode *toDelete = newHead; 74 | newHead = newHead->next; 75 | delete toDelete; 76 | } 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /61/Rotate_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct ListNode 4 | { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | ListNode *rotateRight(ListNode *head, int k) 16 | { 17 | if (head == nullptr || head->next == nullptr) 18 | return head; 19 | 20 | ListNode *temp = head; 21 | int cnt = 1; 22 | 23 | // Count the total number of nodes 24 | while (temp->next != nullptr) 25 | { 26 | temp = temp->next; 27 | cnt++; 28 | } 29 | 30 | // Reduce k to within bounds of list length 31 | k = (k >= cnt) ? k % cnt : k; 32 | if (k == 0) 33 | return head; 34 | 35 | temp = head; 36 | cnt = cnt - k - 1; 37 | 38 | // Move to the node where rotation will happen 39 | while (cnt--) 40 | { 41 | temp = temp->next; 42 | } 43 | 44 | ListNode *nextnode = temp->next; 45 | temp->next = nullptr; // Break the link 46 | 47 | ListNode *newhead = nextnode; 48 | 49 | // Move to the end of the rotated part 50 | while (nextnode->next != nullptr) 51 | { 52 | nextnode = nextnode->next; 53 | } 54 | 55 | nextnode->next = head; // Link the rotated part to the original head 56 | head = newhead; 57 | 58 | return head; 59 | } 60 | }; 61 | 62 | // Driver program to test above 63 | int main() 64 | { 65 | ListNode *head = new ListNode(1); 66 | head->next = new ListNode(2); 67 | head->next->next = new ListNode(3); 68 | head->next->next->next = new ListNode(4); 69 | head->next->next->next->next = new ListNode(5); 70 | 71 | Solution solution; 72 | int k = 2; 73 | ListNode *result = solution.rotateRight(head, k); 74 | 75 | while (result != nullptr) 76 | { 77 | cout << result->val << " "; 78 | result = result->next; 79 | } 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /410/410_Split_array_largest_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int solve(vector nums, int n, int limit) 9 | { 10 | int sum = 0; 11 | int parts = 1; 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (sum + nums[i] <= limit) 15 | { 16 | sum += nums[i]; 17 | } 18 | else 19 | { 20 | parts++; 21 | sum = nums[i]; 22 | } 23 | } 24 | return parts; 25 | } 26 | 27 | int splitArray(vector &nums, int k) 28 | { 29 | if (k > nums.size()) 30 | return -1; 31 | int low = *max_element(nums.begin(), nums.end()); 32 | int high = accumulate(nums.begin(), nums.end(), 0); 33 | int n = nums.size(); 34 | while (low <= high) 35 | { 36 | int mid = low + (high - low) / 2; 37 | int parts = solve(nums, n, mid); 38 | if (parts > k) 39 | { 40 | low = mid + 1; 41 | } 42 | else 43 | { 44 | high = mid - 1; 45 | } 46 | } 47 | return low; 48 | } 49 | }; 50 | 51 | int main() 52 | { 53 | Solution solution; 54 | 55 | // Test case 1 56 | vector nums1 = {7, 2, 5, 10, 8}; 57 | int k1 = 2; 58 | cout << "Test case 1: Minimum largest sum after splitting = " << solution.splitArray(nums1, k1) << endl; 59 | 60 | // Test case 2 61 | vector nums2 = {1, 2, 3, 4, 5}; 62 | int k2 = 2; 63 | cout << "Test case 2: Minimum largest sum after splitting = " << solution.splitArray(nums2, k2) << endl; 64 | 65 | // Test case 3 66 | vector nums3 = {1, 4, 4}; 67 | int k3 = 3; 68 | cout << "Test case 3: Minimum largest sum after splitting = " << solution.splitArray(nums3, k3) << endl; 69 | 70 | // Test case 4 71 | vector nums4 = {2, 3, 1, 2, 4, 3}; 72 | int k4 = 4; 73 | cout << "Test case 4: Minimum largest sum after splitting = " << solution.splitArray(nums4, k4) << endl; 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /34/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | vector searchRange(vector &nums, int target) 8 | { 9 | vector ans(2, -1); 10 | int n = nums.size(); 11 | if (n == 0 || target < nums[0] || target > nums[n - 1] || 12 | *lower_bound(nums.begin(), nums.end(), target) != target || 13 | nums[upper_bound(nums.begin(), nums.end(), target) - nums.begin() - 1] != target) 14 | { 15 | return ans; 16 | } 17 | ans[0] = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); 18 | ans[1] = upper_bound(nums.begin(), nums.end(), target) - nums.begin() - 1; 19 | return ans; 20 | } 21 | }; 22 | 23 | // Driver program to test the Solution class 24 | int main() 25 | { 26 | // Create an instance of the Solution class 27 | Solution solution; 28 | 29 | // Test case 1 30 | vector nums1 = {5, 7, 7, 8, 8, 10}; 31 | int target1 = 8; 32 | vector result1 = solution.searchRange(nums1, target1); 33 | cout << "Test case 1: "; 34 | cout << "Target: " << target1 << " -> Indices: [" << result1[0] << ", " << result1[1] << "]" << endl; 35 | 36 | // Test case 2 37 | vector nums2 = {5, 7, 7, 8, 8, 10}; 38 | int target2 = 6; 39 | vector result2 = solution.searchRange(nums2, target2); 40 | cout << "Test case 2: "; 41 | cout << "Target: " << target2 << " -> Indices: [" << result2[0] << ", " << result2[1] << "]" << endl; 42 | 43 | // Test case 3 44 | vector nums3 = {}; 45 | int target3 = 0; 46 | vector result3 = solution.searchRange(nums3, target3); 47 | cout << "Test case 3: "; 48 | cout << "Target: " << target3 << " -> Indices: [" << result3[0] << ", " << result3[1] << "]" << endl; 49 | 50 | // Test case 4 51 | vector nums4 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 52 | int target4 = 7; 53 | vector result4 = solution.searchRange(nums4, target4); 54 | cout << "Test case 4: "; 55 | cout << "Target: " << target4 << " -> Indices: [" << result4[0] << ", " << result4[1] << "]" << endl; 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /1482/1482_Minimum_number_of_days_to_make_m_bouquets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | bool solve(vector vec, int m, int k, int n, int bloomlimit) 11 | { 12 | int count = 0, bouquets = 0; 13 | for (int i = 0; i < n; i++) 14 | { 15 | if (vec[i] <= bloomlimit) 16 | { 17 | count++; 18 | } 19 | else 20 | { 21 | count = 0; 22 | } 23 | if (count == k) 24 | { 25 | bouquets++; 26 | count = 0; 27 | } 28 | } 29 | return bouquets >= m; 30 | } 31 | 32 | int minDays(vector &bloomDay, int m, int k) 33 | { 34 | int low = *min_element(bloomDay.begin(), bloomDay.end()); 35 | int high = *max_element(bloomDay.begin(), bloomDay.end()); 36 | int mid, ans = -1; 37 | int n = bloomDay.size(); 38 | long long y = m, z = k; 39 | if ((y * z) > n) 40 | return -1; 41 | while (low <= high) 42 | { 43 | mid = low + (high - low) / 2; 44 | if (solve(bloomDay, m, k, n, mid)) 45 | { 46 | ans = mid; 47 | high = mid - 1; 48 | } 49 | else 50 | { 51 | low = mid + 1; 52 | } 53 | } 54 | return ans; 55 | } 56 | }; 57 | 58 | int main() 59 | { 60 | Solution solution; 61 | 62 | // Test case 1 63 | vector bloomDay1 = {1, 10, 3, 10, 2}; 64 | int m1 = 3, k1 = 1; 65 | cout << "Test case 1: Minimum number of days = " << solution.minDays(bloomDay1, m1, k1) << endl; 66 | 67 | // Test case 2 68 | vector bloomDay2 = {1, 10, 3, 10, 2}; 69 | int m2 = 3, k2 = 2; 70 | cout << "Test case 2: Minimum number of days = " << solution.minDays(bloomDay2, m2, k2) << endl; 71 | 72 | // Test case 3 73 | vector bloomDay3 = {7, 7, 7, 7, 12, 7, 7}; 74 | int m3 = 2, k3 = 3; 75 | cout << "Test case 3: Minimum number of days = " << solution.minDays(bloomDay3, m3, k3) << endl; 76 | 77 | // Test case 4 78 | vector bloomDay4 = {1000000000, 1000000000}; 79 | int m4 = 1, k4 = 1; 80 | cout << "Test case 4: Minimum number of days = " << solution.minDays(bloomDay4, m4, k4) << endl; 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /1011/1011_Capacity_to_ship_packages_within_D_days.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | bool calculate(vector weights, int days, int capacity, int n) 9 | { 10 | int actualDays = 0, counter = 0; 11 | for (int i = 0; i < n; i++) 12 | { 13 | counter += weights[i]; 14 | if (counter > capacity) 15 | { 16 | actualDays++; 17 | counter = weights[i]; 18 | } 19 | else if (counter == capacity) 20 | { 21 | actualDays++; 22 | counter = 0; 23 | } 24 | if (i == n - 1 && counter > 0) 25 | { 26 | actualDays++; 27 | } 28 | } 29 | return actualDays <= days; 30 | } 31 | 32 | int shipWithinDays(vector &weights, int days) 33 | { 34 | int n = weights.size(); 35 | int low = *max_element(weights.begin(), weights.end()); 36 | int high = accumulate(weights.begin(), weights.end(), 0); 37 | int mid, ans = -1; 38 | 39 | while (low <= high) 40 | { 41 | mid = low + (high - low) / 2; 42 | if (!calculate(weights, days, mid, n)) 43 | { 44 | low = mid + 1; 45 | } 46 | else 47 | { 48 | ans = mid; 49 | high = mid - 1; 50 | } 51 | } 52 | 53 | return ans; 54 | } 55 | }; 56 | 57 | int main() 58 | { 59 | Solution solution; 60 | 61 | // Test case 1 62 | vector weights1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 63 | int days1 = 5; 64 | cout << "Test case 1: Minimum ship capacity = " << solution.shipWithinDays(weights1, days1) << endl; 65 | 66 | // Test case 2 67 | vector weights2 = {3, 2, 2, 4, 1, 4}; 68 | int days2 = 3; 69 | cout << "Test case 2: Minimum ship capacity = " << solution.shipWithinDays(weights2, days2) << endl; 70 | 71 | // Test case 3 72 | vector weights3 = {1, 2, 3, 1, 1}; 73 | int days3 = 4; 74 | cout << "Test case 3: Minimum ship capacity = " << solution.shipWithinDays(weights3, days3) << endl; 75 | 76 | // Test case 4 77 | vector weights4 = {10, 50, 100, 100, 50, 10}; 78 | int days4 = 5; 79 | cout << "Test case 4: Minimum ship capacity = " << solution.shipWithinDays(weights4, days4) << endl; 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /81/81_Search_in_rotated_sorted_array_with_dup_II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | bool search(vector &nums, int target) 11 | { 12 | int n = nums.size(), start = 0, end = n - 1, mid; 13 | while (start <= end) 14 | { 15 | mid = (start + end) / 2; 16 | if (nums[mid] == target) 17 | return true; 18 | else if (nums[mid] == nums[start] && nums[mid] == nums[end]) 19 | { 20 | start++; 21 | end--; 22 | continue; 23 | } 24 | else if (nums[mid] > nums[end]) 25 | { 26 | if (target < nums[mid] && target >= nums[start]) 27 | { 28 | end = mid - 1; 29 | } 30 | else 31 | { 32 | start = mid + 1; 33 | } 34 | } 35 | else 36 | { 37 | if (target > nums[mid] && target <= nums[end]) 38 | { 39 | start = mid + 1; 40 | } 41 | else 42 | { 43 | end = mid - 1; 44 | } 45 | } 46 | } 47 | return false; 48 | } 49 | }; 50 | 51 | int main() 52 | { 53 | Solution solution; 54 | 55 | // Test case 1 56 | vector nums1 = {2, 5, 6, 0, 0, 1, 2}; 57 | int target1 = 0; 58 | cout << "Test case 1: " << (solution.search(nums1, target1) ? "Found" : "Not Found") << endl; 59 | 60 | // Test case 2 61 | vector nums2 = {1, 3, 5, 7, 9, 11, 13}; 62 | int target2 = 7; 63 | cout << "Test case 2: " << (solution.search(nums2, target2) ? "Found" : "Not Found") << endl; 64 | 65 | // Test case 3 66 | vector nums3 = {1, 1, 1, 1, 1}; 67 | int target3 = 2; 68 | cout << "Test case 3: " << (solution.search(nums3, target3) ? "Found" : "Not Found") << endl; 69 | 70 | // Test case 4 71 | vector nums4 = {3, 1, 1, 1, 1}; 72 | int target4 = 3; 73 | cout << "Test case 4: " << (solution.search(nums4, target4) ? "Found" : "Not Found") << endl; 74 | 75 | // Test case 5 76 | vector nums5 = {1, 2, 3, 4, 5}; 77 | int target5 = 6; 78 | cout << "Test case 5: " << (solution.search(nums5, target5) ? "Found" : "Not Found") << endl; 79 | 80 | return 0; 81 | } -------------------------------------------------------------------------------- /25/25_reverse_Nodes_in_k-group.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct ListNode 4 | { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution 13 | { 14 | public: 15 | void reverse(ListNode *start) 16 | { 17 | ListNode *back = NULL; 18 | ListNode *temp = start; 19 | ListNode *nextn = temp->next; 20 | while (temp != NULL) 21 | { 22 | temp->next = back; 23 | back = temp; 24 | temp = nextn; 25 | if (temp != NULL) 26 | nextn = temp->next; 27 | } 28 | } 29 | 30 | ListNode *reverseKGroup(ListNode *head, int k) 31 | { 32 | if (head == nullptr || head->next == nullptr) 33 | return head; 34 | ListNode *temp = head; 35 | ListNode *kthnode = head; 36 | ListNode *previousEndnode = nullptr; 37 | 38 | for (int i = 0; i < k - 1; i++) 39 | { 40 | if (kthnode->next == nullptr) 41 | return head; 42 | kthnode = kthnode->next; 43 | } 44 | 45 | ListNode *nextnode = kthnode->next; 46 | while (kthnode != nullptr) 47 | { 48 | kthnode->next = nullptr; 49 | reverse(temp); 50 | 51 | if (previousEndnode != nullptr) 52 | previousEndnode->next = kthnode; 53 | if (temp == head) 54 | head = kthnode; 55 | 56 | previousEndnode = temp; 57 | temp->next = nextnode; 58 | temp = nextnode; 59 | kthnode = temp; 60 | 61 | for (int i = 0; i < k - 1 && kthnode != nullptr; i++) 62 | kthnode = kthnode->next; 63 | 64 | if (kthnode != nullptr) 65 | nextnode = kthnode->next; 66 | } 67 | return head; 68 | } 69 | }; 70 | 71 | // Driver program to test above 72 | int main() 73 | { 74 | ListNode *head = new ListNode(1); 75 | head->next = new ListNode(2); 76 | head->next->next = new ListNode(3); 77 | head->next->next->next = new ListNode(4); 78 | head->next->next->next->next = new ListNode(5); 79 | 80 | Solution solution; 81 | int k = 2; 82 | ListNode *result = solution.reverseKGroup(head, k); 83 | 84 | while (result != nullptr) 85 | { 86 | cout << result->val << " "; 87 | result = result->next; 88 | } 89 | return 0; 90 | } -------------------------------------------------------------------------------- /138/138_Copy_List_with_random_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Node 7 | { 8 | public: 9 | int val; 10 | Node *next; 11 | Node *random; 12 | 13 | Node(int _val) 14 | { 15 | val = _val; 16 | next = nullptr; 17 | random = nullptr; 18 | } 19 | }; 20 | 21 | class Solution 22 | { 23 | public: 24 | Node *copyRandomList(Node *head) 25 | { 26 | if (head == nullptr) 27 | return nullptr; 28 | 29 | Node *tempog = head; 30 | Node *dummy = new Node(-1); 31 | Node *tempcpy = dummy; 32 | unordered_map mpp; 33 | 34 | // Create deep copies of the nodes without handling random pointers 35 | while (tempog != nullptr) 36 | { 37 | Node *newnode = new Node(tempog->val); 38 | tempcpy->next = newnode; 39 | tempcpy = tempcpy->next; 40 | mpp[tempog] = tempcpy; 41 | tempog = tempog->next; 42 | } 43 | 44 | // Reset pointers to the beginning 45 | tempog = head; 46 | tempcpy = dummy->next; 47 | 48 | // Assign random pointers in the copied list 49 | while (tempog != nullptr && tempcpy != nullptr) 50 | { 51 | tempcpy->random = mpp[tempog->random]; 52 | tempcpy = tempcpy->next; 53 | tempog = tempog->next; 54 | } 55 | 56 | return dummy->next; 57 | } 58 | }; 59 | 60 | // Driver program to test the solution 61 | int main() 62 | { 63 | Node *head = new Node(1); 64 | Node *second = new Node(2); 65 | Node *third = new Node(3); 66 | 67 | head->next = second; 68 | second->next = third; 69 | 70 | // Setting up random pointers 71 | head->random = third; // Random of 1 points to 3 72 | second->random = head; // Random of 2 points to 1 73 | third->random = second; // Random of 3 points to 2 74 | 75 | Solution solution; 76 | Node *copiedList = solution.copyRandomList(head); 77 | 78 | // Output copied list values and random pointers 79 | Node *temp = copiedList; 80 | while (temp != nullptr) 81 | { 82 | cout << "Node Val: " << temp->val; 83 | if (temp->random != nullptr) 84 | { 85 | cout << ", Random points to: " << temp->random->val << endl; 86 | } 87 | else 88 | { 89 | cout << ", No random pointer" << endl; 90 | } 91 | temp = temp->next; 92 | } 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /234/234_Palindrone_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for singly-linked list. 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode() : val(0), next(nullptr) {} 10 | ListNode(int x) : val(x), next(nullptr) {} 11 | ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *reverse(ListNode *ptr) 18 | { 19 | ListNode *back = NULL; 20 | ListNode *current = ptr; 21 | ListNode *front = NULL; 22 | if (current != NULL) 23 | { 24 | front = ptr->next; 25 | } 26 | 27 | while (current != NULL) 28 | { 29 | current->next = back; 30 | back = current; 31 | current = front; 32 | if (front != NULL) 33 | { 34 | front = front->next; 35 | } 36 | } 37 | return back; 38 | } 39 | 40 | bool isPalindrome(ListNode *head) 41 | { 42 | if (head == NULL || head->next == NULL) 43 | return true; 44 | 45 | // Find the middle of the list 46 | ListNode *slow = head; 47 | ListNode *fast = head; 48 | while (fast != NULL && fast->next != NULL) 49 | { 50 | slow = slow->next; 51 | fast = fast->next->next; 52 | } 53 | 54 | // Reverse the second half of the list 55 | ListNode *newhead = reverse(slow); 56 | ListNode *temphead = newhead; 57 | 58 | // Compare the two halves 59 | ListNode *oldhead = head; 60 | while (temphead != NULL) 61 | { 62 | if (temphead->val != oldhead->val) 63 | { 64 | reverse(newhead); // Revert the list back 65 | return false; 66 | } 67 | oldhead = oldhead->next; 68 | temphead = temphead->next; 69 | } 70 | 71 | // Revert the second half back to original 72 | reverse(newhead); 73 | return true; 74 | } 75 | }; 76 | 77 | // Driver program 78 | int main() 79 | { 80 | Solution sol; 81 | 82 | // Create a linked list: 1 -> 2 -> 2 -> 1 83 | ListNode *head = new ListNode(1); 84 | head->next = new ListNode(2); 85 | head->next->next = new ListNode(2); 86 | head->next->next->next = new ListNode(1); 87 | 88 | // Check if the linked list is a palindrome 89 | if (sol.isPalindrome(head)) 90 | { 91 | cout << "The linked list is a palindrome." << endl; 92 | } 93 | else 94 | { 95 | cout << "The linked list is not a palindrome." << endl; 96 | } 97 | 98 | return 0; 99 | } -------------------------------------------------------------------------------- /4/4_Median_of_two_sorted_arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | double findMedianSortedArrays(vector &nums1, vector &nums2) 8 | { 9 | int n1 = nums1.size(); 10 | int n2 = nums2.size(); 11 | int n = n1 + n2; 12 | int mid = (n / 2); 13 | int count = 0; 14 | int previous = mid - 1; 15 | int ptr1 = 0, ptr2 = 0, midElement = -1, previousElement = -1; 16 | while (ptr1 < n1 && ptr2 < n2) 17 | { 18 | if (nums1[ptr1] <= nums2[ptr2]) 19 | { 20 | if (count == previous) 21 | { 22 | previousElement = nums1[ptr1]; 23 | } 24 | else if (count == mid) 25 | { 26 | midElement = nums1[ptr1]; 27 | } 28 | ptr1++; 29 | count++; 30 | } 31 | else if (nums1[ptr1] > nums2[ptr2]) 32 | { 33 | if (count == previous) 34 | { 35 | previousElement = nums2[ptr2]; 36 | } 37 | else if (count == mid) 38 | { 39 | midElement = nums2[ptr2]; 40 | } 41 | ptr2++; 42 | count++; 43 | } 44 | } 45 | while (ptr1 < n1) 46 | { 47 | if (count == previous) 48 | { 49 | previousElement = nums1[ptr1]; 50 | } 51 | else if (count == mid) 52 | { 53 | midElement = nums1[ptr1]; 54 | } 55 | ptr1++; 56 | count++; 57 | } 58 | while (ptr2 < n2) 59 | { 60 | if (count == previous) 61 | { 62 | previousElement = nums2[ptr2]; 63 | } 64 | else if (count == mid) 65 | { 66 | midElement = nums2[ptr2]; 67 | } 68 | ptr2++; 69 | count++; 70 | } 71 | if (n % 2 == 0) 72 | { 73 | return ((double)midElement + (double)previousElement) / 2.00000; 74 | } 75 | else 76 | { 77 | return midElement; 78 | } 79 | } 80 | }; 81 | 82 | // Driver code 83 | int main() 84 | { 85 | Solution sol; 86 | 87 | vector nums1 = {1, 3}; 88 | vector nums2 = {2}; 89 | cout << "Median is: " << sol.findMedianSortedArrays(nums1, nums2) << endl; 90 | 91 | nums1 = {1, 2}; 92 | nums2 = {3, 4}; 93 | cout << "Median is: " << sol.findMedianSortedArrays(nums1, nums2) << endl; 94 | 95 | return 0; 96 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | LeetCode Logo 3 |

4 | 5 | # LeetMaster's Code Lair 🚀 6 | 7 | Welcome to my Code Lair! Here you'll find a collection of my conquests over the challenging LeetCode problems. Each solution is a tale of wit and logic, crafted in Python and C++. 8 | 9 | ## Table of Contents 10 | 11 | - [About](#about) 12 | - [Getting Started](#getting-started) 13 | 14 | ## About 15 | 16 | In this repository, I've endeavored to document my journey through LeetCode, sharing not just the solutions but the thought process behind them. Each problem is a puzzle, and my solutions are the keys to unlock them. 17 | 18 | ## Getting Started 19 | 20 | To delve into this treasure trove of code, simply clone this repository to your local machine. Feel free to explore, learn, and adapt these solutions to your own adventures! 21 | 22 | ```bash 23 | git clone https://github.com/VatsalyaGautam/leetcode.git 24 | 25 | 26 | 27 | # LeetCode Topics 28 | ## Stack 29 | | | 30 | | ------- | 31 | | [0094-binary-tree-inorder-traversal](https://github.com/VatsalyaGautam/Leetcode/tree/master/0094-binary-tree-inorder-traversal) | 32 | ## Tree 33 | | | 34 | | ------- | 35 | | [0094-binary-tree-inorder-traversal](https://github.com/VatsalyaGautam/Leetcode/tree/master/0094-binary-tree-inorder-traversal) | 36 | | [0104-maximum-depth-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0104-maximum-depth-of-binary-tree) | 37 | | [0543-diameter-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0543-diameter-of-binary-tree) | 38 | ## Depth-First Search 39 | | | 40 | | ------- | 41 | | [0094-binary-tree-inorder-traversal](https://github.com/VatsalyaGautam/Leetcode/tree/master/0094-binary-tree-inorder-traversal) | 42 | | [0104-maximum-depth-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0104-maximum-depth-of-binary-tree) | 43 | | [0543-diameter-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0543-diameter-of-binary-tree) | 44 | ## Binary Tree 45 | | | 46 | | ------- | 47 | | [0094-binary-tree-inorder-traversal](https://github.com/VatsalyaGautam/Leetcode/tree/master/0094-binary-tree-inorder-traversal) | 48 | | [0104-maximum-depth-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0104-maximum-depth-of-binary-tree) | 49 | | [0543-diameter-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0543-diameter-of-binary-tree) | 50 | ## Breadth-First Search 51 | | | 52 | | ------- | 53 | | [0104-maximum-depth-of-binary-tree](https://github.com/VatsalyaGautam/Leetcode/tree/master/0104-maximum-depth-of-binary-tree) | 54 | -------------------------------------------------------------------------------- /33/33_Search_in_Rotated_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Solution 5 | { 6 | public: 7 | int binary_search(vector &nums, int low, int high, int target) 8 | { 9 | while (low <= high) 10 | { 11 | int mid = low + high; 12 | mid /= 2; 13 | if (target == nums[mid]) 14 | { 15 | return mid; 16 | } 17 | else if (target < nums[mid]) 18 | { 19 | high = mid - 1; 20 | } 21 | else 22 | { 23 | low = mid + 1; 24 | } 25 | } 26 | return -1; 27 | } 28 | 29 | int search(vector &nums, int target) 30 | { 31 | int n = nums.size(); 32 | int high = n - 1, low = 0, mid; 33 | while (low <= high) 34 | { 35 | mid = low + high; 36 | mid /= 2; 37 | if (nums[mid] == target) 38 | return mid; 39 | if (nums[low] > nums[mid]) 40 | { 41 | if (nums[mid] < target && nums[high] >= target) 42 | { 43 | return binary_search(nums, mid + 1, high, target); 44 | } 45 | else 46 | { 47 | high = mid - 1; 48 | } 49 | } 50 | else if (nums[high] < nums[mid]) 51 | { 52 | if (target >= nums[low] && target < nums[mid]) 53 | { 54 | return binary_search(nums, low, mid - 1, target); 55 | } 56 | else 57 | { 58 | low = mid + 1; 59 | } 60 | } 61 | else 62 | { 63 | return binary_search(nums, low, high, target); 64 | } 65 | } 66 | return -1; 67 | } 68 | }; 69 | 70 | // Driver function to test the Solution class 71 | int main() 72 | { 73 | Solution solution; 74 | 75 | vector, int>> testCases = { 76 | {{4, 5, 6, 7, 0, 1, 2}, 0}, 77 | {{4, 5, 6, 7, 0, 1, 2}, 3}, 78 | {{1}, 0}, 79 | {{1}, 1}, 80 | {{1, 3, 5}, 5}, 81 | {{5, 1, 3}, 3}}; 82 | 83 | for (const auto &testCase : testCases) 84 | { 85 | const auto &nums = testCase.first; 86 | int target = testCase.second; 87 | int index = solution.search(const_cast &>(nums), target); 88 | 89 | cout << "Searching for " << target << " in ["; 90 | for (size_t i = 0; i < nums.size(); i++) 91 | { 92 | cout << nums[i]; 93 | if (i != nums.size() - 1) 94 | cout << ", "; 95 | } 96 | cout << "] gives index: " << index << endl; 97 | } 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /148/148_Sort_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode 5 | { 6 | int val; 7 | ListNode *next; 8 | ListNode() : val(0), next(nullptr) {} 9 | ListNode(int x) : val(x), next(nullptr) {} 10 | ListNode(int x, ListNode *next) : val(x), next(next) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | public: 16 | ListNode *findMiddle(ListNode *head) 17 | { 18 | if (head == nullptr || head->next == nullptr) 19 | return head; 20 | ListNode *slow = head; 21 | ListNode *fast = head->next; 22 | while (fast != nullptr && fast->next != nullptr) 23 | { 24 | slow = slow->next; 25 | fast = fast->next->next; 26 | } 27 | return slow; 28 | } 29 | 30 | ListNode *merge(ListNode *left, ListNode *right) 31 | { 32 | ListNode *dummy = new ListNode(-1); 33 | ListNode *temp = dummy; 34 | 35 | while (left != nullptr && right != nullptr) 36 | { 37 | if (left->val < right->val) 38 | { 39 | temp->next = left; 40 | left = left->next; 41 | } 42 | else 43 | { 44 | temp->next = right; 45 | right = right->next; 46 | } 47 | temp = temp->next; 48 | } 49 | 50 | if (left != nullptr) 51 | temp->next = left; 52 | if (right != nullptr) 53 | temp->next = right; 54 | 55 | return dummy->next; 56 | } 57 | 58 | ListNode *mergeSort(ListNode *head) 59 | { 60 | if (head == nullptr || head->next == nullptr) 61 | { 62 | return head; 63 | } 64 | 65 | ListNode *middle = findMiddle(head); 66 | ListNode *secondHead = middle->next; 67 | middle->next = nullptr; 68 | 69 | ListNode *left = mergeSort(head); 70 | ListNode *right = mergeSort(secondHead); 71 | 72 | return merge(left, right); 73 | } 74 | 75 | ListNode *sortList(ListNode *head) 76 | { 77 | return mergeSort(head); 78 | } 79 | }; 80 | 81 | // Driver program 82 | int main() 83 | { 84 | // Create a sample linked list: 4 -> 2 -> 1 -> 3 85 | ListNode *head = new ListNode(4); 86 | head->next = new ListNode(2); 87 | head->next->next = new ListNode(1); 88 | head->next->next->next = new ListNode(3); 89 | 90 | Solution solution; 91 | ListNode *sortedHead = solution.sortList(head); 92 | 93 | // Print the sorted linked list 94 | ListNode *temp = sortedHead; 95 | while (temp != nullptr) 96 | { 97 | std::cout << temp->val << " "; 98 | temp = temp->next; 99 | } 100 | std::cout << std::endl; 101 | 102 | // Clean up the linked list 103 | while (sortedHead != nullptr) 104 | { 105 | ListNode *toDelete = sortedHead; 106 | sortedHead = sortedHead->next; 107 | delete toDelete; 108 | } 109 | 110 | return 0; 111 | } --------------------------------------------------------------------------------