├── 1]. DSA CP └── Aot assignment │ ├── LinkedList │ ├── CircularLinkedList.c │ ├── DoubleLinkedList.c │ ├── LinkedList.c │ ├── QueueUsingLinkedList.c │ └── StackUsingLinkedList.c │ ├── Sorting │ ├── bubbleSort.c │ ├── insertionSort.c │ ├── mergeSort.c │ ├── quickSort.c │ └── selectionSort.c │ ├── Stacks and Queues │ ├── infixToPostfix.c │ ├── postfixEvaluation.c │ └── stack.c │ └── Trees │ └── BinarySearchTree.c ├── 1]. DSA + CP ├── 1]. Data Structures │ ├── 01]. Array │ │ ├── C++ │ │ │ ├── _001)_Search_Element.cpp │ │ │ ├── _002)_Insert_Element.cpp │ │ │ ├── _003)_Delete_Element.cpp │ │ │ ├── _004)_Reverse_Array.cpp │ │ │ ├── _005)_Left_Rotate_Array_by_1.cpp │ │ │ ├── _006)_Left_Rotate_Array_by_d_(Time_dN).cpp │ │ │ ├── _007)_Left_Rotate_Array_by_d_(Time_N).cpp │ │ │ ├── _008)_Left_Rotate_Array_by_d_(Time_N).cpp │ │ │ ├── _009)_Remove_Duplicates_from_a_sorted_array.cpp │ │ │ ├── _010)_Remove_Duplicates_from_a_sorted_array.cpp │ │ │ ├── _011)_Largest_Number.cpp │ │ │ ├── _012)_Largest_Number.cpp │ │ │ ├── _013)_Second_Largest_Number.cpp │ │ │ ├── _014)_Second_Largest_Number.cpp │ │ │ ├── _015)_Move_All_ZEROs_to_End.cpp │ │ │ ├── _016)_Move_All_ZEROs_to_End.cpp │ │ │ ├── _017)_Leaders_in_an_Array.cpp │ │ │ ├── _018)_Leaders_in_an_Array.cpp │ │ │ ├── _019)_Maximum_Difference.cpp │ │ │ ├── _020)_Maximum_Difference.cpp │ │ │ ├── _021)_Stock_Buy_and_Shell.cpp │ │ │ ├── _022)_Stock_Buy_and_Shell.cpp │ │ │ ├── _023)_Trapping_Rain_Water.cpp │ │ │ ├── _024)_Trapping_Rain_Water.cpp │ │ │ ├── _025)_Maximum_Consecutive_ONEs_in_binary_Array.cpp │ │ │ ├── _026)_Maximum_Consecutive_ONEs_in_binary_Array.cpp │ │ │ ├── _027)_Maximum_Subarray_Sum.cpp │ │ │ ├── _028)_Maximum_Subarray_Sum.cpp │ │ │ ├── _029)_Maximum_Circular_Subarray_Sum.cpp │ │ │ ├── _030)_Maximum_Circular_Subarray_Sum.cpp │ │ │ ├── _031)_Maximum_length_Even_Odd_Subarray.cpp │ │ │ ├── _032)_Maximum_length_Even_Odd_Subarray.cpp │ │ │ ├── _033)_Majority_Element.cpp │ │ │ ├── _034)_Majority_Element.cpp │ │ │ ├── _035)_Minimum_Group_flips_to_make_same.cpp │ │ │ ├── _036)_Max_Sum_of_K_Consecutive_Element.cpp │ │ │ ├── _037)_Max_Sum_of_K_Consecutive_Element.cpp │ │ │ ├── _038)_Find_SubArray_of_Given_Sum.cpp │ │ │ ├── _039)_Find_SubArray_of_Given_Sum.cpp │ │ │ ├── _040)_Print_N_bonacci_Numbers.cpp │ │ │ ├── _041)_GetSum_on_Given_Queries.cpp │ │ │ ├── _042)_Find_Equilibrium_Point_in_an_Array.cpp │ │ │ ├── _043)_Find_Equilibrium_Point_in_an_Array.cpp │ │ │ ├── _044)_In_Two_Array_Find_Max_Occuring_Element.cpp │ │ │ ├── _045)_sum_of_three_element_is_K.cpp │ │ │ ├── _046)_3Sum.cpp │ │ │ ├── _047)_4Sum.cpp │ │ │ ├── _048)_Jump-Game.cpp │ │ │ ├── _049)_Remove-Duplicates-From-Sorted-Array.cpp │ │ │ ├── _050)_Reverse-Integer.cpp │ │ │ ├── _051)_Set-Matrix-Zeros.cpp │ │ │ └── _052)_Two-Sum.cpp │ │ ├── Dart │ │ │ ├── _001)_Search_Element.dart │ │ │ ├── _002)_Insert_Element.dart │ │ │ ├── _003)_Delete_Element.dart │ │ │ ├── _004)_Reverse_Array.dart │ │ │ ├── _005)_Left_Rotate_Array_by_1.dart │ │ │ ├── _006)_Left_Rotate_Array_by_d_(Time_dN).dart │ │ │ ├── _007)_Left_Rotate_Array_by_d_(Time_N).dart │ │ │ ├── _008)_Left_Rotate_Array_by_d_(Time_N).dart │ │ │ ├── _009)_Right_Rotate_Array_by_d_(Time_N).dart │ │ │ ├── _010)_Remove_Duplicates_from_a_sorted_array.dart │ │ │ ├── _011)_Largest_Number.dart │ │ │ ├── _013)_Second_Largest_Number.dart │ │ │ ├── _014)_Second_Largest_Number.dart │ │ │ ├── _015)_Move_All_ZEROs_to_End.dart │ │ │ ├── _016)_Move_All_ZEROs_to_End.dart │ │ │ ├── _017)_Leaders_in_an_Array.dart │ │ │ ├── _018)_Leaders_in_an_Array.dart │ │ │ ├── _019)_Maximum_Difference.dart │ │ │ ├── _020)_Maximum_Difference.dart │ │ │ ├── _021)_Stock_Buy_and_Sell.dart │ │ │ ├── _022)_Stock_Buy_and_Sell.dart │ │ │ ├── _023)_Trapping_Rain_Water.dart │ │ │ ├── _024)_Trapping_Rain_Water.dart │ │ │ ├── _025)_Maximum_Consecutive_ONEs_in_binary_Array.dart │ │ │ ├── _026)_Maximum_Consecutive_ONEs_in_binary_Array.dart │ │ │ ├── _027)_Maximum_Subarray_Sum.dart │ │ │ ├── _028)_Maximum_Subarray_Sum.dart │ │ │ ├── _029)_Maximum_Circular_Subarray_Sum.dart │ │ │ ├── _030)_Maximum_Circular_Subarray_Sum.dart │ │ │ ├── _031)_Maximum_length_Even_Odd_Subarray.dart │ │ │ ├── _032)_Maximum_length_Even_Odd_Subarray.dart │ │ │ ├── _033)_Majority_Element_in_Array.dart │ │ │ ├── _034)_Majority_Element_in_Array.dart │ │ │ ├── _035)_Minimum_Group_flips_to_make_same.dart │ │ │ ├── _036)_Max_Sum_of_K_Consecutive_Element.dart │ │ │ ├── _037)_Max_Sum_of_K_Consecutive_Element.dart │ │ │ ├── _038)_Find_SubArray_of_Given_Sum.dart │ │ │ ├── _039)_Find_SubArray_of_Given_Sum.dart │ │ │ ├── _040)_Print_N_bonacci_Numbers.dart │ │ │ ├── _041)_Get_Sum_on_Given_Queries.dart │ │ │ ├── _042)_Find_Equilibrium_Point_in_an_Array.dart │ │ │ ├── _043)_Find_Equilibrium_Point_in_an_Array.dart │ │ │ ├── _044)_In_Two_Array_Find_Max_Occuring_Element.dart │ │ │ ├── _046)_Flat_Nested_Array.dart │ │ │ ├── _047)_Sort_Zero_One_Two.dart │ │ │ ├── _048)_Intersection_of_Arrays.dart │ │ │ └── _45)_sum_of_three_element_is_K.dart │ │ ├── Java │ │ │ ├── Contigously_Increasing_Numbers.java │ │ │ ├── _01)_Basic_Array.java │ │ │ ├── _02)_Reverse_Array.java │ │ │ ├── _03)_Duplicate_Array.java │ │ │ ├── _04)_Copy_Array.java │ │ │ ├── _06)_four_sum_problem.java │ │ │ └── _07)_Valid_Triangle.java │ │ ├── JavaScript │ │ │ ├── _001)_Search_Element.js │ │ │ ├── _002)_Insert_Element.js │ │ │ ├── _003)_Delete_Element.js │ │ │ ├── _004)_Reverse_Array.js │ │ │ ├── _005)_Left_Rotate_Array_by_1.js │ │ │ ├── _006)_Left_Rotate_Array_by_d_(Time_dN).js │ │ │ ├── _007)_Left_Rotate_Array_by_d_(Time_N).js │ │ │ ├── _008)_Left_Rotate_Array_by_d_(Time_N).js │ │ │ ├── _009)_Remove_Duplicates_from_a_sorted_array.js │ │ │ ├── _010)_Remove_Duplicates_from_a_sorted_array.js │ │ │ ├── _011)_Largest_Number.js │ │ │ ├── _012)_Largest_Number.js │ │ │ ├── _013)_Second_Largest_Number.js │ │ │ ├── _014)_Second_Largest_Number.js │ │ │ ├── _015)_Move_All_ZEROs_to_End.js │ │ │ ├── _016)_Move_All_ZEROs_to_End.js │ │ │ ├── _017)_Leaders_in_an_Array.js │ │ │ ├── _018)_Leaders_in_an_Array.js │ │ │ ├── _019)_Maximum_Difference.js │ │ │ ├── _020)_Maximum_Difference.js │ │ │ ├── _021_i_)_Best_Time_to_Buy_and_Sell_Stock.js │ │ │ ├── _021_ii_)_Best_Time_to_Buy_and_Sell_Stock.js │ │ │ └── _021_iii_)_Best_Time_to_Buy_and_Sell_Stock.js │ │ └── Python │ │ │ ├── _001)_Search_Element.py │ │ │ ├── _002)_Insert_Element.py │ │ │ ├── _003)_Delete_Element.py │ │ │ ├── _004)_Reverse_Array.py │ │ │ ├── _005)_Left_Rotate_Array_by_1.py │ │ │ ├── _006)_Left_Rotate_Array_by_d_places.py │ │ │ └── _007)_Right_Rotate_Array_by_d_places.py │ ├── 02]. 2D Array │ │ ├── C++ │ │ │ ├── _01)_Print_Snack_Pattern.cpp │ │ │ ├── _02)_Print_Boundry_Element.cpp │ │ │ ├── _03)_Transpose_of_Matrix.cpp │ │ │ ├── _04)_Transpose_of_Matrix.cpp │ │ │ ├── _05)_Rotate_Matrix_by_90_Degree.cpp │ │ │ ├── _06)_Rotate_Matrix_by_90_Degree.cpp │ │ │ ├── _07)_Spiral_Traversal_of_Matrix.cpp │ │ │ ├── _08)_Search_in_RowWise_and_ColumnWise.cpp │ │ │ └── _09)_Search_in_RowWise_and_ColumnWise.cpp │ │ ├── Java │ │ │ ├── _01)_Multi_Dim_Array.java │ │ │ └── _02)_Adding_Multi_Dim_Array.java │ │ └── Python │ │ │ ├── _01)_Print_Snake_Pattern.py │ │ │ └── _03)_Transposing_Matrix.py │ ├── 03]. Recursion │ │ ├── C++ │ │ │ ├── _1)_Print_Subset_Strings.cpp │ │ │ ├── _2)_Rod_Cutting.cpp │ │ │ ├── _3)_Sum_of_Digits.cpp │ │ │ ├── _4)_Tower_of_Hanoi.cpp │ │ │ ├── _5)_Josephus_Problem.cpp │ │ │ ├── _6)_Subset_Sum_Problem.cpp │ │ │ ├── _7)_Generate_Parentheses.cpp │ │ │ ├── _8)_Rat In A Maze.cpp │ │ │ └── _9)_Distinct Occurences.cpp │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ ├── Add Contents.txt │ │ │ ├── Binary_Number.py │ │ │ ├── Climbing_Stairs.py │ │ │ ├── Count_Digits.py │ │ │ ├── Fibonacci_Series.py │ │ │ ├── GCD.py │ │ │ ├── Pow(x,n).py │ │ │ ├── Reverse_String.py │ │ │ ├── Search_In_A_Binary_Search_Tree.py │ │ │ ├── Symmetric_Tree.py │ │ │ ├── factorial.py │ │ │ ├── palindrome_(i).py │ │ │ └── palindrome_(ii).py │ ├── 04]. Strings │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ └── _01)_Longest-Common-prefix.cpp │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 05]. Hashing │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── HashMap.java │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 06]. Linked List │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_Insert_At_Beginning_Of_LinkedList.cpp │ │ │ ├── _02)_Insert_At_End_Of_LinkedList.cpp │ │ │ ├── _03)_Delete-Node-in-Linked-List.cpp │ │ │ ├── _04)_Intersection-Of-Two-Linked-List.cpp │ │ │ ├── _05)_Insert_At_A_Given_Position_Of_LinkedLIst.cpp │ │ │ ├── _06)_Add-Two-Numbers.cpp │ │ │ ├── _07)_Middle-of-the-Linked-List.cpp │ │ │ ├── _08)_Reverse-Linked-List.cpp │ │ │ └── _09)_SortList.cpp │ │ ├── C │ │ │ └── _01)_Create_Insert_Delete_Search_Display_Operations_in_Listed_List.c │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_Insert_At_Beginning _Of_LinkedList.java │ │ │ ├── _02)_Insert_At_End_End_Of_LinkedLIst.java │ │ │ ├── _03)_Delete_From_Beginning_Of_LinkedList.java │ │ │ ├── _04)_Delete_From_End_Of_LinkedList.java │ │ │ ├── _05)_Insert_At_A_Given_Position_In_LinkedList.java │ │ │ ├── _06)i)_Search_In_LinkedList_(Iterative).java │ │ │ ├── _06)ii)_Search_In_LinkedList_(Recursive).java │ │ │ ├── _07)_Middle_Of_LinkedList.java │ │ │ ├── _08)i)_Nth_Node_From_End_Of_LinkedList_(Using length of Linked List).java │ │ │ ├── _08)ii)_Nth_Node_From_End_Of_LinkedList_(Using 2 pointers).java │ │ │ ├── _09)i)_Reverse_LinkedList_(Iterative).java │ │ │ ├── _09)ii)a)_Reverse_LinkedList_(Recursive-Method1).java │ │ │ ├── _09)ii)b)_Reverse_LinkedList_(Recursive-Method2).java │ │ │ ├── _10)_Remove_Duplicates_From_Sorted_linkedList.java │ │ │ ├── _11)i)_Reverse_LinkedList_In_Groups_Of_K_(Iterative).java │ │ │ ├── _11)ii)_Reverse_LinkedList_In_Groups_Of_K_(Recursive).java │ │ │ ├── _12)i)_Detect_Loop_(Using Pointers).java │ │ │ ├── _12)ii)_Detect_Loop_(Using Hashing).java │ │ │ ├── _13)_Detect_And_Remove_Loop_In_LinkedList.java │ │ │ ├── _14)_Delete_Node_With_Given_Pointer_In_LinkedList.java │ │ │ ├── _15)i)_Intersection_Point_Of_2_LinkedLists_(Using Hashing).java │ │ │ ├── _15)ii)_Intersection_Point_Of_2_LinkedLists_(Using Difference of Lengths).java │ │ │ └── _16)_Merge_2_LinkedLists.java │ │ └── Python │ │ │ ├── Add Contents.txt │ │ │ ├── Rotate_LinkedList.py │ │ │ ├── addition.py │ │ │ ├── compare_strings.py │ │ │ ├── count_rotations.py │ │ │ ├── deletion.py │ │ │ ├── insert_whole_LL_at_k.py │ │ │ ├── insertion.py │ │ │ └── swap_nodes.py │ ├── 07]. Doubly Linked List │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_InsertFront_DoublyLinkedList.cpp │ │ │ ├── _02)_InsertAfterGivenNode_DoublyLInkedList.cpp │ │ │ └── _03)_InsertEnd_DoublyLinkedList.cpp │ │ ├── C │ │ │ └── _001)_Palindrome_Checking.c │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── DoubleLinkedList.java │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 08]. Circular Linked List │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── CircularLinkedList.java │ │ └── Python │ │ │ ├── Add Contents.txt │ │ │ └── check_circular_linkedlist.py │ ├── 09]. Stack │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_The Stock Span Problem.cpp │ │ │ ├── _02)_paranthesis.cpp │ │ │ ├── _03)_stack_using_2_queue_costly_pop_approach.cpp │ │ │ ├── _04)_stack_using_2_queue_costly_push_approach.cpp │ │ │ ├── _05)_basics_of_array_imple_of_stack.cpp │ │ │ ├── _06)_reverse_sentence_using_stack.cpp │ │ │ ├── _07)_reverse_stack.cpp │ │ │ ├── _08)_infix_to_postfix_using_stack.cpp │ │ │ ├── _09)_postfix_expr_eval_using_stack.cpp │ │ │ └── _10)_prefix_expr_eval_using_stack.cpp │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── Stacks.java │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 10]. Queue │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_implementation_of_queue_using_linked_list.cpp │ │ │ ├── _02)_implementation_of_queue_using_single_stack.cpp │ │ │ └── _03)_queue_implem_using_2_stack_costly_pop_approach.cpp │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── Queues.java │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 11]. Deueue │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── Dequeue.java │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 12]. Tree │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ ├── BinaryTree.java │ │ │ └── N_ary.java │ │ └── Python │ │ │ ├── Add Contents.txt │ │ │ ├── Binary_Tree_Height.py │ │ │ ├── Binary_Tree_Level_Order_Traversal_(i).py │ │ │ ├── Binary_Tree_Level_Order_Traversal_(ii).py │ │ │ ├── Binary_Tree_Level_Order_Traversal_(iii).py │ │ │ ├── Binary_Tree_Traversal_(i).py │ │ │ ├── Binary_Tree_Traversal_(ii).py │ │ │ ├── Path_Sum.py │ │ │ ├── Symmetric_Tree_(i).py │ │ │ └── Symmetric_Tree_(ii).py │ ├── 13]. Binary Search Tree │ │ ├── C++ │ │ │ ├── _01)_Bottom_View_of_Binary_Tree.cpp │ │ │ ├── _02)_Ceilingin_BST.cpp │ │ │ ├── _03)_Check_For_BST.cpp │ │ │ ├── _04)_Fix_BST_with_Two_Nodes_Swapped.cpp │ │ │ ├── _05)_Kth_Largest.cpp │ │ │ ├── _06)_PairSum.cpp │ │ │ ├── _07)_Top_View_of_Binary_Tree.cpp │ │ │ ├── _08)_Vertical_Sum_of_Binary_Tree.cpp │ │ │ ├── _09)_Vertical_Traversal_of_Binary_Tree.cpp │ │ │ ├── _10)_Delete-Node-in-a-BST.cpp │ │ │ ├── _11)_Insert-in-BST.cpp │ │ │ ├── _12)_LCA-of-a-BST.cpp │ │ │ └── _13)_Search-in-BST.cpp │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── BinarySearchTree.java │ │ └── Python │ │ │ ├── Add Contents.txt │ │ │ └── Validate_Binary_Search_Tree.py │ ├── 14]. Heap │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── Heap.java │ │ ├── Python │ │ │ └── Add Contents.txt │ │ └── TypeScript │ │ │ └── Heap.ts │ ├── 15]. Graph │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── Graph.java │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 16]. Trie │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── Add Contents.txt │ │ │ └── Trie.java │ │ ├── Python │ │ │ ├── Add Contents.txt │ │ │ └── Insert_search_&_delete.py │ │ └── TypeScript │ │ │ └── Trie.ts │ ├── 17]. Segment-Tree │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ └── Add Contents.txt │ └── 18]. Disjoint Set │ │ ├── C++ │ │ └── Add Contents.txt │ │ ├── Java │ │ └── Add Contents.txt │ │ └── Python │ │ └── Add Contents.txt ├── 2]. Algorithms │ ├── 01]. Searching Algorithms │ │ ├── C++ │ │ │ ├── 01) Binary Search.cpp │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── BFS.java │ │ │ ├── BinarSearch.java │ │ │ └── DepthFirstSearch.java │ │ ├── Python │ │ │ ├── 01) Binary Search.py │ │ │ ├── 02) Recursive Binary Search.py │ │ │ ├── 03) Jump Search.py │ │ │ ├── 04) Interpolation Search.py │ │ │ ├── 05) Exponential Search.py │ │ │ └── 06) Fibonacci Search.py │ │ └── Searching_Algorithms_Explanations.md │ ├── 02]. Sorting Algorithms │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _1)_Merge Sort On 2 Linked Lists.cpp │ │ │ ├── _2)_Heap_Sort.cpp │ │ │ ├── _3_)_Bubble_Sort.cpp │ │ │ ├── _4_)_Insertion_Sort.cpp │ │ │ └── _5_)_Selection_Sort.cpp │ │ ├── Java │ │ │ ├── BubbleSort.java │ │ │ ├── InsertionSort.java │ │ │ ├── MergeSort.java │ │ │ ├── QuickSort.java │ │ │ └── SelectionSort.java │ │ ├── Python │ │ │ ├── Add Contents.txt │ │ │ ├── Bubble_Sort.py │ │ │ ├── Count_Sort.py │ │ │ ├── Insertion_Sort.py │ │ │ ├── Merge_Sort.py │ │ │ └── Selection_Sort.py │ │ └── Sorting_Algorithm_Explainations.md │ ├── 03]. Bitwise Algorithms │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _001)_Find_ith_bit.cpp │ │ │ ├── _002)_Replace_Bits.cpp │ │ │ ├── _003)_Decimal_to_Binary.cpp │ │ │ ├── _004)_Clear_ith_bit.cpp │ │ │ ├── _005)_If_A_Number_Is_Power_Of_Two.cpp │ │ │ ├── _006)_Position_Of_Only_Set_Bit.cpp │ │ │ ├── _007)_Two_Non_Repeating_Elements.cpp │ │ │ ├── _008_i_)_Count_Set_Bits_In_An_Integer.cpp │ │ │ ├── _008_ii_)_Count_Set_Bits_In_An_Integer.cpp │ │ │ └── _008_iii_)_Count_Set_Bits_In_An_Integer.cpp │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 04]. Mathematical Algorithms │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 05]. Geometric Algorithms │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 06]. Greedy │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ ├── DijkstraAlgorithm.java │ │ │ └── PrimsAlgorithm.java │ │ └── Python │ │ │ ├── Add Contents.txt │ │ │ ├── Dijkstra's Algorithm.py │ │ │ └── Kruskal’s Minimum Spanning Tree Algorithm.py │ ├── 07]. Dynamic Programming │ │ ├── C++ │ │ │ ├── Add Contents.txt │ │ │ ├── _1)_Minimum Steps to reach the end.cpp │ │ │ ├── _2)_.Frog Jump-1.cpp │ │ │ ├── _3)_Number of Coins.cpp │ │ │ ├── _4)_Wines Problem Top Down dp.cpp │ │ │ ├── _5)_Ladders Problem Bottom up dp.cpp │ │ │ ├── _6)_Box Stacking Problem.cpp │ │ │ └── _7)_Friends pairing problem bottom up.cpp │ │ ├── Java │ │ │ └── Add Contents.txt │ │ └── Python │ │ │ └── Add Contents.txt │ ├── 08]. Divide and Counqer │ │ ├── C++ │ │ │ └── Add Contents.txt │ │ ├── Java │ │ │ └── Add Contents.txt │ │ ├── Python │ │ │ └── Add Contents.txt │ │ └── Readme.md │ └── 09]. Backtracking │ │ ├── C++ │ │ ├── Add Contents.txt │ │ └── _1)_Sudoku Solver.cpp │ │ ├── Java │ │ └── Add Contents.txt │ │ └── Python │ │ ├── N-Queens.py │ │ ├── Rat_maze.py │ │ └── sudoku.py ├── 3]. 450 DSA by Love Babbar │ ├── C++ │ │ ├── 01]. Array │ │ │ ├── _01)_Reverse_String.cpp │ │ │ ├── _02)_Max_Min_Element.cpp │ │ │ ├── _03)_1_Kth_Max_Min_Element.cpp │ │ │ ├── _03)_2_Kth_Max_Min_Element.cpp │ │ │ ├── _04)_Sort_an_Array_012.cpp │ │ │ ├── _05)_Move_All_Negative_Elements.cpp │ │ │ ├── _06) Union_of_two_arrays.cpp │ │ │ ├── _07)_Cyclically_rotate_an_array_by_one.cpp │ │ │ ├── _08)_Kadane's_Algorithm.cpp │ │ │ ├── _09)_Minimize_the_Height.cpp │ │ │ ├── _10)_Minimum_number_of_jumps.cpp │ │ │ ├── _11)_Find_the_Duplicate_Number.cpp │ │ │ ├── _12)_Merge_Without_Extra_Space.cpp │ │ │ ├── _13)_Kadanes_Algorithm.cpp │ │ │ ├── _14)_Merge_Intervals.cpp │ │ │ ├── _15)_Next_Permutation.cpp │ │ │ ├── _16)_Count_Inversions.cpp │ │ │ ├── _17)_Best_Time_to_Buy_and_Sell_Stock.cpp │ │ │ ├── _18)_Count_pairs_with_given_sum.cpp │ │ │ ├── _19)_Common_elements.cpp │ │ │ ├── _20)_Rearrange_Array_in altenating_posotive_and_negative.cpp │ │ │ ├── _21)_SubArray_with_0_Sum.cpp │ │ │ ├── _22)_Factorial_of_Large_Numbers.cpp │ │ │ ├── _23)_Maximum_Product_SubArray.cpp │ │ │ ├── _24)_Largest_consecutive_SubArray.cpp │ │ │ ├── _25)_Count_more_than_n-k_Occurences.cpp │ │ │ ├── _26)_Maximum_Profit_buy_sell_almost_twice.cpp │ │ │ ├── _27)_Array_Subset_of_Another_Array.cpp │ │ │ ├── _28)_Triplet_Sum_in_Array.cpp │ │ │ ├── _29)_Trapping_Rain_Water.cpp │ │ │ ├── _30)_Chocolate_Distribution_Problem.cpp │ │ │ ├── _31)_Smallest_SubArray_with_Sum_Greater_than_x.cpp │ │ │ ├── _32)_Three_way_partitioning.cpp │ │ │ ├── _33)_Minimum_Swaps_and_K_together.cpp │ │ │ ├── _34)_Palindromic_Array.cpp │ │ │ ├── _35)_Find_the _Median.cpp │ │ │ └── _36)_Median_of_two_Sorted_Arrays_of_Different_Size.cpp │ │ ├── 02]. Matrix │ │ │ ├── _01)_Spirally_traversing_a_matrix.cpp │ │ │ ├── _02)_1)_Search_a_2D_Matrix.cpp │ │ │ ├── _02)_2)_Search_a_2D_Matrix.cpp │ │ │ ├── _03)_Median_in_a_row_wise_sorted_Matrix.cpp │ │ │ ├── _04)_Row_with_max_1s.cpp │ │ │ ├── _05)_Sorted_matrix.cpp │ │ │ ├── _06)_Max_Rectangle.cpp │ │ │ ├── _07)_Find_a_Specific_Pair_in_Matrix.cpp │ │ │ ├── _08)_Rotate_a_Matrix_by_90_Degree_in_Clockwise_without_Extra_Space.cpp │ │ │ ├── _09)_Kth_element_in_Matrix.cpp │ │ │ └── _10)_Common_elements_in_all_rows_of_matrix.cpp │ │ ├── 03]. Strings │ │ │ ├── _01)_0_Reverse_String.cpp │ │ │ ├── _01)_1_Reverse_a_String.cpp │ │ │ ├── _02)_Palindrome_String.cpp │ │ │ ├── _03)_Print_all_Duplicates_String.cpp │ │ │ ├── _05)_Check_String_are_Rotations_of_Each_Other.cpp │ │ │ └── _06)_Check_String_is_valid_Suffle_of_two_different_Strings.cpp │ │ ├── 04]. Searching & Sorting │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_First_and_Last_Position_of_an_Element.cpp │ │ │ ├── _02)_find_fixed_point(value equals to index).cpp │ │ │ ├── _03)_Search_in_Rotated_Sorted_Array.cpp │ │ │ ├── _04)_Count_Squares.cpp │ │ │ ├── _05)_Middle-of_Three.cpp │ │ │ ├── _07)_Find_Missing_And_Repeating.cpp │ │ │ ├── _08)_Majority_Element.cpp │ │ │ ├── _10)_Find_Pair_Given_Difference.cpp │ │ │ ├── _11)_Find_All_Four_Sum_Numbers.cpp │ │ │ ├── _12)_Stickler_Thief.cpp │ │ │ ├── _13)_Count_triplets_with_sum_smaller_than_X.cpp │ │ │ ├── _14)_Merge_Without_Extra_Space.cpp │ │ │ ├── _15)_Zero_Sum_Subarrays.cpp │ │ │ ├── _16)_Product_array_puzzle.cpp │ │ │ ├── _17)_Sort_by_Set_Bit_Count.cpp │ │ │ ├── _18)_Minimum_Swaps_to_Sort.cpp │ │ │ └── _22)_Find_pivot_element_in_a_sorted_array.cpp │ │ ├── 05]. Linked List │ │ │ ├── 01)_i_Write_a_Program_to_reverse_the_Linked_List.[Iterative_Solution].cpp │ │ │ ├── 01)_ii_Write_a_Program_to_reverse_the_Linked_List.cpp │ │ │ └── Add Contents.txt │ │ ├── 06]. Binary Tree │ │ │ └── Add Contents.txt │ │ ├── 07]. Binary Search Tree │ │ │ └── Add Contents.txt │ │ ├── 08]. Greedy │ │ │ └── Add Contents.txt │ │ ├── 09]. Backtracking │ │ │ └── Add Contents.txt │ │ ├── 10]. Stack & Queue │ │ │ └── Add Contents.txt │ │ ├── 11]. Heap │ │ │ └── Add Contents.txt │ │ ├── 12]. Graph │ │ │ └── Add Contents.txt │ │ ├── 13]. Trie │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_Construct_a_trie_from_scratch.cpp │ │ │ └── _03)_Word_Break_Problem_(Trie solution).cpp │ │ ├── 14]. Dynamic Programming │ │ │ ├── Add Contents.txt │ │ │ ├── _07)_Edit_Distance.cpp │ │ │ ├── _14)_Longest_Common_Subsequence.cpp │ │ │ └── _35)_LargestSum_Contiguous_Subarray.cpp │ │ └── 15]. Bit Manipulation │ │ │ ├── Add Contents.txt │ │ │ ├── _01)_Number_of_1_Bits.cpp │ │ │ ├── _02)_Non_Repeating_Numbers.cpp │ │ │ ├── _03)_Bit_Difference.cpp │ │ │ ├── _04)_Count_total_set_bits.cpp │ │ │ ├── _05)_Power_of_2.cpp │ │ │ └── _06)_Find_position_of_set_bit.cpp │ ├── Java │ │ ├── 01]. Array │ │ │ ├── _01)_Reverse_String.java │ │ │ ├── _02)_Max_Min_Element.java │ │ │ ├── _03)_kth_max_min.java │ │ │ ├── _04)_Sort_an_Array_012.java │ │ │ ├── _05)_MOve_all_neg_elements.java │ │ │ ├── _06)_Union_intersection_ofSorted_Arrays.java │ │ │ ├── _07)_rotate_array_by one.java │ │ │ ├── _08)_Largest_sum_contiguous_subarray.java │ │ │ └── _10)_min_jumps_to_reach_end.java │ │ ├── 02]. Matrix │ │ │ └── Add Contents.txt │ │ ├── 03]. Strings │ │ │ └── _06)_Check_String_is_valid_Suffle_of_two_different_Strings.java │ │ ├── 04]. Searching & Sorting │ │ │ └── Add Contents.txt │ │ ├── 05]. Linked List │ │ │ └── Add Contents.txt │ │ ├── 06]. Binary Tree │ │ │ └── Add Contents.txt │ │ ├── 07]. Binary Search Tree │ │ │ └── Add Contents.txt │ │ ├── 08]. Greedy │ │ │ └── Add Contents.txt │ │ ├── 09]. Backtracking │ │ │ └── Add Contents.txt │ │ ├── 10]. Stack & Queue │ │ │ └── Add Contents.txt │ │ ├── 11]. Heap │ │ │ └── Add Contents.txt │ │ ├── 12]. Graph │ │ │ └── Add Contents.txt │ │ ├── 13]. Trie │ │ │ └── Add Contents.txt │ │ ├── 14]. Dynamic Programming │ │ │ └── Add Contents.txt │ │ └── 15]. Bit Manipulation │ │ │ ├── _01)_0_Count_SetBit.java │ │ │ ├── _01)_1_Count_SetBits.java │ │ │ ├── _02)_0_Non_Repeating_Elements.java │ │ │ ├── _02)_1_Non_Repeating_Elements.java │ │ │ ├── _03)_Bits_To_Flip.java │ │ │ ├── _04)_0_Total_SetBits_in_Range.java │ │ │ ├── _04)_1_Total_SetBits_in_Range.java │ │ │ ├── _04)_2_Total_SetBits_in_Range.java │ │ │ ├── _05)_Power_Of_2_or_Not.java │ │ │ ├── _06)_Position_Of_Only_SetBit.java │ │ │ ├── _07)_0_Copy_SetBits_From_X_to_Y.java │ │ │ ├── _07)_1_Copy_SetBits_From_X_to_Y.java │ │ │ ├── _08)_Division.java │ │ │ ├── _09)_Square_Of_a_Number.java │ │ │ ├── _10)_0_Power_Set.java │ │ │ └── _10)_1_Power_Set.java │ └── Python │ │ ├── 01]. Array │ │ ├── 01_)_Reverse_Array.py │ │ ├── 02_)_Max_Min_Element.py │ │ ├── 03_)_Kth_Max_Min_Element.py │ │ ├── 04_i_)_Sort012.py │ │ ├── 04_ii_)_Sort0123.py │ │ ├── 05_i_)_Move_All_Negative_Element_To_Front.py │ │ ├── 05_ii_)_Move_All_Negative_Element_To_Front.py │ │ ├── 06_)_Union_Of_Two_Array.py │ │ ├── 07_)_Cyclically_Rotate_An_Array_By_One.py │ │ ├── 08_i_)_Kadane's_Algorithm.py │ │ ├── 08_ii_)_Kadane's_Algorithm.py │ │ ├── 09_)_Minimize_The_Height.py │ │ ├── 14_Merge_Intervals.py │ │ └── Add Contents.txt │ │ ├── 02]. Matrix │ │ └── Add Contents.txt │ │ ├── 03]. Strings │ │ └── Add Contents.txt │ │ ├── 04]. Searching & Sorting │ │ └── Add Contents.txt │ │ ├── 05]. Linked List │ │ └── Add Contents.txt │ │ ├── 06]. Binary Tree │ │ └── Add Contents.txt │ │ ├── 07]. Binary Search Tree │ │ └── Add Contents.txt │ │ ├── 08]. Greedy │ │ └── Add Contents.txt │ │ ├── 09]. Backtracking │ │ └── Add Contents.txt │ │ ├── 10]. Stack & Queue │ │ └── Add Contents.txt │ │ ├── 11]. Heap │ │ └── Add Contents.txt │ │ ├── 12]. Graph │ │ └── Add Contents.txt │ │ ├── 13]. Trie │ │ └── Add Contents.txt │ │ ├── 14]. Dynamic Programming │ │ └── Add Contents.txt │ │ └── 15]. Bit Manipulation │ │ └── Add Contents.txt ├── 4]. Striver Series │ └── 30 Days of SDE Sheet │ │ └── C++ │ │ ├── 01]. Day-1 (Arrays) │ │ ├── CodeStudio │ │ │ ├── 1]. Set Matrix Zeros.cpp │ │ │ ├── 2]. Pascal’s Triangle.cpp │ │ │ ├── 3]. Next Permutation.cpp │ │ │ ├── 4]. Kadane’s Algorithm.cpp │ │ │ ├── 5]. Sort an array of 0’s 1’s 2’s.cpp │ │ │ └── 6]. Stock buy and Sell.cpp │ │ ├── Interviewbit │ │ │ ├── 1]. Set Matrix Zeros.cpp │ │ │ ├── 2]. Pascal’s Triangle.cpp │ │ │ ├── 3]. Next Permutation.cpp │ │ │ ├── 4]. Kadane’s Algorithm.cpp │ │ │ ├── 5]. Sort an array of 0’s 1’s 2’s.cpp │ │ │ └── 6]. Stock buy and Sell.cpp │ │ └── Leetcode │ │ │ ├── 1]. Set Matrix Zeros.cpp │ │ │ ├── 2]. Pascal’s Triangle.cpp │ │ │ ├── 3]. Next Permutation.cpp │ │ │ ├── 4]. Kadane’s Algorithm.cpp │ │ │ ├── 5]. Sort an array of 0’s 1’s 2’s.cpp │ │ │ └── 6]. Stock buy and Sell.cpp │ │ ├── 02]. Day-2 (Arrays) │ │ ├── CodeStudio │ │ │ ├── 1]. Rotate Matrix.cpp │ │ │ ├── 2]. Merge Overlapping Subintervals.cpp │ │ │ ├── 3]. Merge two sorted Arrays without extra space.cpp │ │ │ ├── 4]. Find the duplicate in an array of N+1 integers.cpp │ │ │ ├── 5]. Repeat and Missing Number.cpp │ │ │ └── 6]. Count Inversions.cpp │ │ ├── Interviewbit │ │ │ └── 5]. Repeat and Missing Number.cpp │ │ └── Leetcode │ │ │ ├── 1]. Rotate Matrix.cpp │ │ │ ├── 2]. Merge Overlapping Subintervals.cpp │ │ │ ├── 3]. Merge two sorted Arrays without extra space.cpp │ │ │ └── 4]. Find the duplicate in an array of N+1 integers.cpp │ │ ├── 03]. Day-3 (Arrays) │ │ ├── CodeStudio │ │ │ ├── 1]. Search in a 2d Matrix.cpp │ │ │ ├── 2]. Pow(X,n).cpp │ │ │ ├── 3]. Majority Element (N-2 times).cpp │ │ │ ├── 4]. Majority Element (N-3 times).cpp │ │ │ ├── 5]. Grid Unique Paths.cpp │ │ │ └── 6]. Reverse Pairs.cpp │ │ └── Leetcode │ │ │ ├── 1]. Search in a 2d Matrix.cpp │ │ │ ├── 2]. Pow(X,n).cpp │ │ │ ├── 3]. Majority Element (N-2 times).cpp │ │ │ ├── 4]. Majority Element (N-3 times).cpp │ │ │ ├── 5]. Grid Unique Paths.cpp │ │ │ └── 6]. Reverse Pairs.cpp │ │ ├── 04]. Day-4 (Arrays) │ │ ├── CodeStudio │ │ │ ├── 1]. 2-Sum-Problem.cpp │ │ │ ├── 2]. 4-sum-Problem.cpp │ │ │ ├── 3]. Longest Consecutive Sequence.cpp │ │ │ ├── 4]. Largest Subarray with 0 sum.cpp │ │ │ ├── 5]. Count number of subarrays with given Xor K.cpp │ │ │ └── 6]. Longest Substring without repeat.cpp │ │ ├── GeeksforGeeks │ │ │ └── 4]. Largest Subarray with 0 sum.cpp │ │ ├── Interviewbit │ │ │ └── 5]. Count number of subarrays with given Xor K.cpp │ │ └── Leetcode │ │ │ ├── 1]. 2-Sum-Problem.cpp │ │ │ ├── 2]. 4-sum-Problem.cpp │ │ │ ├── 3]. Longest Consecutive Sequence.cpp │ │ │ └── 6]. Longest Substring without repeat.cpp │ │ ├── 05]. Day-5 (Linked List) │ │ ├── CodeStudio │ │ │ ├── 1]. Reverse a LinkedList.cpp │ │ │ ├── 2]. Find the middle of LinkedList.cpp │ │ │ ├── 3]. Merge two sorted Linked List.cpp │ │ │ ├── 4]. Remove N-th node from back of LinkedList.cpp │ │ │ ├── 5]. Add two numbers as LinkedList.cpp │ │ │ └── 6]. Delete a given Node when a node is given.cpp │ │ └── Leetcode │ │ │ ├── 1]. Reverse a LinkedList.cpp │ │ │ ├── 2]. Find the middle of LinkedList.cpp │ │ │ ├── 3]. Merge two sorted Linked List.cpp │ │ │ ├── 4]. Remove N-th node from back of LinkedList.cpp │ │ │ ├── 5]. Add two numbers as LinkedList.cpp │ │ │ └── 6]. Delete a given Node when a node is given.cpp │ │ ├── 06]. Day-6 (Linked List) │ │ ├── CodeStudio │ │ │ ├── 1]. Find intersection point of Y LinkedList.cpp │ │ │ ├── 2]. Detect a cycle in Linked List.cpp │ │ │ ├── 3]. Reverse a LinkedList in groups of size k.cpp │ │ │ ├── 4]. Check if a LinkedList is palindrome or not.cpp │ │ │ ├── 5]. Find the starting point of the Loop of LinkedList.cpp │ │ │ └── 6]. Flattening of a LinkedList.cpp │ │ ├── GeeksforGeeks │ │ │ └── 6]. Flattening of a LinkedList.cpp │ │ └── Leetcode │ │ │ ├── 1]. Find intersection point of Y LinkedList.cpp │ │ │ ├── 2]. Detect a cycle in Linked List.cpp │ │ │ ├── 3]. Reverse a LinkedList in groups of size k.cpp │ │ │ ├── 4]. Check if a LinkedList is palindrome or not.cpp │ │ │ └── 5]. Find the starting point of the Loop of LinkedList.cpp │ │ ├── 07]. Day-7 (Linked List and Arrays) │ │ ├── CodeStudio │ │ │ ├── 1]. Rotate a LinkedList.cpp │ │ │ ├── 2]. Clone a Linked List with random and next pointer.cpp │ │ │ ├── 3]. 3 sum.cpp │ │ │ ├── 4]. Trapping rainwater.cpp │ │ │ ├── 5]. Remove Duplicate from Sorted array.cpp │ │ │ └── 6]. Max consecutive ones.cpp │ │ └── Leetcode │ │ │ ├── 1]. Rotate a LinkedList.cpp │ │ │ ├── 2]. Clone a Linked List with random and next pointer.cpp │ │ │ ├── 3]. 3 sum.cpp │ │ │ ├── 4]. Trapping rainwater.cpp │ │ │ ├── 5]. Remove Duplicate from Sorted array.cpp │ │ │ └── 6]. Max consecutive ones.cpp │ │ ├── 08]. Day-8 (Greedy Algorithm) │ │ ├── CodeStudio │ │ │ ├── 1]. N meetings in one room.cpp │ │ │ ├── 2]. Minimum number of platforms required for a railway.cpp │ │ │ ├── 3]. Job sequencing Problem.cpp │ │ │ ├── 4]. Fractional Knapsack Problem.cpp │ │ │ ├── 5]. Greedy algorithm to find minimum number of coins.cpp │ │ │ └── 6]. Activity Selection.cpp │ │ └── GeeksforGeeks │ │ │ ├── 1]. N meetings in one room.cpp │ │ │ ├── 2]. Minimum number of platforms required for a railway.cpp │ │ │ ├── 3]. Job sequencing Problem.cpp │ │ │ ├── 4]. Fractional Knapsack Problem.cpp │ │ │ ├── 5]. Greedy algorithm to find minimum number of coins.cpp │ │ │ └── 6]. Activity Selection.cpp │ │ ├── 09]. Day-9 (Recursion) │ │ ├── CodeStudio │ │ │ ├── 1]. Subset Sums.cpp │ │ │ ├── 2]. Subset-II.cpp │ │ │ ├── 3]. Combination sum-1.cpp │ │ │ ├── 4]. Combination sum-2.cpp │ │ │ ├── 5]. Palindrome Partitioning.cpp │ │ │ └── 6]. K-th permutation Sequence.cpp │ │ ├── GeeksforGeeks │ │ │ └── 1]. Subset Sums.cpp │ │ └── Leetcode │ │ │ ├── 2]. Subset-II.cpp │ │ │ ├── 3]. Combination sum-1.cpp │ │ │ ├── 4]. Combination sum-2.cpp │ │ │ ├── 5]. Palindrome Partitioning.cpp │ │ │ └── 6]. K-th permutation Sequence.cpp │ │ ├── 10]. Day-10 (Recursion and Backtracking) │ │ ├── CodeStudio │ │ │ ├── 1]. Print all permutations of a string-array.cpp │ │ │ ├── 2]. N queens Problem.cpp │ │ │ ├── 3]. Sudoku Solver.cpp │ │ │ ├── 4]. M coloring Problem.cpp │ │ │ ├── 5]. Rat in a Maze.cpp │ │ │ └── 6]. Word Break.cpp │ │ ├── GeeksforGeeks │ │ │ ├── 4]. M coloring Problem.cpp │ │ │ └── 5]. Rat in a Maze.cpp │ │ └── Leetcode │ │ │ ├── 1]. Print all permutations of a string-array.cpp │ │ │ ├── 2]. N queens Problem.cpp │ │ │ └── 3]. Sudoku Solver.cpp │ │ └── 11]. Day-11 (Binary Search) │ │ └── CodeStudio │ │ ├── 1]. The N-th root of an integer.cpp │ │ └── 2]. Matrix Median.cpp └── Aot assignment │ ├── LinkedList │ ├── CircularLinkedList.c │ ├── DoubleLinkedList.c │ ├── LinkedList.c │ ├── QueueUsingLinkedList.c │ └── StackUsingLinkedList.c │ ├── Sorting │ ├── bubbleSort.c │ ├── insertionSort.c │ ├── mergeSort.c │ ├── quickSort.c │ └── selectionSort.c │ └── Trees │ └── BinarySearchTree.c ├── 2]. DSA Notes.pdf ├── 3]. DSA BOOK .pdf ├── 4]. The Bible of DSA .pdf ├── LICENSE └── README.md /1]. DSA + CP/1]. Data Structures/01]. Array/C++/_001)_Search_Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // function to search for element in array 4 | int search(int arr[], int n, int x) 5 | { 6 | for(int i = 0; i < n; i++) 7 | { 8 | if(arr[i] == x) 9 | return i; 10 | } 11 | 12 | return -1; 13 | } 14 | 15 | int main() 16 | { 17 | int arr[] = {20, 5, 7, 25}, x = 5; 18 | cout<<"Searched Index = "< 2 | #include 3 | using namespace std; 4 | 5 | void lRotateOne(int arr[], int n) 6 | { 7 | int temp = arr[0]; 8 | for(int i = 1; i < n; i++) 9 | { 10 | arr[i - 1] = arr[i]; 11 | } 12 | arr[n - 1] = temp; 13 | } 14 | 15 | int main() 16 | { 17 | int arr[] = {1, 2, 3, 4, 5}, n = 5; 18 | cout<<"Before Left Rotation"< 2 | using namespace std; 3 | 4 | int getLargest(int arr[], int n) 5 | { 6 | for(int i=0; i arr[i]) 12 | { 13 | flag = false; 14 | break; 15 | } 16 | } 17 | if(flag==true) 18 | return i; 19 | } 20 | return -1; 21 | } 22 | 23 | int main() 24 | { 25 | int arr[] = {10, 5, 20, 8}, n=4; 26 | cout<<"Index = "< 2 | using namespace std; 3 | 4 | int getLargest(int arr[], int n) 5 | { 6 | int res=0; 7 | for(int i=1; i arr[res]) 9 | res = i; 10 | return res; 11 | } 12 | 13 | int main() 14 | { 15 | int arr[] = {10, 5, 20, 8}, n=4; 16 | cout<<"Index = "< 2 | using namespace std; 3 | 4 | int getLargest(int arr[], int n) 5 | { 6 | int largest=0; 7 | for(int i=1; i arr[largest]) 9 | largest = i; 10 | return largest; 11 | } 12 | 13 | int getSecondLargest(int arr[], int n) 14 | { 15 | int largest = getLargest(arr, n); 16 | int res = -1; 17 | for(int i=0; i arr[res]) 24 | res = i; 25 | } 26 | } 27 | return res; 28 | } 29 | 30 | int main() 31 | { 32 | int arr[] = {10, 5, 18, 20}, n=4; 33 | cout<<"Index = "< 2 | using namespace std; 3 | 4 | int getSecondLargest(int arr[], int n) 5 | { 6 | int res = -1, largest = 0; 7 | for(int i=1; i arr[largest]) 10 | { 11 | res=largest; 12 | largest=i; 13 | } 14 | else if(arr[i] != arr[largest]) 15 | { 16 | if(res == -1 ||arr[i] > arr[res]) 17 | res = i; 18 | } 19 | } 20 | return res; 21 | } 22 | 23 | int main() 24 | { 25 | int arr[] = {10, 5, 18, 20}, n=4; 26 | cout<<"Index = "< 2 | using namespace std; 3 | 4 | int leaders(int arr[], int n) 5 | { 6 | for(int i=0; i=0; i--) 10 | { 11 | if(cur_leader < arr[i]) 12 | { 13 | cur_leader = arr[i]; 14 | cout< 2 | using namespace std; 3 | 4 | int maxSumOf_K_Element(int arr[], int k, int n) 5 | { 6 | int max_sum=0; 7 | for(int i=0; i+k-1 2 | using namespace std; 3 | 4 | int subArraySum(int arr[], int n, int given_sum) 5 | { 6 | int curr_sum = arr[0], start=0; 7 | for(int end=1; end given_sum && start < end-1) 10 | { 11 | curr_sum -= arr[start]; 12 | start++; 13 | } 14 | if(curr_sum == given_sum) 15 | { 16 | cout<<"Sum found between indexes "<< start <<" and "<< end-1 < 2 | using namespace std; 3 | 4 | void bonacciseries(long n, int m) 5 | { 6 | cout << "N-bonacci Numbers = "; 7 | int a[m] = {0}; 8 | a[n - 1] = 1; 9 | a[n] = 1; 10 | for (int i = n + 1; i < m; i++) 11 | a[i] = 2 * a[i - 1] - a[i - n - 1]; 12 | for (int i = 0; i < m; i++) 13 | cout << a[i] << " "; 14 | cout << endl; 15 | } 16 | 17 | int main() 18 | { 19 | int N = 5, M = 15; 20 | bonacciseries(N, M); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/C++/_041)_GetSum_on_Given_Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int prefix_sum[10000]; 5 | void preSum(int arr[], int n) 6 | { 7 | prefix_sum[0] = arr[0]; 8 | 9 | for(int i = 1; i < n; i++) 10 | { 11 | prefix_sum[i] = prefix_sum[i - 1] + arr[i]; 12 | } 13 | } 14 | 15 | int getSum(int prefix_sum[], int l, int r) 16 | { 17 | if(l != 0) 18 | return prefix_sum[r] - prefix_sum[l - 1]; 19 | else 20 | return prefix_sum[r]; 21 | } 22 | 23 | 24 | 25 | int main() 26 | { 27 | int arr[] = {2, 8, 3, 9, 6, 5, 4}, n = 7; 28 | preSum(arr, n); 29 | cout<<"Sum of subarray index from 1 to 3 = "< 2 | using namespace std; 3 | 4 | bool checkEquilibrium(int arr[], int n) 5 | { 6 | int sum = 0; 7 | for(int i = 0; i < n; i++) 8 | sum += arr[i]; 9 | int l_sum = 0; 10 | for(int i = 0; i < n; i++) 11 | { 12 | if(l_sum == sum - arr[i]) 13 | return true; 14 | l_sum += arr[i]; 15 | sum -= arr[i]; 16 | } 17 | return false; 18 | } 19 | 20 | int main() 21 | { 22 | int arr[] = {4, 2, 2}, n = 3; 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int maxOcc(int L[], int R[], int n) 6 | { 7 | int arr[1000]; 8 | memset(arr, 0, sizeof(arr)); 9 | for (int i = 0; i < n; i++) 10 | { 11 | arr[L[i]]++; 12 | arr[R[i] + 1]--; 13 | } 14 | int maxm = arr[0], res = 0; 15 | for (int i = 1; i < 1000; i++) 16 | { 17 | arr[i] += arr[i - 1]; 18 | if (maxm < arr[i]) 19 | { 20 | maxm = arr[i]; 21 | res = i; 22 | } 23 | } 24 | return res; 25 | } 26 | 27 | int main() 28 | { 29 | int L[] = {1, 2, 3}, R[] = {3, 5, 7}, n = 3; 30 | cout << "Maximum Occuring Element in both Array = " << maxOcc(L, R, n) << endl; 31 | } 32 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/C++/_048)_Jump-Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int n = nums.size(); 5 | int goodIndx = n-1; 6 | for(int i=n-2;i>=0;i--){ 7 | 8 | if(i+nums[i]>=goodIndx){ 9 | goodIndx = i; 10 | } 11 | 12 | } 13 | 14 | if(goodIndx == 0){ 15 | return true; 16 | }else{ 17 | return false; 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/C++/_049)_Remove-Duplicates-From-Sorted-Array.cpp: -------------------------------------------------------------------------------- 1 | int removeDuplicates(vector& nums) { 2 | int k = 0,n = nums.size(); 3 | 4 | for(int i=0;i -pow(2,31) && sum < pow(2,31)-1){ 14 | return sum; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/C++/_052)_Two-Sum.cpp: -------------------------------------------------------------------------------- 1 | vector twoSum(vector& A, int target) { 2 | 3 | vector ans; 4 | unordered_map mymap; 5 | 6 | for(int i=0; i arr.length) { 3 | throw new Exception("Invalid position"); 4 | } 5 | for (int i = positon; i < arr.length; i++) { 6 | var temp = arr[i]; 7 | arr[i] = element; 8 | element = temp; 9 | } 10 | arr.add(element); 11 | } 12 | 13 | void main() { 14 | var arr = [1, 2, 3, 4, 5]; 15 | print('Before Insertion: ${arr}'); 16 | insert(arr, 2, 6); 17 | print('After Insertion: ${arr}'); 18 | } 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_003)_Delete_Element.dart: -------------------------------------------------------------------------------- 1 | List delete(arr, element) { 2 | if (arr.indexOf(element) == -1) { 3 | throw new Exception("Element does not exist in array"); 4 | } 5 | var newArr = []; 6 | for (int i = 0; i < arr.length; i++) { 7 | if (arr[i] != element) { 8 | newArr.add(arr[i]); 9 | } 10 | } 11 | return newArr; 12 | } 13 | 14 | void main() { 15 | var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 16 | print('Before Deletion: ${arr}'); 17 | var delArr = delete(arr, 6); 18 | print('After Deletion: ${delArr}'); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_004)_Reverse_Array.dart: -------------------------------------------------------------------------------- 1 | void reverse(arr) { 2 | var low = 0; 3 | var high = arr.length - 1; 4 | 5 | while (low < high) { 6 | var temp = arr[low]; 7 | arr[low] = arr[high]; 8 | arr[high] = temp; 9 | 10 | low++; 11 | high--; 12 | } 13 | } 14 | 15 | void main() { 16 | var arr = [1, 2, 3, 4, 5]; 17 | print('Before Reverse: ${arr}'); 18 | reverse(arr); 19 | print('After Reverse: ${arr}'); 20 | } 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_005)_Left_Rotate_Array_by_1.dart: -------------------------------------------------------------------------------- 1 | void leftRotateOne(arr) { 2 | var temp = arr[0]; 3 | for (int i = 1; i < arr.length; i++) { 4 | arr[i - 1] = arr[i]; 5 | } 6 | arr[arr.length - 1] = temp; 7 | } 8 | 9 | void main() { 10 | var arr = [1, 2, 3, 4, 5]; 11 | print('Before Left Rotate: ${arr}'); 12 | leftRotateOne(arr); 13 | print('After Left Rotate: ${arr}'); 14 | } 15 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_006)_Left_Rotate_Array_by_d_(Time_dN).dart: -------------------------------------------------------------------------------- 1 | void leftRotateOne(arr, int n) { 2 | var temp = arr[0]; 3 | for (int i = 1; i < n; i++) { 4 | arr[i - 1] = arr[i]; 5 | } 6 | arr[n - 1] = temp; 7 | } 8 | 9 | void leftRotate(arr, int d, int n) { 10 | for (int i = 0; i < d; i++) { 11 | leftRotateOne(arr, n); 12 | } 13 | } 14 | 15 | void main(List args) { 16 | var arr = [1, 2, 3, 4, 5]; 17 | print('Before Left Rotate: ${arr}'); 18 | leftRotate(arr, 2, arr.length); 19 | print('After Left Rotate: ${arr}'); 20 | } 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_007)_Left_Rotate_Array_by_d_(Time_N).dart: -------------------------------------------------------------------------------- 1 | void leftRotate(arr, int d, int n) { 2 | var temp = List.empty(growable: true)..length = d; 3 | 4 | for (int i = 0; i < d; i++) { 5 | temp[i] = arr[i]; 6 | } 7 | for (int i = d; i < n; i++) { 8 | arr[i - d] = arr[i]; 9 | } 10 | for (int i = 0; i < d; i++) { 11 | arr[n - d + i] = temp[i]; 12 | } 13 | } 14 | 15 | void main() { 16 | var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 17 | print('Before Left Rotate: ${arr}'); 18 | leftRotate(arr, 3, arr.length); 19 | print('After Left Rotate: ${arr}'); 20 | } 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_008)_Left_Rotate_Array_by_d_(Time_N).dart: -------------------------------------------------------------------------------- 1 | void reverse(arr, low, high) { 2 | while (low < high) { 3 | var temp = arr[low]; 4 | arr[low] = arr[high]; 5 | arr[high] = temp; 6 | 7 | low++; 8 | high--; 9 | } 10 | } 11 | 12 | void leftRotate(arr, d, n) { 13 | reverse(arr, 0, d - 1); 14 | reverse(arr, d, n - 1); 15 | reverse(arr, 0, n - 1); 16 | } 17 | 18 | void main() { 19 | var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 20 | print('Before Left Rotate: ${arr}'); 21 | leftRotate(arr, 4, arr.length); 22 | print('After Left Rotate: ${arr}'); 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_009)_Right_Rotate_Array_by_d_(Time_N).dart: -------------------------------------------------------------------------------- 1 | void reverse(arr, low, high) { 2 | while (low < high) { 3 | var temp = arr[low]; 4 | arr[low] = arr[high]; 5 | arr[high] = temp; 6 | 7 | low++; 8 | high--; 9 | } 10 | } 11 | 12 | void leftRotate(arr, d, n) { 13 | reverse(arr, 0, n - 1); 14 | reverse(arr, 0, d - 1); 15 | reverse(arr, d, n - 1); 16 | } 17 | 18 | void main() { 19 | var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 20 | print('Before Left Rotate: ${arr}'); 21 | leftRotate(arr, 4, arr.length); 22 | print('After Left Rotate: ${arr}'); 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_010)_Remove_Duplicates_from_a_sorted_array.dart: -------------------------------------------------------------------------------- 1 | List remDups(arr) { 2 | var i = 0; 3 | while (i < arr.length - 1) { 4 | if (arr[i] == arr[i + 1]) { 5 | arr.removeAt(i); 6 | } else { 7 | i++; 8 | } 9 | } 10 | return arr; 11 | } 12 | 13 | void main() { 14 | var arr = [0, 20, 20, 30, 30, 30, 30, 50, 70]; 15 | print('Before Duplicate Removal: ${arr}'); 16 | var newArr = remDups(arr); 17 | print('After Duplicate Removal: ${newArr}'); 18 | } 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_011)_Largest_Number.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int getLargest(arr) { 4 | var res = 0; 5 | for (var i = 1; i < arr.length; i++) { 6 | if (arr[i] > arr[res]) res = i; 7 | } 8 | return res; 9 | } 10 | 11 | void main() { 12 | var arr = [10, 100, 20, 70, 40, 50, 60, 30, 80, 90]; 13 | var index = getLargest(arr); 14 | print('Largest element in ${arr} is ${arr[index]}.'); 15 | 16 | var largestNum = arr.reduce((value, element) => max(value, element)); 17 | print('Largest element in ${arr} is $largestNum.'); 18 | } 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_013)_Second_Largest_Number.dart: -------------------------------------------------------------------------------- 1 | int getLargest(arr) { 2 | var res = 0; 3 | for (var i = 1; i < arr.length; i++) { 4 | if (arr[i] > arr[res]) res = i; 5 | } 6 | return res; 7 | } 8 | 9 | int getSecondLargest(arr) { 10 | var largest = getLargest(arr); 11 | var res = -1; 12 | for (var i = 0; i < arr.length; i++) { 13 | if (arr[i] != arr[largest]) { 14 | if (res == -1) 15 | res = i; 16 | else if (arr[i] > arr[res]) res = i; 17 | } 18 | } 19 | return res; 20 | } 21 | 22 | void main() { 23 | var arr = [10, 100, 20, 70, 40, 50, 60, 30, 80, 90]; 24 | var index = getSecondLargest(arr); 25 | print('Second largest element in ${arr} is ${arr[index]}.'); 26 | } 27 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_014)_Second_Largest_Number.dart: -------------------------------------------------------------------------------- 1 | int getSecondLargest(arr) { 2 | var res = -1, largest = 0; 3 | for (var i = 1; i < arr.length; i++) { 4 | if (arr[i] > arr[largest]) { 5 | res = largest; 6 | largest = i; 7 | } else if (arr[i] != arr[largest]) { 8 | if (res == -1 || arr[i] > arr[res]) res = i; 9 | } 10 | } 11 | return res; 12 | } 13 | 14 | void main() { 15 | var arr = [10, 100, 20, 70, 40, 50, 60, 30, 80, 90]; 16 | var index = getSecondLargest(arr); 17 | print('Second largest element in ${arr} is ${arr[index]}.'); 18 | } 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_015)_Move_All_ZEROs_to_End.dart: -------------------------------------------------------------------------------- 1 | void moveToEnd(arr) { 2 | for (var i = 0; i < arr.length; i++) { 3 | if (arr[i] == 0) { 4 | for (var j = i + 1; j < arr.length; j++) { 5 | if (arr[j] != 0) { 6 | //swaping the values 7 | var temp = arr[i]; 8 | arr[i] = arr[j]; 9 | arr[j] = temp; 10 | break; 11 | } 12 | } 13 | } 14 | } 15 | } 16 | 17 | void main() { 18 | var arr = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]; 19 | print('Before moving zeros: ${arr}'); 20 | moveToEnd(arr); 21 | print('After moving zeros: ${arr}'); 22 | } 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_016)_Move_All_ZEROs_to_End.dart: -------------------------------------------------------------------------------- 1 | void moveToEnd(arr) { 2 | var count = 0; 3 | for (var i = 0; i < arr.length; i++) { 4 | if (arr[i] != 0) { 5 | //swaping the values 6 | var temp = arr[i]; 7 | arr[i] = arr[count]; 8 | arr[count] = temp; 9 | count++; 10 | } 11 | } 12 | } 13 | 14 | void main() { 15 | var arr = [5, 0, 0, 4, 0, 7, 0, 0, 0, 3]; 16 | print('Before moving zeros: ${arr}'); 17 | moveToEnd(arr); 18 | print('After moving zeros: ${arr}'); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_017)_Leaders_in_an_Array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | If current element/person value is greater than front element/person 3 | then current element is considered as a leader 4 | */ 5 | 6 | List findLeaders(arr) { 7 | List leaders = []; 8 | for (int i = 0; i < arr.length; i++) { 9 | var flag = false; 10 | for (var j = i + 1; j < arr.length; j++) { 11 | if (arr[i] <= arr[j]) { 12 | flag = true; 13 | break; 14 | } 15 | } 16 | if (flag == false) { 17 | leaders.add(arr[i]); 18 | } 19 | } 20 | return leaders; 21 | } 22 | 23 | void main() { 24 | var arr = [7, 10, 4, 3, 6, 5, 2]; 25 | var leaders = findLeaders(arr); 26 | print('Leaders in the array are: $leaders'); 27 | } 28 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_018)_Leaders_in_an_Array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | If current element/person value is greater than front element/person 3 | then current element is considered as a leader 4 | */ 5 | 6 | List findLeaders(arr) { 7 | var n = arr.length; 8 | var currLeader = arr[n - 1]; 9 | var leaders = [arr[n - 1]]; 10 | 11 | for (int i = n - 2; i >= 0; i--) { 12 | if (arr[i] > currLeader) { 13 | currLeader = arr[i]; 14 | leaders.add(currLeader); 15 | } 16 | } 17 | return leaders; 18 | } 19 | 20 | void main() { 21 | var arr = [7, 10, 4, 3, 6, 5, 2]; 22 | var leaders = findLeaders(arr); 23 | print('Leaders in the array are: $leaders'); 24 | } 25 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_019)_Maximum_Difference.dart: -------------------------------------------------------------------------------- 1 | int maxDiff(arr) { 2 | var n = arr.length; 3 | var res = arr[1] - arr[0]; 4 | 5 | for (int i = 0; i < n; i++) { 6 | for (int j = i + 1; j < n; j++) { 7 | if (arr[j] - arr[i] > res) { 8 | res = arr[j] - arr[i]; 9 | } 10 | } 11 | } 12 | return res; 13 | } 14 | 15 | void main() { 16 | var arr = [2, 3, 10, 6, 4, 8, 1]; 17 | var diff = maxDiff(arr); 18 | print('Leaders in the array are: $diff'); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_020)_Maximum_Difference.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxDiff(arr) { 4 | int n = arr.length; 5 | int res = arr[1] - arr[0]; 6 | int minVal = arr[0]; 7 | 8 | for (int i = 1; i < n; i++) { 9 | res = max(res, arr[i] - minVal); 10 | minVal = min(minVal, arr[i]); 11 | } 12 | return res; 13 | } 14 | 15 | void main() { 16 | var arr = [2, 3, 10, 6, 4, 8, 1]; 17 | var diff = maxDiff(arr); 18 | print('Leaders in the array are: $diff'); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_021)_Stock_Buy_and_Sell.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxProfit(price, start, end) { 4 | if (end <= start) return 0; 5 | int profit = 0; 6 | 7 | for (int i = start; i < end; i++) { 8 | for (int j = i + 1; j < end; j++) { 9 | if (price[j] > price[i]) { 10 | int currProfit = (price[j] - price[i]) + 11 | maxProfit(price, start, i - 1) + 12 | maxProfit(price, j + 1, end); 13 | profit = max(profit, currProfit); 14 | } 15 | } 16 | } 17 | return profit; 18 | } 19 | 20 | void main() { 21 | var price = [1, 5, 3, 8, 12]; 22 | int start = 0, end = 5; 23 | print('Maximum Profit in Stock Market = ${maxProfit(price, start, end)}'); 24 | } 25 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_022)_Stock_Buy_and_Sell.dart: -------------------------------------------------------------------------------- 1 | int maxProfit(price) { 2 | int profit = 0; 3 | 4 | for (int i = 1; i < price.length; i++) { 5 | if (price[i] > price[i - 1]) { 6 | profit += (price[i] - price[i - 1]) as int; 7 | } 8 | } 9 | 10 | return profit; 11 | } 12 | 13 | void main() { 14 | var price = [1, 5, 3, 8, 12]; 15 | print('Maximum Profit in Stock Market = ${maxProfit(price)}'); 16 | } 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_023)_Trapping_Rain_Water.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int getWater(arr) { 4 | int res = 0; 5 | 6 | for (int i = 1; i < arr.length - 1; i++) { 7 | int lMax = arr[i]; 8 | for (int j = 0; j < i; j++) lMax = max(lMax, arr[j]); 9 | int rMax = arr[i]; 10 | for (int j = i + 1; j < arr.length; j++) rMax = max(rMax, arr[j]); 11 | 12 | res += min(lMax, rMax) - arr[i] as int; 13 | } 14 | 15 | return res; 16 | } 17 | 18 | void main(List args) { 19 | var arr = [3, 0, 1, 2, 5]; 20 | print('Maximum fill boxes = ${getWater(arr)}'); 21 | } 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_025)_Maximum_Consecutive_ONEs_in_binary_Array.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxConsecutiveOnes(arr) { 4 | var res = 0; 5 | 6 | for (var i = 0; i < arr.length; i++) { 7 | var count = 0; 8 | for (int j = i; j < arr.length; j++) { 9 | if (arr[j] == 1) { 10 | count++; 11 | } else { 12 | break; 13 | } 14 | } 15 | res = max(res, count); 16 | } 17 | return res; 18 | } 19 | 20 | void main() { 21 | var arr = [0, 1, 1, 0, 1, 1, 1]; 22 | print('Max consecutive ones in binary array is ${maxConsecutiveOnes(arr)}.'); 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_026)_Maximum_Consecutive_ONEs_in_binary_Array.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxConsecutiveOnes(arr) { 4 | var res = 0; 5 | var count = 0; 6 | for (var i = 0; i < arr.length; i++) { 7 | if (arr[i] == 0) { 8 | count = 0; 9 | } else { 10 | count++; 11 | res = max(res, count); 12 | } 13 | } 14 | return max(res, count); 15 | } 16 | 17 | void main() { 18 | var arr = [0, 1, 1, 0, 1, 1, 1]; 19 | print('Max consecutive ones in binary array is ${maxConsecutiveOnes(arr)}.'); 20 | } 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_027)_Maximum_Subarray_Sum.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxSubArraySum(arr) { 4 | int res = arr[0]; 5 | 6 | for (int i = 0; i < arr.length; i++) { 7 | int curr = 0; 8 | for (int j = i; j < arr.length; j++) { 9 | curr += arr[j] as int; 10 | res = max(res, curr); 11 | } 12 | } 13 | return res; 14 | } 15 | 16 | void main() { 17 | var arr = [2, 3, -8, 7, -1, 2, 3]; 18 | print('Maximum sub-array sum is ${maxSubArraySum(arr)}.'); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_028)_Maximum_Subarray_Sum.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxSubArraySum(List arr) { 4 | int res = arr[0]; 5 | int maxEnding = arr[0]; 6 | 7 | for (int i = 1; i < arr.length; i++) { 8 | maxEnding = max(maxEnding + arr[i], arr[i]); 9 | res = max(res, maxEnding); 10 | } 11 | return res; 12 | } 13 | 14 | void main() { 15 | var arr = [2, 3, -8, 7, -1, 2, 3]; 16 | print('Maximum sub-array sum is ${maxSubArraySum(arr)}.'); 17 | } 18 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_029)_Maximum_Circular_Subarray_Sum.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxCircularSubArraySum(List arr) { 4 | int res = arr[0]; 5 | 6 | for (int i = 0; i < arr.length; i++) { 7 | int currMax = arr[i]; 8 | int currSum = arr[i]; 9 | 10 | for (int j = 1; j < arr.length; j++) { 11 | int index = (i + j) % arr.length; 12 | currSum += arr[index]; 13 | currMax = max(currMax, currSum); 14 | } 15 | res = max(res, currMax); 16 | } 17 | return res; 18 | } 19 | 20 | void main() { 21 | var arr = [5, -2, 3, 4]; 22 | print('Maximum circular sub-array sum is ${maxCircularSubArraySum(arr)}.'); 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_031)_Maximum_length_Even_Odd_Subarray.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxEvenOdd(List arr) { 4 | int res = 1; 5 | 6 | for (int i = 0; i < arr.length; i++) { 7 | int curr = 1; 8 | for (int j = i + 1; j < arr.length; j++) { 9 | if ((arr[j] % 2 == 0 && arr[j - 1] % 2 == 0) || 10 | (arr[j] % 2 != 0 && arr[j - 1] % 2 != 0)) { 11 | curr++; 12 | } else { 13 | break; 14 | } 15 | } 16 | res = max(res, curr); 17 | } 18 | 19 | return res; 20 | } 21 | 22 | void main() { 23 | var arr = [10, 12, 14, 7, 8]; 24 | print('Maximum length of Even Odd Subarray: ${maxEvenOdd(arr)}'); 25 | } 26 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_032)_Maximum_length_Even_Odd_Subarray.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxEvenOdd(List arr) { 4 | int res = 1; 5 | int curr = 1; 6 | 7 | for (int i = 1; i < arr.length; i++) { 8 | if ((arr[i] % 2 == 0 && arr[i - 1] % 2 != 0) || 9 | (arr[i] % 2 != 0 && arr[i - 1] % 2 == 0)) { 10 | curr++; 11 | res = max(res, curr); 12 | } else { 13 | curr = 1; 14 | } 15 | } 16 | 17 | return res; 18 | } 19 | 20 | void main() { 21 | var arr = [10, 12, 14, 7, 8]; 22 | print('Maximum length of Even Odd Subarray: ${maxEvenOdd(arr)}'); 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_033)_Majority_Element_in_Array.dart: -------------------------------------------------------------------------------- 1 | int majorityElement(List arr) { 2 | for (int i = 0; i < arr.length; i++) { 3 | int count = 0; 4 | for (int j = 0; j < arr.length; j++) { 5 | if (arr[i] == arr[j]) { 6 | count++; 7 | } 8 | } 9 | if (count > arr.length ~/ 2) { 10 | return arr[i]; 11 | } 12 | } 13 | return -1; 14 | } 15 | 16 | void main() { 17 | var arr = [2, 3, 4, 5, 5, 5, 5]; 18 | print('Majority Element in Array: ${majorityElement(arr)}'); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_034)_Majority_Element_in_Array.dart: -------------------------------------------------------------------------------- 1 | int majorityElementIndex(List arr) { 2 | int res = 0; 3 | int count = 1; 4 | 5 | for (int i = 1; i < arr.length; i++) { 6 | if (arr[res] == arr[i]) { 7 | count++; 8 | } else { 9 | count--; 10 | } 11 | 12 | if (count == 0) { 13 | res = i; 14 | count = 1; 15 | } 16 | } 17 | 18 | count = 0; 19 | for (int i = 0; i < arr.length; i++) { 20 | if (arr[res] == arr[i]) count++; 21 | } 22 | 23 | if (count <= arr.length ~/ 2) return -1; 24 | 25 | return res; 26 | } 27 | 28 | void main() { 29 | var arr = [2, 3, 4, 5, 5, 5, 5]; 30 | print('Index of Majority Element in Array: ${majorityElementIndex(arr)}'); 31 | } 32 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_035)_Minimum_Group_flips_to_make_same.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void minGroupFlipSame(List arr) { 4 | for (int i = 1; i < arr.length; i++) { 5 | if (arr[i] != arr[i - 1]) { 6 | if (arr[i] != arr[0]) 7 | stdout.write('From $i to '); 8 | else 9 | stdout.writeln(i - 1); 10 | } 11 | } 12 | 13 | if (arr[arr.length - 1] != arr[0]) stdout.writeln(arr.length - 1); 14 | } 15 | 16 | void main() { 17 | var arr = [1, 1, 0, 0, 0, 0, 0, 1, 1, 0]; 18 | minGroupFlipSame(arr); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_036)_Max_Sum_of_K_Consecutive_Element.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxSumOfKElements(List arr, int k) { 4 | int maxSum = 0; 5 | 6 | for (int i = 0; i + k - 1 < arr.length; i++) { 7 | int sum = 0; 8 | for (int j = 0; j < k; j++) { 9 | sum += arr[i + j]; 10 | } 11 | maxSum = max(maxSum, sum); 12 | } 13 | return maxSum; 14 | } 15 | 16 | void main() { 17 | var arr = [1, 8, 30, -5, 20, 7]; 18 | int k = 3; 19 | print( 20 | 'Maximum Sum of $k consecutive elements = ${maxSumOfKElements(arr, k)}'); 21 | } 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_037)_Max_Sum_of_K_Consecutive_Element.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | 3 | int maxSumOfKElements(List arr, int k) { 4 | int currMax = 0; 5 | for (int i = 0; i < k; i++) { 6 | currMax += arr[i]; 7 | } 8 | int maxSum = currMax; 9 | 10 | for (int i = k; i < arr.length; i++) { 11 | currMax += (arr[i] - arr[i - k]); 12 | maxSum = max(maxSum, currMax); 13 | } 14 | return maxSum; 15 | } 16 | 17 | void main() { 18 | var arr = [1, 8, 30, -5, 20, 7]; 19 | int k = 3; 20 | print( 21 | 'Maximum Sum of $k consecutive elements = ${maxSumOfKElements(arr, k)}'); 22 | } 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_038)_Find_SubArray_of_Given_Sum.dart: -------------------------------------------------------------------------------- 1 | void subArraySum(List arr, int reqSum) { 2 | for (int i = 0; i < arr.length; i++) { 3 | int sum = 0; 4 | for (int j = i; j < arr.length; j++) { 5 | sum += arr[j]; 6 | if (sum == reqSum) { 7 | print('Sum found between indexes $i to $j.'); 8 | return; 9 | } 10 | } 11 | } 12 | print('Sum not found.'); 13 | } 14 | 15 | void main() { 16 | var arr = [1, 4, 20, 3, 10, 5]; 17 | int sum = 23; 18 | subArraySum(arr, sum); 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_039)_Find_SubArray_of_Given_Sum.dart: -------------------------------------------------------------------------------- 1 | void subArraySum(List arr, int reqSum) { 2 | int sum = arr[0]; 3 | int start = 0; 4 | for (int end = 1; end < arr.length; end++) { 5 | while (sum > reqSum && start < end - 1) { 6 | sum -= arr[start]; 7 | start++; 8 | } 9 | if (sum == reqSum) { 10 | print("Subarray found."); 11 | print("Start: $start End: ${end - 1}"); 12 | return; 13 | } 14 | if (end < arr.length) sum += arr[end]; 15 | } 16 | 17 | print("Subarray not found."); 18 | } 19 | 20 | void main() { 21 | var arr = [1, 4, 20, 3, 10, 5]; 22 | int sum = 23; 23 | subArraySum(arr, sum); 24 | } 25 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_040)_Print_N_bonacci_Numbers.dart: -------------------------------------------------------------------------------- 1 | void printNbonacciNumbers(int n, int m) { 2 | var series = List.generate(m, (i) => 0); 3 | series[n - 1] = 1; 4 | series[n] = 1; 5 | for (int i = n + 1; i < m; i++) { 6 | series[i] = 2 * series[i - 1] - series[i - n - 1]; 7 | } 8 | print('N-bonacci Numbers = $series.'); 9 | } 10 | 11 | void main() { 12 | int n = 5; 13 | int m = 15; 14 | printNbonacciNumbers(n, m); 15 | } 16 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_041)_Get_Sum_on_Given_Queries.dart: -------------------------------------------------------------------------------- 1 | List preSum(List arr) { 2 | var perfixSum = List.filled(arr.length, 0); 3 | perfixSum[0] = arr[0]; 4 | 5 | for (int i = 1; i < arr.length; i++) { 6 | perfixSum[i] = perfixSum[i - 1] + arr[i]; 7 | } 8 | return perfixSum; 9 | } 10 | 11 | int getSum(List arr, int l, int r) { 12 | var perfixSum = preSum(arr); 13 | if (l != 0) { 14 | return perfixSum[r] - perfixSum[l - 1]; 15 | } 16 | return perfixSum[r]; 17 | } 18 | 19 | void main() { 20 | var arr = [2, 8, 3, 9, 6, 5, 4]; 21 | print('Sum of subarray index from 1 to 3 = ${getSum(arr, 1, 3)}'); 22 | print('Sum of subarray index from 0 to 2 = ${getSum(arr, 0, 2)}'); 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_042)_Find_Equilibrium_Point_in_an_Array.dart: -------------------------------------------------------------------------------- 1 | bool checkEquilibrium(List arr) { 2 | for (int i = 0; i < arr.length; i++) { 3 | int lSum = 0, rSum = 0; 4 | for (int j = 0; j < i; j++) lSum += arr[j]; 5 | 6 | for (int j = i + 1; j < arr.length; j++) rSum += arr[j]; 7 | 8 | if (lSum == rSum) return true; 9 | } 10 | return false; 11 | } 12 | 13 | void main() { 14 | var arr = [3, 4, 8, -9, 20, 6]; 15 | print('Equilibrium = ${checkEquilibrium(arr)}'); 16 | } 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_043)_Find_Equilibrium_Point_in_an_Array.dart: -------------------------------------------------------------------------------- 1 | bool checkEquilibrium(List arr) { 2 | int sum = 0; 3 | 4 | for (int i = 0; i < arr.length; i++) sum += arr[i]; 5 | 6 | int lSum = 0; 7 | 8 | for (int i = 0; i < arr.length; i++) { 9 | if (lSum == sum - arr[i]) return true; 10 | lSum += arr[i]; 11 | sum -= arr[i]; 12 | } 13 | 14 | return false; 15 | } 16 | 17 | void main() { 18 | var arr = [3, 4, 8, -9, 20, 6]; 19 | print('Equilibrium = ${checkEquilibrium(arr)}'); 20 | } 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_044)_In_Two_Array_Find_Max_Occuring_Element.dart: -------------------------------------------------------------------------------- 1 | int findMaxOccuringElement(List L, List R) { 2 | Map map = {}; 3 | for (int i = 0; i < L.length; i++) { 4 | if (map.containsKey(L[i])) 5 | map[L[i]]! + 1; 6 | else 7 | map[L[i]] = 1; 8 | } 9 | int max = 0; 10 | int maxElement = 0; 11 | for (int i = 0; i < R.length; i++) { 12 | if (map.containsKey(R[i])) { 13 | if (map[R[i]]! > max) { 14 | max = map[R[i]]!; 15 | maxElement = R[i]; 16 | } 17 | } 18 | } 19 | return maxElement; 20 | } 21 | 22 | void main() { 23 | var L = [1, 2, 3], R = [3, 5, 7]; 24 | print( 25 | 'Maximum Occuring Element in both Array = ${findMaxOccuringElement(L, R)}'); 26 | } 27 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_046)_Flat_Nested_Array.dart: -------------------------------------------------------------------------------- 1 | List flatten(arr) { 2 | var temp = []; 3 | process(arr, temp); 4 | return temp; 5 | } 6 | 7 | void process(arr, output) { 8 | for (var i = 0; i < arr.length; i++) { 9 | if (arr[i] is List) { 10 | process(arr[i], output); 11 | } else { 12 | output.add(arr[i]); 13 | } 14 | } 15 | } 16 | 17 | void main() { 18 | var arr = [ 19 | [2, 5, 8, 1], 20 | 10, 21 | [ 22 | [ 23 | 80, 24 | [45, 86, 98], 25 | 30 26 | ] 27 | ], 28 | 4, 29 | 0, 30 | 7, 31 | [ 32 | 12, 33 | [16, 25] 34 | ] 35 | ]; 36 | print('Before flatten array: ${arr}'); 37 | var temp = flatten(arr); 38 | print('After flatten array: ${temp}'); 39 | } 40 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Dart/_048)_Intersection_of_Arrays.dart: -------------------------------------------------------------------------------- 1 | List findArrayIntersection(List arr1, List arr2) { 2 | List result = []; 3 | 4 | int i = 0, j = 0; 5 | 6 | while (i < arr1.length && j < arr2.length) { 7 | if (arr1[i] == arr2[j]) { 8 | result.add(arr1[i]); 9 | i++; 10 | j++; 11 | } else if (arr1[i] < arr2[j]) { 12 | i++; 13 | } else { 14 | j++; 15 | } 16 | } 17 | 18 | return result; 19 | } 20 | 21 | void main() { 22 | var arr1 = [4, 2, 3, 1, 10, 12, 5, 20, 16]; 23 | var arr2 = [12, 5, 20, 2, 0, 1]; 24 | 25 | var result = findArrayIntersection(arr1, arr2); 26 | print(result); 27 | } 28 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Java/_03)_Duplicate_Array.java: -------------------------------------------------------------------------------- 1 | class FindDuplicate { 2 | // Function to print duplicates 3 | static void printRepeating(int arr[], int size) { 4 | int i; 5 | System.out.println("The repeating elements are : "); 6 | 7 | for (i = 0; i < size; i++) { 8 | int j = Math.abs(arr[i]); 9 | if (arr[j] >= 0) 10 | arr[j] = -arr[j]; 11 | else 12 | System.out.print(j + " "); 13 | } 14 | } 15 | 16 | public static void main(String[] args) { 17 | int arr[] = { 1, 2, 3, 1, 3, 6, 6 }; 18 | int arr_size = arr.length; 19 | printRepeating(arr, arr_size); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_001)_Search_Element.js: -------------------------------------------------------------------------------- 1 | // function to search for element in array 2 | function search(arr, x) { 3 | for (let index = 0; index < arr.length; index++) { 4 | if (arr[index] === x) { 5 | return index; 6 | } 7 | } 8 | //if no such number exist in the array return -1 9 | return -1; 10 | } 11 | 12 | const arr = [20, 5, 7, 25]; 13 | console.log(`Searched index= ${search(arr, 5)}`); 14 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_002)_Insert_Element.js: -------------------------------------------------------------------------------- 1 | function insert(arr, position, element) { 2 | // shift all elements one 3 | // place to the back until index 4 | for (i = arr.length; i > position; i--) { 5 | arr[i] = arr[i - 1]; 6 | } 7 | // insert the element at the index 8 | arr[position] = element; 9 | } 10 | 11 | let arr = [1, 2, 3, 5]; 12 | console.log(`before insertion -> ${arr}`); 13 | insert(arr, 1, -4); 14 | console.log(`After insertion -> ${arr}`); 15 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_003)_Delete_Element.js: -------------------------------------------------------------------------------- 1 | function deleteElement(arr, element) { 2 | if (arr.indexOf(element) === -1) { 3 | return 'no such element exist in array'; 4 | } 5 | let newArr = []; 6 | for (let i = 0; i < arr.length; i++) { 7 | if (i !== arr.indexOf(element)) { 8 | newArr.push(arr[i]); 9 | } 10 | } 11 | return newArr; 12 | } 13 | 14 | let arr = [1, 2, 3, 5]; 15 | 16 | console.log(arr); 17 | console.log(`before deletion -> ${arr}`); 18 | let deleteElemArr = deleteElement(arr, 25); 19 | console.log(`after deletion -> ${deleteElemArr}`); 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_004)_Reverse_Array.js: -------------------------------------------------------------------------------- 1 | function reverse(arr) { 2 | let low = 0; 3 | let high = arr.length - 1; 4 | while (low < high) { 5 | let temp = arr[low]; 6 | arr[low] = arr[high]; 7 | arr[high] = temp; 8 | 9 | low++; 10 | high--; 11 | } 12 | } 13 | 14 | let arr = [1, 2, 3, 4, 5]; 15 | 16 | console.log(`Befor Reverse -> ${arr}`); 17 | reverse(arr); 18 | console.log(`After Reverse -> ${arr}`); 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_005)_Left_Rotate_Array_by_1.js: -------------------------------------------------------------------------------- 1 | function lRotateOne(arr, n) { 2 | let temp = arr[0]; 3 | for (let i = 1; i < n; i++) { 4 | arr[i - 1] = arr[i]; 5 | } 6 | arr[n - 1] = temp; 7 | } 8 | 9 | let arr = [1, 2, 3, 4, 5]; 10 | let n = 5; 11 | console.log(`Before Left Rotation -> ${arr}`); 12 | lRotateOne(arr, n); 13 | console.log(`After '1' Left Rotation -> ${arr}`); 14 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_006)_Left_Rotate_Array_by_d_(Time_dN).js: -------------------------------------------------------------------------------- 1 | function lRotateOne(arr, n) { 2 | let temp = arr[0]; 3 | for (let i = 1; i < n; i++) { 4 | arr[i - 1] = arr[i]; 5 | } 6 | arr[n - 1] = temp; 7 | } 8 | function leftRotate(arr, d, n) { 9 | for (let i = 0; i < d; i++) { 10 | lRotateOne(arr, n); 11 | } 12 | } 13 | 14 | let arr = [1, 2, 3, 4, 5]; 15 | let n = 5; 16 | let d = 2; 17 | console.log(`Before Left Rotation->${arr}`); 18 | leftRotate(arr, d, n); 19 | console.log(`After '2' Left Rotation->${arr}`); 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_007)_Left_Rotate_Array_by_d_(Time_N).js: -------------------------------------------------------------------------------- 1 | function leftRotate(arr, d, n) { 2 | let temp = new Array(d); 3 | for (let i = 0; i < d; i++) { 4 | temp[i] = arr[i]; 5 | } 6 | for (let i = d; i < n; i++) { 7 | arr[i - d] = arr[i]; 8 | } 9 | for (let i = 0; i < d; i++) { 10 | arr[n - d + i] = temp[i]; 11 | } 12 | } 13 | 14 | let arr = [1, 2, 3, 4, 5]; 15 | let n = 5; 16 | let d = 2; 17 | console.log(`Before Left Rotation->${arr}`); 18 | leftRotate(arr, d, n); 19 | console.log(`After '2' Left Rotation->${arr}`); 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_008)_Left_Rotate_Array_by_d_(Time_N).js: -------------------------------------------------------------------------------- 1 | function reverse(arr, low, high) { 2 | while (low < high) { 3 | //swap(arr[high], arr[low]); 4 | [arr[high], arr[low]] = [arr[low], arr[high]]; 5 | low++; 6 | high--; 7 | } 8 | } 9 | function leftRotate(arr, d, n) { 10 | reverse(arr, 0, d - 1); 11 | reverse(arr, d, n - 1); 12 | reverse(arr, 0, n - 1); 13 | } 14 | 15 | let arr = [1, 2, 3, 4, 5]; 16 | let n = 5; 17 | let d = 4; 18 | console.log(`Before Left Rotation->${arr}`); 19 | leftRotate(arr, d, n); 20 | console.log(`After '2' Left Rotation->${arr}`); 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_009)_Remove_Duplicates_from_a_sorted_array.js: -------------------------------------------------------------------------------- 1 | function remDups(arr) { 2 | let n = arr.length + 1; 3 | let temp = []; 4 | for (let i = 0; i < n; i++) { 5 | if (arr[i] != arr[i + 1]) { 6 | temp.push(arr[i]); 7 | } 8 | } 9 | return temp; 10 | } 11 | 12 | let arr = [0, 20, 20, 30, 30, 30, 30]; 13 | console.log(`Before Removal Duplicates-> ${arr}`); 14 | 15 | let Res = remDups(arr); 16 | console.log(`After Removal Duplicates-> ${Res}`); 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_010)_Remove_Duplicates_from_a_sorted_array.js: -------------------------------------------------------------------------------- 1 | function remDups(arr) { 2 | let res = 1; 3 | for (let i = 1; i < arr.length; i++) { 4 | if (arr[i] != arr[res - 1]) { 5 | arr[res] = arr[i]; 6 | res++; 7 | } 8 | } 9 | return res; 10 | } 11 | 12 | let arr = [0, 20, 20, 30, 30, 30, 30]; 13 | console.log(`Before Removal Duplicates-> ${arr}`); 14 | 15 | let Res = remDups(arr); 16 | console.log(`After Removal Duplicates-> ${arr.splice(0, Res)}`); 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_011)_Largest_Number.js: -------------------------------------------------------------------------------- 1 | function getLargest(arr) { 2 | for (let i = 0; i < arr.length; i++) { 3 | let flag = true; 4 | for (let j = 1; j < arr.length; j++) { 5 | if (arr[j] > arr[i]) { 6 | flag = false; 7 | break; 8 | } 9 | } 10 | if (flag == true) return i; 11 | } 12 | return -1; 13 | } 14 | 15 | let arr = [10, 5, 20, 8]; 16 | let index = getLargest(arr); 17 | console.log(`Index = ${index} 18 | Largest Number in [${arr}] is ${arr[index]}`); 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_012)_Largest_Number.js: -------------------------------------------------------------------------------- 1 | function getLargest(arr) { 2 | let n = arr.length; 3 | let res = 0; 4 | for (let i = 1; i < n; i++) if (arr[i] > arr[res]) res = i; 5 | return res; 6 | } 7 | 8 | let arr = [10, 5, 20, 8]; 9 | 10 | let index = getLargest(arr); 11 | 12 | console.log(`Index = ${index} 13 | Largest Number in [${arr}] is ${arr[index]}`); 14 | 15 | let largestNumber = arr.reduce((a, b) => { 16 | return Math.max(a, b); 17 | }); 18 | 19 | console.log(`Largest Number in [${arr}] is ${largestNumber1}`); 20 | 21 | let largestNumber1 = arr.sort((a, b) => { 22 | return b - a; 23 | })[0]; 24 | console.log(`Largest Number in [${arr}] is ${largestNumber1}`); 25 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_013)_Second_Largest_Number.js: -------------------------------------------------------------------------------- 1 | function getLargest(arr) { 2 | let largest = 0; 3 | for (let i = 1; i < arr.length; i++) if (arr[i] > arr[largest]) largest = i; 4 | return largest; 5 | } 6 | 7 | function getSecondLargest(arr) { 8 | let largest = getLargest(arr); 9 | let res = -1; 10 | for (let i = 0; i < arr.length; i++) { 11 | if (arr[i] != arr[largest]) { 12 | if (res == -1) res = i; 13 | else if (arr[i] > arr[res]) res = i; 14 | } 15 | } 16 | return res; 17 | } 18 | 19 | let arr = [10, 5, 18, 20]; 20 | let index = getSecondLargest(arr); 21 | console.log(`Index = ${index} 22 | Second Largest Number in [${arr}] is ${arr[index]}`); 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_014)_Second_Largest_Number.js: -------------------------------------------------------------------------------- 1 | function getSecondLargest(arr) { 2 | let res = -1, 3 | largest = 0; 4 | for (let i = 1; i < arr.length; i++) { 5 | if (arr[i] > arr[largest]) { 6 | res = largest; 7 | largest = i; 8 | } else if (arr[i] != arr[largest]) { 9 | if (res == -1 || arr[i] > arr[res]) res = i; 10 | } 11 | } 12 | return res; 13 | } 14 | let arr = [10, 5, 18, 20]; 15 | let index = getSecondLargest(arr); 16 | console.log(`Index = ${index} 17 | Second Largest Number in [${arr}] is ${arr[index]}`); 18 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_015)_Move_All_ZEROs_to_End.js: -------------------------------------------------------------------------------- 1 | function moveToEnd(arr) { 2 | for (let i = 0; i < arr.length; i++) { 3 | if (arr[i] == 0) { 4 | for (let j = i + 1; j < arr.length; j++) { 5 | if (arr[j] != 0) { 6 | [arr[i], arr[j]] = [arr[j], arr[i]]; //swap the values 7 | break; 8 | } 9 | } 10 | } 11 | } 12 | } 13 | let arr = [0, 8, 5, 0, 0, 10, 0, 20]; 14 | console.log(`Before Move Zeros: ${arr}`); 15 | 16 | moveToEnd(arr); 17 | 18 | console.log(`After Move Zeros: ${arr}`); 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_016)_Move_All_ZEROs_to_End.js: -------------------------------------------------------------------------------- 1 | function moveToEnd(arr) { 2 | let count = 0; 3 | for (let i = 0; i < arr.length; i++) { 4 | if (arr[i] != 0) { 5 | [arr[i], arr[count]] = [arr[count], arr[i]]; 6 | /* 7 | //swaping the values 8 | let temp = arr[i]; 9 | arr[i] = arr[count]; 10 | arr[count] = temp; 11 | */ 12 | count++; 13 | } 14 | } 15 | } 16 | 17 | let arr = [0, 8, 5, 0, 0, 10, 0, 20]; 18 | console.log(`Before Move Zeros: ${arr}`); 19 | 20 | moveToEnd(arr); 21 | 22 | console.log(`After Move Zeros: ${arr}`); 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_017)_Leaders_in_an_Array.js: -------------------------------------------------------------------------------- 1 | //if current element/person value is greater than front element/person then current element is considered as a leader 2 | function leaders(arr) { 3 | let leaders = []; 4 | for (let i = 0; i < arr.length; i++) { 5 | let flag = false; 6 | for (let j = i + 1; j < arr.length; j++) { 7 | if (arr[i] <= arr[j]) { 8 | flag = true; 9 | break; 10 | } 11 | } 12 | if (flag == false) leaders.push(arr[i]); 13 | } 14 | return leaders; 15 | } 16 | 17 | let arr = [7, 10, 4, 3, 6, 5, 2]; 18 | console.log(`Leaders = ${leaders(arr)}`); 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_018)_Leaders_in_an_Array.js: -------------------------------------------------------------------------------- 1 | //if current element/person value is greater than front element/person then current element is considered as a leader 2 | function leaders(arr) { 3 | let n = arr.length; 4 | let cur_leader = arr[n - 1]; 5 | let leaders = [arr[n - 1]]; 6 | for (let i = n - 2; i >= 0; i--) { 7 | if (cur_leader < arr[i]) { 8 | cur_leader = arr[i]; 9 | leaders.push(cur_leader); 10 | } 11 | } 12 | return leaders; 13 | } 14 | 15 | let arr = [7, 10, 4, 3, 6, 5, 2]; 16 | console.log(`Leaders = ${leaders(arr)}`); 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_019)_Maximum_Difference.js: -------------------------------------------------------------------------------- 1 | function maxDiff(arr) { 2 | let n = arr.length; 3 | let res = arr[1] - arr[0]; 4 | for (let i = 0; i < n; i++) 5 | for (let j = i + 1; j < n; j++) res = Math.max(res, arr[j] - arr[i]); 6 | return res; 7 | } 8 | 9 | let arr = [2, 3, 10, 6, 4, 8, 1]; 10 | console.log(`Maximum Difference = ${maxDiff(arr)}`); 11 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_020)_Maximum_Difference.js: -------------------------------------------------------------------------------- 1 | function maxDiff(arr) { 2 | let n = arr.length; 3 | let res = arr[1] - arr[0], 4 | minVal = arr[0]; 5 | for (let i = 1; i < n; i++) { 6 | res = Math.max(res, arr[i] - minVal); 7 | minVal = Math.min(minVal, arr[i]); 8 | } 9 | return res; 10 | } 11 | 12 | let arr = [2, 3, 10, 6, 4, 8, 1]; 13 | console.log(`Maximum Difference = ${maxDiff(arr)}`); 14 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_021_i_)_Best_Time_to_Buy_and_Sell_Stock.js: -------------------------------------------------------------------------------- 1 | function maxProfit(price, start, end) { 2 | if (end <= start) return 0; 3 | let profit = 0; 4 | for (let i = start; i < end; i++) { 5 | for (let j = i + 1; j <= end; j++) { 6 | if (price[j] > price[i]) { 7 | let cur_profit = 8 | price[j] - 9 | price[i] + 10 | maxProfit(price, start, i - 1) + 11 | maxProfit(price, j + 1, end); 12 | profit = Math.max(profit, cur_profit); 13 | } 14 | } 15 | } 16 | return profit; 17 | } 18 | 19 | let price = [1, 5, 3, 8, 12]; 20 | let start = 0; 21 | let end = 5; 22 | console.log(`Maximum Profit in Stock Market = ${maxProfit(price, start, end)}`); 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_021_ii_)_Best_Time_to_Buy_and_Sell_Stock.js: -------------------------------------------------------------------------------- 1 | function maxProfit(price) { 2 | let profit = 0; 3 | for (let i = 1; i < price.length; i++) 4 | if (price[i] > price[i - 1]) profit += price[i] - price[i - 1]; 5 | return profit; 6 | } 7 | 8 | let price = [1, 5, 3, 8, 12]; 9 | console.log(`Maximum Profit in Stock Market = ${maxProfit(price)}`); 10 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/JavaScript/_021_iii_)_Best_Time_to_Buy_and_Sell_Stock.js: -------------------------------------------------------------------------------- 1 | var maxProfit = function(prices) { 2 | let minPrice = Number.MAX_VALUE; 3 | let maxProfit = 0; 4 | for(let i = 0; i < prices.length; i++) { 5 | if(prices[i] < minPrice) { 6 | minPrice = prices[i]; 7 | } 8 | else if (prices[i] - minPrice > maxProfit) { 9 | maxProfit = prices[i] - minPrice; 10 | } 11 | } 12 | return maxProfit; 13 | }; 14 | 15 | console.log(maxProfit([7,1,5,3,6,4])); -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Python/_001)_Search_Element.py: -------------------------------------------------------------------------------- 1 | # function to perform linear search for element in array 2 | def search(arr, element): 3 | for index in range(0, len(arr)): 4 | if arr[index] == element: 5 | return index 6 | 7 | # if no such number exist in the array return -1 8 | return -1 9 | 10 | 11 | arr = [20, 5, 7, 25] 12 | element = 5 13 | 14 | print("Array Searched:", arr) 15 | print("Searching for:", element) 16 | print("Searched index=", search(arr, element)) 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Python/_002)_Insert_Element.py: -------------------------------------------------------------------------------- 1 | """ 2 | Function to insert a new element at a given position 3 | 4 | Takes the position of an array and an element as arguments 5 | """ 6 | 7 | arr = [1, 2, 3, 5] 8 | 9 | def insert(arr, index, successor): 10 | for index in range(index, len(arr)): 11 | arr[index], successor = successor, arr[index] 12 | 13 | arr.append(successor) 14 | 15 | print("Array before insertion:", arr) 16 | # OUTPUT: [1, 2, 3, 5] 17 | insert(arr, 3, 4) 18 | print("Array after insertion:", arr) 19 | # OUTPUT: [1, 2, 3, 4, 5] 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Python/_003)_Delete_Element.py: -------------------------------------------------------------------------------- 1 | """ 2 | Function to delete an element in an array 3 | 4 | Takes in an array and an element to delete 5 | 6 | Returns an new Array 7 | """ 8 | 9 | 10 | def delete(arr, element): 11 | if element not in arr: 12 | return "no such element exist in array" 13 | 14 | new_arr = [] 15 | 16 | for index in range(len(arr)): 17 | if arr[index] != element: 18 | new_arr.append(arr[index]) 19 | 20 | return new_arr 21 | 22 | 23 | arr = [1, 2, 3, 5] 24 | element = 2 25 | 26 | print("Before deletion:", arr) 27 | print("Deleting:", element) 28 | 29 | new_arr = delete(arr, element) 30 | print("After deletion:", new_arr) 31 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Python/_004)_Reverse_Array.py: -------------------------------------------------------------------------------- 1 | """ 2 | Function to Reverse an Array 3 | 4 | Takes in an Array and Returns an New Array 5 | """ 6 | 7 | 8 | def reverse(arr): 9 | new_arr = [] 10 | 11 | for index in range(len(arr)): 12 | new_arr.insert(0, arr[index]) 13 | 14 | return new_arr 15 | 16 | 17 | arr = [1, 2, 3, 4, 5] 18 | new_arr = reverse(arr) 19 | 20 | print("Before Reverse:", arr) 21 | print("After Reverse:", new_arr) 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Python/_005)_Left_Rotate_Array_by_1.py: -------------------------------------------------------------------------------- 1 | """ 2 | Function to rotate an the first element 3 | """ 4 | 5 | 6 | def left_rotate(arr, num): 7 | tmp = arr[0] 8 | 9 | for index in range(1, num): 10 | arr[index - 1] = arr[index] 11 | 12 | arr[num - 1] = tmp 13 | 14 | 15 | arr = [1, 2, 3, 4, 5] 16 | num = 2 17 | 18 | print("Before Left Rotation:", arr) 19 | 20 | left_rotate(arr, num) 21 | print("After '1' Left Rotation:", arr) 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/01]. Array/Python/_007)_Right_Rotate_Array_by_d_places.py: -------------------------------------------------------------------------------- 1 | #optimized method using reverse function 2 | def reverse(lst, l, r): 3 | while(l 2 | using namespace std; 3 | 4 | const int R=4, C=4; 5 | void printSnake(int mat[R][C]) 6 | { 7 | for(int i=0; i=0; j--) 18 | cout< 2 | using namespace std; 3 | 4 | const int n=4; 5 | void Transpose(int mat[n][n]) 6 | { 7 | for(int i=0; i 2 | using namespace std; 3 | 4 | int maxCuts(int n, int a, int b, int c) 5 | { 6 | if(n == 0) 7 | return 0; 8 | if(n <= -1) 9 | return -1; 10 | int res = max(maxCuts(n-a, a, b, c), 11 | max(maxCuts(n-b, a, b, c), 12 | maxCuts(n-c, a, b, c))); 13 | if(res == -1) 14 | return -1; 15 | return res + 1; 16 | } 17 | 18 | int main() 19 | { 20 | int n = 5, a = 2, b = 1, c = 5; 21 | cout<<"Maximum cutting Rod: "< 2 | using namespace std; 3 | 4 | int fun(int n) 5 | { 6 | if(n < 10) 7 | return n; 8 | return fun(n / 10) + n % 10; 9 | } 10 | 11 | int main() 12 | { 13 | cout<<"Sum of Digits = "< 2 | using namespace std; 3 | 4 | void ToH(int n, char A, char B, char C) 5 | { 6 | if (n == 1) 7 | { 8 | cout<<"Move 1 from " << A << " to " << C << endl; 9 | return; 10 | } 11 | ToH(n-1, A, C, B); 12 | cout<<"Move " << n << " from " << A << " to " << C << endl; 13 | ToH(n-1, B, A, C); 14 | } 15 | 16 | int main() 17 | { 18 | int n = 3; 19 | ToH(n, 'A', 'B', 'C'); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/C++/_5)_Josephus_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int jos(int n, int k) 5 | { 6 | if(n == 1) 7 | return 0; 8 | else 9 | return (jos(n - 1, k) + k) % n; 10 | } 11 | 12 | int myJos(int n, int k) 13 | { 14 | return jos(n, k) + 1; 15 | } 16 | 17 | int main() 18 | { 19 | cout<<"Last Remain person index(start index at 1) after every 3rd person killed in 5 persons = "< 2 | using namespace std; 3 | 4 | int countSubsets(int arr[], int n, int sum) 5 | { 6 | if(n==0) 7 | return sum==0? 1 : 0; 8 | return countSubsets(arr, n-1, sum) + countSubsets(arr, n-1, sum - arr[n-1]); 9 | } 10 | 11 | int main() 12 | { 13 | int n = 3, arr[]= {10, 20, 15}, sum = 25; 14 | cout<<"Number of subarray i.e sum(25) = "< 2 | using namespace std; 3 | int count_subsequences(string a,string b, int m, int n){ 4 | //Base Case 5 | //1. we have found some subsequence 6 | if((m==-1&&n==-1)||n==-1) 7 | return 1; 8 | //2. Source string empty 9 | if(m==-1) 10 | return 0; 11 | //Recursive 12 | if(a[m]==b[n]){ 13 | return count_subsequences(a,b,m,n-1)+count_subsequences(a,b,m-1,n); 14 | } 15 | return count_subsequences(a,b,m-1,n); 16 | } 17 | int main(){ 18 | string a,b; 19 | cin>>a>>b; 20 | cout< int: 12 | if (n <= 2): 13 | return n 14 | elif self.ways[n] != 0: 15 | return self.ways[n] 16 | else: 17 | self.ways[n] = self.climbStairs(n - 1) + self.climbStairs(n - 2) 18 | return self.ways[n] 19 | 20 | # Time Complexity: O(N) 21 | # Space Complexity: O(N) -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/Count_Digits.py: -------------------------------------------------------------------------------- 1 | # Given an integer N, count number of its digits. 2 | def countDigits(N): 3 | if N == 0: 4 | return 1 5 | if N < 0: 6 | N = -N 7 | if N >= 10: 8 | N //= 10 9 | return countDigits(N) + 1 10 | else: 11 | return 1 12 | 13 | print(countDigits(12688)) 14 | # OUTPUT: 5 15 | print(countDigits(375)) 16 | # OUTPUT: 3 17 | print(countDigits(64)) 18 | # OUTPUT: 2 19 | print(countDigits(-3459)) 20 | # OUTPUT: 4 21 | 22 | # Time Complexity: O(logN) (Or O(log10(N)) to be exact 23 | # Space Complexity: O(1) -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/Fibonacci_Series.py: -------------------------------------------------------------------------------- 1 | # To print the fibonacci series 2 | #0 1 1 2 3 5 8 3 | 4 | def fib(n): 5 | if n==0 or n==1: 6 | return n 7 | else: 8 | return fib(n-2)+fib(n-1) 9 | for i in range(6): 10 | print(fib(i), end = " ") 11 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/GCD.py: -------------------------------------------------------------------------------- 1 | #finding gcd of 2 numbers recursively 2 | def gcd(a,b): 3 | if b==0: 4 | return a 5 | else: 6 | return gcd(b, a%b) 7 | 8 | a = 10 9 | b = 4 10 | if a>b: 11 | a,b = b,a 12 | print(gcd(a,b)) -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/Pow(x,n).py: -------------------------------------------------------------------------------- 1 | # Implement pow(x, n), which calculates x raised to the power n (i.e., x^n). 2 | def myPow(x: float, n: int): 3 | 4 | if (n == 0): 5 | return 1 6 | elif (x == 0): 7 | return 0 8 | 9 | else: 10 | if (n < 0): 11 | return 1 / myPow(x, -n) 12 | else: 13 | temp = myPow(x, n//2) 14 | if (n % 2 == 0): 15 | return temp * temp 16 | else: 17 | return temp * temp * x 18 | 19 | print(myPow(2.00000, 10)) 20 | # OUTPUT: 1024.0 21 | print(myPow(2.10000, 3)) 22 | # OUTPUT: 9.261000000000001 23 | print(myPow(2.00000, -2)) 24 | # OUTPUT: 0.25 25 | 26 | # Time Complexity: O(logN) 27 | # Space Complexity: O(1) -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/Reverse_String.py: -------------------------------------------------------------------------------- 1 | # Write a function that reverses a string. The input string is given as an array of characters s. 2 | def reverseString(s) -> None: 3 | 4 | left = 0 5 | right = len(s) - 1 6 | 7 | def recursiveReverse(l, r): 8 | 9 | if l == r or l > r: 10 | return 11 | 12 | s[l], s[r] = s[r], s[l] 13 | return recursiveReverse(l + 1, r - 1) 14 | recursiveReverse(left, right) 15 | return s 16 | 17 | print(reverseString(["h","e","l","l","o"])) 18 | # OUTPUT: ['o', 'l', 'l', 'e', 'h'] 19 | print(reverseString(["H","a","n","n","a","h"])) 20 | # OUTPUT: ['h', 'a', 'n', 'n', 'a', 'H'] 21 | 22 | # Time Complexity: O(n) 23 | # Space Complexity: O(N) to maintain the recursion stack -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/factorial.py: -------------------------------------------------------------------------------- 1 | # Calculating the factorial of an given argument "n" 2 | # Mathematical formula: 3 | # n! = 1.2.3...(n-2).(n-1).n 4 | def factorial(n): 5 | if n <= 1: 6 | return 1 7 | 8 | return n*factorial(n-1) 9 | 10 | print(factorial(5)) 11 | # OUTPUT: 120 12 | print(factorial(0)) 13 | # OUTPUT 1 14 | print(factorial(1)) 15 | # OUTPUT:1 16 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/03]. Recursion/Python/palindrome_(ii).py: -------------------------------------------------------------------------------- 1 | #checking a palindrome or not using recursion 2 | # ex: "peep" 3 | 4 | def palindrome(s,b,e): 5 | if b>e: 6 | return True 7 | if s[b]!=s[e]: 8 | return False 9 | else: 10 | return palindrome(s, b+1, e-1) 11 | 12 | s = "pop" 13 | print(palindrome(s, 0, len(s)-1)) 14 | 15 | s = "pick" 16 | print(palindrome(s, 0, len(s)-1)) 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/04]. Strings/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/04]. Strings/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/04]. Strings/C++/_01)_Longest-Common-prefix.cpp: -------------------------------------------------------------------------------- 1 | string longestCommonPrefix(vector& strs) { 2 | 3 | //for string the final answer 4 | string ans = ""; 5 | 6 | if(strs.size() == 0) 7 | return ""; 8 | 9 | //It will return the minimum element 10 | string s = *min_element(strs.begin(), strs.end()); 11 | 12 | for(int i=0; i 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data=x; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | Node *insertBegin(Node *head,int x){ 14 | Node *temp=new Node(x); 15 | temp->next=head; 16 | return temp; 17 | 18 | } 19 | 20 | void printlist(Node *head){ 21 | Node *curr=head; 22 | while(curr!=NULL){ 23 | cout<data<<" "; 24 | curr=curr->next; 25 | } 26 | } 27 | int main() 28 | { 29 | Node *head=NULL; 30 | head=insertBegin(head,30); 31 | head=insertBegin(head,20); 32 | head=insertBegin(head,10); 33 | printlist(head); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/C++/_03)_Delete-Node-in-Linked-List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | 13 | node->val=node->next->val; 14 | node->next=node->next->next; 15 | 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/C++/_04)_Intersection-Of-Two-Linked-List.cpp: -------------------------------------------------------------------------------- 1 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 2 | 3 | ListNode * pA = headA; 4 | ListNode * pB = headB; 5 | 6 | while (pA != pB) { 7 | 8 | pA = pA == nullptr ? headB : pA -> next; 9 | pB = pB == nullptr ? headA : pB -> next; 10 | 11 | } 12 | 13 | return pA; 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/C++/_07)_Middle-of-the-Linked-List.cpp: -------------------------------------------------------------------------------- 1 | ListNode* middleNode(ListNode* head) { 2 | 3 | if(head == NULL) return NULL; 4 | 5 | ListNode *slow = head; 6 | ListNode *fast = head; 7 | 8 | while(fast != NULL && fast->next != NULL){ 9 | 10 | slow = slow -> next; 11 | fast = fast -> next -> next; 12 | 13 | } 14 | 15 | return slow; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/C++/_08)_Reverse-Linked-List.cpp: -------------------------------------------------------------------------------- 1 | ListNode* reverseList(ListNode* head) { 2 | ListNode* curr = head; 3 | ListNode* prev = NULL; 4 | 5 | while(curr != NULL){ 6 | ListNode* n = curr->next; 7 | curr->next = prev; 8 | 9 | prev = curr; 10 | curr = n; 11 | } 12 | return prev; 13 | } 14 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/C++/_09)_SortList.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* sortList(ListNode* head) { 4 | if(head == NULL) 5 | return NULL; 6 | else if(head->next == NULL) 7 | return head; 8 | else{ 9 | vector v; 10 | ListNode *result = head; 11 | while(result != NULL){ 12 | v.push_back(result->val); 13 | result = result->next; 14 | } 15 | sort(v.begin(),v.end()); 16 | result = head; 17 | for(int i=0;ival = v.at(i); 19 | result = result->next; 20 | } 21 | return head; 22 | } 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/06]. Linked List/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/06]. Linked List/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/06]. Linked List/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/07]. Doubly Linked List/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/07]. Doubly Linked List/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/07]. Doubly Linked List/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/07]. Doubly Linked List/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/07]. Doubly Linked List/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/07]. Doubly Linked List/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/08]. Circular Linked List/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/08]. Circular Linked List/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/08]. Circular Linked List/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/08]. Circular Linked List/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/08]. Circular Linked List/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/08]. Circular Linked List/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/09]. Stack/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/09]. Stack/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/09]. Stack/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/09]. Stack/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/09]. Stack/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/09]. Stack/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/10]. Queue/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/10]. Queue/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/10]. Queue/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/10]. Queue/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/10]. Queue/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/10]. Queue/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/11]. Deueue/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/11]. Deueue/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/11]. Deueue/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/11]. Deueue/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/11]. Deueue/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/11]. Deueue/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/12]. Tree/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/12]. Tree/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/12]. Tree/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/1]. Data Structures/12]. Tree/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/12]. Tree/Python/Add Contents.txt: -------------------------------------------------------------------------------- 1 | Added: 2 | 1. Binary Tree Traversals: 3 | a. Inorder 4 | b. Preorder 5 | c. Postorder 6 | 7 | 2. Binary Tree Height Calculation 8 | 9 | 3. Binary Tree Level Order Traversal 10 | 11 | 4. Symmetric Tree 12 | 13 | 5. Path Sum -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/12]. Tree/Python/Binary_Tree_Height.py: -------------------------------------------------------------------------------- 1 | # Binary Tree Height Calculation 2 | 3 | class Node: 4 | def __init__(self, key): 5 | self.val = key 6 | self.left = None 7 | self.right = None 8 | 9 | class BinaryTree: 10 | def height(self, root): 11 | if root == None: 12 | return 0 13 | else: 14 | return max(self.height(root.left), self.height(root.right)) + 1 15 | 16 | 17 | t = Node(20) 18 | t.left = Node(10) 19 | t.right = Node(30) 20 | t.left.left = Node(5) 21 | t.right.right = Node(35) 22 | b = BinaryTree() 23 | 24 | #Height of Binary Tree 25 | print(b.height(t)) -------------------------------------------------------------------------------- /1]. DSA + CP/1]. Data Structures/13]. Binary Search Tree/C++/_02)_Ceilingin_BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printCeiling(int arr[], int n){ 5 | cout<<"-1"<<" "; 6 | set s; 7 | s.insert(arr[0]); 8 | for(int i=1;i 2 | using namespace std; 3 | int binarySearch (int arr[], int n, int key) 4 | { 5 | int s = 0, e = n - 1; 6 | while (s <= e) 7 | { 8 | int mid = s + (e - s) / 2; 9 | if (arr[mid] == key) 10 | { 11 | return mid; 12 | } 13 | if (key > arr[mid]) 14 | { 15 | s = mid + 1; 16 | } 17 | else 18 | { 19 | e = mid - 1; 20 | } 21 | mid = s + (e - s) / 2; 22 | } 23 | return -1; 24 | } 25 | 26 | int main () 27 | { 28 | int arr[] = { 1, 2, 4, 5, 7, 9, 11 }; 29 | int n = 7; 30 | int ans = binarySearch (arr, 7, 2); 31 | if (ans == -1) 32 | cout << "Not Found "; 33 | else 34 | cout << " Element found at index " << ans; 35 | } 36 | -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/01]. Searching Algorithms/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/01]. Searching Algorithms/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/02]. Sorting Algorithms/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/02]. Sorting Algorithms/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/02]. Sorting Algorithms/C++/_3_)_Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(vector& a, int n) 5 | { 6 | for(int i=1;ia[j+1]) swap(a[j],a[j+1]); 9 | } 10 | 11 | 12 | int main(){ 13 | int n; 14 | cin>>n; 15 | int a; 16 | vector arr; 17 | for(int i=0;i>a; 19 | arr.push_back(a); 20 | } 21 | 22 | bubbleSort(arr,n); 23 | 24 | for(auto &i : arr) 25 | cout< 2 | using namespace std; 3 | 4 | void insertionSort(int n, vector &a){ 5 | int i,j; 6 | for(i=1;i=0;j--){ 9 | if(a[j]>temp){ 10 | a[j+1]=a[j]; 11 | }else break; 12 | } 13 | a[j+1]=temp; 14 | } 15 | } 16 | 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | int a; 21 | vector arr; 22 | for(int i=0;i>a; 24 | arr.push_back(a); 25 | } 26 | 27 | insertionSort(n,arr); 28 | 29 | for(auto &i : arr) 30 | cout< 2 | using namespace std; 3 | 4 | void selectionSort(vector& arr, int n) 5 | { 6 | for(int i=0;i>n; 21 | int a; 22 | vector arr; 23 | for(int i=0;i>a; 25 | arr.push_back(a); 26 | } 27 | 28 | selectionSort(arr,n); 29 | 30 | for(auto &i : arr) 31 | cout< a[j + 1]: 5 | a[j], a[j + 1] = a[j + 1], a[j] 6 | print(a) -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/02]. Sorting Algorithms/Python/Count_Sort.py: -------------------------------------------------------------------------------- 1 | #counting sort algorithm for positive numbers 2 | 3 | def countsort(lst): 4 | n = len(lst) 5 | ans = [0]*(n) 6 | for i in range(n): 7 | ans[lst[i]] += 1 8 | 9 | for i in range(n): 10 | while(ans[i]>0): 11 | print(i, end = ' ') 12 | ans[i] -= 1 13 | 14 | lst = [2,1,2,2,1,3,4,0,1,3] 15 | countsort(lst) -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/02]. Sorting Algorithms/Python/Insertion_Sort.py: -------------------------------------------------------------------------------- 1 | def sort(a): 2 | for j in range(1, len(a)): 3 | key = a[j] 4 | i = j - 1 5 | while i > -1 and a[i] > key: 6 | a[i + 1] = a[i] 7 | i = i - 1 8 | a[i + 1] = key 9 | return a 10 | 11 | if __name__ == '__main__': 12 | a = [5, 4, 8, 3, 4, 14, 90, 45, 9, 3, 2, 4] 13 | print(sort(a)) -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/02]. Sorting Algorithms/Python/Merge_Sort.py: -------------------------------------------------------------------------------- 1 | def divide(arr): 2 | if len(arr)>1: 3 | a = arr[:len(arr)//2] 4 | b = arr[len(arr)//2:] 5 | divide(a) 6 | divide(b) 7 | i = j = k = 0 8 | while(i 2 | using namespace std; 3 | 4 | int getbit(int n, int i) 5 | { 6 | int mask = 1 << i; 7 | int bit = (n & mask) > 0 ? 1 : 0; 8 | return bit; 9 | } 10 | int main() 11 | { 12 | int n, i; 13 | cout << "Enter The Number: "; 14 | cin >> n; 15 | cout << "Enter the value of i: "; 16 | cin >> i; 17 | cout << getbit(n, i) << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/C++/_003)_Decimal_to_Binary.cpp: -------------------------------------------------------------------------------- 1 | // Convert a decimal number to its binary form 2 | 3 | #include 4 | using namespace std; 5 | 6 | int DecimalToBinary(int n) 7 | { 8 | int ans = 0, p = 1; 9 | while (n > 0) 10 | { 11 | int last_bit = (n & 1); 12 | ans += p * last_bit; 13 | 14 | p = p * 10; 15 | n = n >> 1; 16 | } 17 | return ans; 18 | } 19 | 20 | int main() 21 | { 22 | int n; 23 | cout << "Enter n: "; 24 | cin >> n; 25 | 26 | cout << DecimalToBinary(n) << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/C++/_004)_Clear_ith_bit.cpp: -------------------------------------------------------------------------------- 1 | // Clear ith bit of a number 2 | 3 | #include 4 | using namespace std; 5 | 6 | void ClearBit(int &n, int i) // value of n is passed by refrence so we dont have to return its value 7 | { 8 | int mask = ~(1 << i); 9 | n = n & mask; 10 | } 11 | 12 | int main() 13 | { 14 | int n = 7, i = 2; 15 | 16 | ClearBit(n, i); 17 | cout << n; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/C++/_005)_If_A_Number_Is_Power_Of_Two.cpp: -------------------------------------------------------------------------------- 1 | // All power of two numbers has only a one-bit set. So count the no. of set bits and if you get 1 then the number is a power of 2. 2 | 3 | #include 4 | using namespace std; 5 | 6 | int CountBits(int n) 7 | { 8 | int c = 0; 9 | 10 | while (n > 0) 11 | { 12 | n = n & (n - 1); 13 | c++; 14 | } 15 | return c; 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cout << "Enter the number: "; 22 | cin >> n; 23 | 24 | if (CountBits(n) == 1) 25 | { 26 | cout << "Power of 2" << endl; 27 | } 28 | else 29 | { 30 | cout << "Not a power of 2" << endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/C++/_008_i_)_Count_Set_Bits_In_An_Integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cout << "Enter the number: "; 8 | cin >> n; 9 | 10 | // Method-1 (Inbuilt function in STL) 11 | cout << __builtin_popcount(n) << endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/C++/_008_ii_)_Count_Set_Bits_In_An_Integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Countbits(int n) 5 | { 6 | int c = 0; 7 | 8 | while (n > 0) 9 | { 10 | c = c + (n & 1); 11 | n = n >> 1; 12 | } 13 | 14 | return c; 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cout << "Enter the number: "; 21 | cin >> n; 22 | 23 | // Method-2 24 | cout << Countbits(n) << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/C++/_008_iii_)_Count_Set_Bits_In_An_Integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int CountbitsFast(int n) 5 | { 6 | int c = 0; 7 | 8 | while (n > 0) 9 | { 10 | n = n & (n - 1); 11 | c++; 12 | } 13 | 14 | return c; 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cout << "Enter the number: "; 21 | cin >> n; 22 | 23 | // Method- 3 (Brian Kernighan’s Algorithm) 24 | cout << CountbitsFast(n) << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/03]. Bitwise Algorithms/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/04]. Mathematical Algorithms/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/04]. Mathematical Algorithms/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/04]. Mathematical Algorithms/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/04]. Mathematical Algorithms/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/04]. Mathematical Algorithms/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/04]. Mathematical Algorithms/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/05]. Geometric Algorithms/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/05]. Geometric Algorithms/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/05]. Geometric Algorithms/Java/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/05]. Geometric Algorithms/Java/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/05]. Geometric Algorithms/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/05]. Geometric Algorithms/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/06]. Greedy/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/06]. Greedy/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/06]. Greedy/Python/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/06]. Greedy/Python/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/07]. Dynamic Programming/C++/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/2]. Algorithms/07]. Dynamic Programming/C++/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/2]. Algorithms/07]. Dynamic Programming/C++/_1)_Minimum Steps to reach the end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int min_steps(int *a,int n,vectordp,int i){ 4 | if(i==n-1) 5 | return 0; 6 | if(i>=n) 7 | return INT_MAX; 8 | int max_jump=a[i]; 9 | int steps=INT_MAX; 10 | for(int jump=1;jump<=max_jump;jump++){ 11 | int nextcell=i+jump; 12 | int subproblem=min_steps(a,n,dp,nextcell); 13 | if(subproblem!=INT_MAX){ 14 | steps=min(steps,subproblem+1); 15 | } 16 | } 17 | return dp[n]=steps; 18 | } 19 | int main(){ 20 | int n; 21 | cin>>n; 22 | int a[n]; 23 | for(int i=0;i>a[i]; 25 | vectordp(n+1,0); 26 | cout< 2 | using namespace std; 3 | int max_profit(int n,int prices[],int i,int j,int y,int dp[100][100]){ 4 | //base case 5 | if(i>j){ 6 | return 0; 7 | } 8 | if(dp[i][j]!=0){ 9 | return dp[i][j]; 10 | } 11 | //recursive case 12 | int op1,op2; 13 | op1=prices[i]*y+max_profit(n,prices,i+1,j,y+1,dp); 14 | op2=prices[j]*y+max_profit(n,prices,i,j-1,y+1,dp); 15 | int ans=max(op1,op2); 16 | return dp[i][j]=ans; 17 | } 18 | int main(){ 19 | int n; 20 | cin>>n; 21 | int prices[n]; 22 | for(int i=0;i>prices[i]; 24 | } 25 | int dp[100][100]={0}; 26 | cout< 2 | using namespace std; 3 | int ladder_problem_Bottom_up_optimised(int n,int k){ 4 | int dp[n+1]={0}; 5 | dp[0]=dp[1]=1; 6 | for(int i=2;i<=n;i++){ 7 | dp[i]=2*dp[i-1]; 8 | } 9 | for(int i=2;i<=n;i++){ 10 | if(i-(k+1)>=0) 11 | dp[i]=2*dp[i-1] - dp[i-(k+1)]; 12 | } 13 | return dp[n]; 14 | } 15 | int main(){ 16 | int n,k; 17 | cin>>n>>k; 18 | int ans=ladder_problem_Bottom_up_optimised(n,k); 19 | cout< 2 | using namespace std; 3 | int No_of_ways(int n){ 4 | int dp[n+1]={0}; 5 | dp[0]=dp[1]=1; 6 | for(int i=2;i<=n;i++){ 7 | dp[i]=(i-1)*dp[i-2]+dp[i-1]; 8 | } 9 | return dp[n]; 10 | } 11 | int main(){ 12 | int n; 13 | cin>>n; 14 | int ans=No_of_ways(n); 15 | cout< 11 | using namespace std; 12 | 13 | string reverseWord(string str){ 14 | //Your code here 15 | int low=0, high=str.length()-1; 16 | 17 | while(low < high) 18 | { 19 | swap(str[low], str[high]); 20 | low++; 21 | high--; 22 | } 23 | return str; 24 | } 25 | 26 | int main() { 27 | int t; 28 | cin>>t; 29 | while(t--) 30 | { 31 | string s; 32 | cin >> s; 33 | cout << reverseWord(s) << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/01]. Array/_03)_2_Kth_Max_Min_Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // arr : given array 4 | // l : starting index of the array i.e 0 5 | // r : ending index of the array i.e size-1 6 | // k : find kth smallest element and return using this function 7 | int kthSmallest(int arr[], int l, int n, int k) { 8 | sort(arr,arr+n+1); 9 | return arr[k-1]; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/01]. Array/_05)_Move_All_Negative_Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void MoveNegative(int arr[], int arr_size) 5 | { 6 | int j=0; 7 | for(int i=0; i> merge(vector>& intervals) { 4 | sort(intervals.begin(), intervals.end()); 5 | vector> merged; 6 | for (auto interval : intervals) { 7 | if (merged.empty() || merged.back()[1] < interval[0]) { 8 | merged.push_back(interval); 9 | } 10 | else { 11 | merged.back()[1] = max(merged.back()[1], interval[1]); 12 | } 13 | } 14 | return merged; 15 | } 16 | }; -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/01]. Array/_15)_Next_Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | if(nums.size() == 1) return; 5 | int i = nums.size()-2,j = i+1; 6 | while(i >= 0 && nums[i] >= nums[i+1] ) i--; 7 | if(i >= 0){ 8 | while(i>= 0 && nums[i] >= nums[j]) j--; 9 | swap(nums[i],nums[j]); 10 | } 11 | reverse(nums.begin()+i+1,nums.end()); 12 | } 13 | }; -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/01]. Array/_17)_Best_Time_to_Buy_and_Sell_Stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) 4 | { 5 | int buy=prices[0],ans=0; 6 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int price[] = { 2, 30, 15, 10, 8, 25, 80 }; 7 | int n = 7; 8 | int profit = 0; 9 | 10 | for (int i = 1; i < n; i++) 11 | { 12 | int sub = price[i] - price[i - 1]; 13 | if (sub > 0) 14 | profit += sub; 15 | } 16 | 17 | cout << "Maximum Profit=" << profit; 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/02]. Matrix/_02)_2)_Search_a_2D_Matrix.cpp: -------------------------------------------------------------------------------- 1 | // Search a 2D Matrix 2 | // Link : https://leetcode.com/problems/search-a-2d-matrix/ 3 | 4 | // Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 5 | 6 | // Integers in each row are sorted from left to right. 7 | // The first integer of each row is greater than the last integer of the previous row. 8 | 9 | // Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 10 | // Output: true 11 | 12 | //Using binary_search STL 13 | bool searchMatrix(vector>& matrix, int target) { 14 | 15 | for(auto i : matrix) 16 | if(binary_search(i.begin(),i.end(),target)) 17 | return true; 18 | return false; 19 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/02]. Matrix/_07)_Find_a_Specific_Pair_in_Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define N 5 5 | 6 | int findMaxValue(int mat[][N]) 7 | { 8 | int maxValue = INT_MIN; 9 | 10 | for (int a = 0; a < N - 1; a++) 11 | for (int b = 0; b < N - 1; b++) 12 | for (int d = a + 1; d < N; d++) 13 | for (int e = b + 1; e < N; e++) 14 | if (maxValue < (mat[d][e] - mat[a][b])) 15 | maxValue = mat[d][e] - mat[a][b]; 16 | 17 | return maxValue; 18 | } 19 | 20 | int main() 21 | { 22 | int mat[N][N] = { 23 | { 1, 2, -1, -4, -20 }, 24 | { -8, -3, 4, 2, 1 }, 25 | { 3, 8, 6, 1, 3 }, 26 | { -4, -1, 1, 7, -6 }, 27 | { 0, -4, 10, -5, 1 } 28 | }; 29 | cout << "Maximum Value is " 30 | << findMaxValue(mat); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/02]. Matrix/_08)_Rotate_a_Matrix_by_90_Degree_in_Clockwise_without_Extra_Space.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define N 4 5 | 6 | void rotate90Clockwise(int arr[N][N]) 7 | { 8 | for (int j = 0; j < N; j++) 9 | { 10 | for (int i = N - 1; i >= 0; i--) 11 | cout << arr[i][j] << " "; 12 | cout << '\n'; 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int arr[N][N] = { { 1, 2, 3, 4 }, 19 | { 5, 6, 7, 8 }, 20 | { 9, 10, 11, 12 }, 21 | { 13, 14, 15, 16 } }; 22 | 23 | cout<<"Rotate a matrix by 90 degree: "<& s) { 14 | reverse(s.begin(), s.end()); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/03]. Strings/_01)_1_Reverse_a_String.cpp: -------------------------------------------------------------------------------- 1 | //<----------------> GFG Question Solution <---------------> 2 | 3 | //User function Template for C++ 4 | string reverseWord(string str){ 5 | 6 | int n = str.size(); 7 | 8 | for(int i=n; i>=0; i--){ 9 | cout< 2 | using namespace std; 3 | 4 | void printDups(string str) 5 | { 6 | map count; 7 | for (int i = 0; i < str.length(); i++) 8 | count[str[i]]++; 9 | 10 | for (auto it : count) 11 | if (it.second > 1) 12 | cout << it.first << ", count = " << it.second< 2 | using namespace std; 3 | 4 | bool areRotations(string str1, string str2) 5 | { 6 | if (str1.length() != str2.length()) 7 | return false; 8 | 9 | string temp = str1 + str1; 10 | return (temp.find(str2) != string::npos); 11 | } 12 | 13 | int main() 14 | { 15 | string str1 = "AACD", str2 = "ACDA"; 16 | if (areRotations(str1, str2)) 17 | cout<<"Strings are rotations of each other"; 18 | else 19 | cout<<"Strings are not rotations of each other"; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/03]. Strings/_06)_Check_String_is_valid_Suffle_of_two_different_Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void check_shuffle_of_2_string() 5 | { 6 | string a = "xy", b = "12"; 7 | string s2 = "1xy2"; 8 | int i = 0; 9 | int j = 0; 10 | for (auto& x : s2) 11 | { 12 | if (i < a.size() && x == a[i]) 13 | i++; 14 | 15 | else if (j < b.size() && x == b[j]) 16 | j++; 17 | } 18 | if (s2.size() == i + j && i == a.size() && j == b.size()) 19 | cout << "Yes" << endl; 20 | else 21 | cout << "No" << endl; 22 | } 23 | 24 | int main() { 25 | check_shuffle_of_2_string(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/04]. Searching & Sorting/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/04]. Searching & Sorting/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/05]. Linked List/01)_i_Write_a_Program_to_reverse_the_Linked_List.[Iterative_Solution].cpp: -------------------------------------------------------------------------------- 1 | //<----------> Iterative Solution <-------------------> 2 | 3 | #include 4 | class Solution 5 | { 6 | public: 7 | //Function to reverse a linked list. 8 | struct Node* reverseList(struct Node *head) 9 | { 10 | vector ans; 11 | 12 | struct Node* curr; 13 | 14 | if(head == NULL) return{}; 15 | for(curr = head; curr!=NULL; curr=curr->next){ 16 | ans.push_back(curr->data); 17 | } 18 | 19 | for(curr = head; curr!=NULL; curr=curr->next){ 20 | curr->data = ans.back(); 21 | ans.pop_back(); 22 | } 23 | return head; 24 | 25 | } 26 | 27 | }; 28 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/05]. Linked List/01)_ii_Write_a_Program_to_reverse_the_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | //<----------> LeetCode Question Solution <-------> 4 | 5 | class Solution { 6 | public: 7 | ListNode* reverseList(ListNode* head) { 8 | ListNode* curr = head; 9 | ListNode* prev = NULL; 10 | 11 | while(curr != NULL){ 12 | ListNode* n = curr->next; 13 | curr->next = prev; 14 | 15 | prev = curr; 16 | curr = n; 17 | } 18 | return prev; 19 | } 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/05]. Linked List/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/05]. Linked List/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/06]. Binary Tree/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/06]. Binary Tree/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/07]. Binary Search Tree/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/07]. Binary Search Tree/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/08]. Greedy/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/08]. Greedy/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/09]. Backtracking/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/09]. Backtracking/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/10]. Stack & Queue/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/10]. Stack & Queue/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/11]. Heap/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/11]. Heap/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/12]. Graph/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/12]. Graph/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/13]. Trie/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/13]. Trie/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/14]. Dynamic Programming/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/14]. Dynamic Programming/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/15]. Bit Manipulation/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/C++/15]. Bit Manipulation/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/01]. Array/_05)_MOve_all_neg_elements.java: -------------------------------------------------------------------------------- 1 | /*Move all the negative elements to one side of the array*/ 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class move_negandpos { 7 | public static void main (String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int arr[] = {1,9,-3,-5,-11,4,7}; 10 | int temp; 11 | for(int i=0; iarr[j]){ 14 | temp = arr[i]; 15 | arr[i] = arr[j]; 16 | arr[j] = temp; 17 | } 18 | } 19 | } 20 | for(int j=0; j0;i--){ 12 | arr[i] = arr[i-1]; 13 | } 14 | arr[0] = x; 15 | 16 | System.out.println("Rotates array is "+ Arrays.toString(arr)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/02]. Matrix/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/02]. Matrix/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/04]. Searching & Sorting/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/04]. Searching & Sorting/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/05]. Linked List/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/05]. Linked List/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/06]. Binary Tree/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/06]. Binary Tree/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/07]. Binary Search Tree/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/07]. Binary Search Tree/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/08]. Greedy/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/08]. Greedy/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/09]. Backtracking/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/09]. Backtracking/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/10]. Stack & Queue/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/10]. Stack & Queue/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/11]. Heap/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/11]. Heap/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/12]. Graph/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/12]. Graph/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/13]. Trie/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/13]. Trie/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/14]. Dynamic Programming/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/14]. Dynamic Programming/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_01)_0_Count_SetBit.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class CountSetBitsInInteger { 4 | 5 | static int countSetBits(int n) { 6 | int setBits = 0; 7 | while(n!=0) { 8 | setBits++; 9 | n&=(n-1); 10 | } 11 | return setBits; 12 | } 13 | 14 | public static void main(String[] args) { 15 | 16 | int n = 10; 17 | 18 | System.out.println(countSetBits(n)); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_01)_1_Count_SetBits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class CountSetBitsInInteger { 4 | 5 | static int countSetBits(int n) { 6 | int setBits = 0, bitMask = 1; 7 | for(int i=0 ; i<32 ; i++) { 8 | if((n & bitMask)!=0) { 9 | setBits++; 10 | } 11 | bitMask<<=1; 12 | } 13 | return setBits; 14 | } 15 | 16 | public static void main(String[] args) { 17 | 18 | int n = 10; 19 | 20 | System.out.println(countSetBits(n)); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_02)_0_Non_Repeating_Elements.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class NonRepeatingElement { 4 | 5 | static int[] twoNumbers(int a[]) { 6 | int xor = 0; 7 | for(int i : a) { 8 | xor^=i; 9 | } 10 | int mask = xor&~(xor-1); // least significant bit 11 | int first = 0, second = 0; 12 | for(int i : a) { 13 | if((i&mask)!=0) { 14 | first = first^i; 15 | } 16 | else { 17 | second = second^i; 18 | } 19 | } 20 | return new int[] {first,second}; 21 | } 22 | 23 | public static void main(String[] args) { 24 | int n = 2; 25 | int arr[] = {1, 2, 3, 2, 1, 4}; 26 | twoNumbers(arr); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_02)_1_Non_Repeating_Elements.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class NonRepeatingElement { 4 | 5 | static int[] twoNumbers(int a[]) { 6 | int xor = 0; 7 | for(int i : a) { 8 | xor^=i; 9 | } 10 | int mask = xor&(-xor); 11 | int first = 0; 12 | for(int i : a) { 13 | if((i&mask)!=0) { 14 | first^=i; 15 | } 16 | } 17 | return new int[] {first,(xor^first)}; 18 | } 19 | 20 | public static void main(String[] args) { 21 | int n = 2; 22 | int arr[] = {1, 2, 3, 2, 1, 4}; 23 | twoNumbers(arr); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_03)_Bits_To_Flip.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class BitsToFlip { 4 | 5 | static int bitsToFlip(int a, int b) { 6 | int xor = a^b; 7 | // After xor of two numbers, implement number of set bits in xor. 8 | int count = 0; //setbits 9 | while(xor != 0) { 10 | count++; 11 | xor&=(xor-1); 12 | } 13 | return count; // setbits 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | int A = 10; 19 | int B = 20; 20 | int ans = bitsToFlip(A,B); 21 | System.out.println(ans); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_04)_0_Total_SetBits_in_Range.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class TotalSetBits1toN { 4 | 5 | static int[] setBitsinRange(int n) { 6 | int ans[] = new int[n+1]; 7 | for(int i=1 ; i<=n ; i++) { 8 | ans[i] = ans[i&(i-1)] + 1 ; 9 | } 10 | return ans; 11 | } 12 | 13 | public static void main(String[] args) { 14 | int n = 5; 15 | System.out.println(setBitsinRange(n)); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_04)_1_Total_SetBits_in_Range.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class TotalSetBits1toN { 4 | 5 | static int SetBitsinRange(int n) { 6 | int ans[] = new int[n+1]; 7 | for(int i=0 ; i0 && (n&(n-1))==0) ? true : false ; 5 | } 6 | 7 | static int position(int N) { 8 | if(!isPowerOf2(N)) return -1; 9 | int num = 1, position = 1; 10 | while((num & N)==0) { 11 | num<<=1; 12 | position++; 13 | } 14 | return position; 15 | } 16 | 17 | public static void main(String[] args) { 18 | int n = 4; 19 | int ans = position(4); 20 | System.out.println(ans); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_07)_0_Copy_SetBits_From_X_to_Y.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class CopySetBitsFromXtoY { 4 | 5 | // One by One copy from x to y 6 | static void copySetBits(int x, int y, int l, int r) { 7 | if(l < 1 || r > 32) { 8 | return; 9 | } 10 | for(int i=l ; i<=r ; i++) { 11 | int mask = 1<<(i-1); 12 | if((y&mask)==1) { 13 | x = x|mask; 14 | } 15 | } 16 | } 17 | 18 | public static void main(String[] args) { 19 | int x = 10, y=13, l=2, r=3; 20 | 21 | copySetBits(x,y,l,r); 22 | 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_07)_1_Copy_SetBits_From_X_to_Y.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class CopySetBitsFromXtoY { 4 | 5 | // At once 6 | static void CopySetBits(int x, int y, int left, int right) { 7 | if(left < 1 || right > 32) { 8 | return; 9 | } 10 | int length = (1<<(right-left+1))-1; 11 | int mask = (length<<(left-1))&y; 12 | x=x|mask; 13 | } 14 | 15 | public static void main(String[] args) { 16 | int x = 10, y=13, l=2, r=3; 17 | 18 | CopySetBits(x,y,l,r); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_08)_Division.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class Division { 4 | 5 | static int divide(int dividend, int divisor) { 6 | if(dividend==Integer.MIN_VALUE && divisor==-1) { 7 | return Integer.MAX_VALUE; 8 | } 9 | int result = 0; 10 | int a = Math.abs(dividend); 11 | int b = Math.abs(divisor); 12 | for(int i=31 ; i>=0 ; i--) { 13 | if((a >>> i) - b >= 0) { 14 | result += 1< 0) == (divisor > 0) ? result : -result; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int a = 4, b = 2; 23 | System.out.println(divide(a,b)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_09)_Square_Of_a_Number.java: -------------------------------------------------------------------------------- 1 | public class SqaureOfNumber { 2 | 3 | static int sqaure(int n) { 4 | if(n == 0) return 0; 5 | if(n < 0) n = -n; 6 | int x = n >> 1; 7 | if(n % 2 != 0) { 8 | return ( (sqaure(x) << 2) + (x << 2) + 1); 9 | } 10 | else { 11 | return (sqaure(x) << 2); 12 | } 13 | } 14 | 15 | public static void main(String[] args) { 16 | int n = 2; 17 | System.out.println(sqaure(n)); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Java/15]. Bit Manipulation/_10)_0_Power_Set.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PowerSet { 4 | 5 | static List< List > subset (int a[]){ 6 | List< List > output = new ArrayList<>(); 7 | int n = a.length; 8 | for(int i=(int) Math.pow(2, n) ; i curr = new ArrayList<>(); 11 | for(int j=0 ; j > Subset (int a[]){ 6 | List< List > output = new ArrayList<>(); 7 | int n = a.length; 8 | int size = 1< curr = new ArrayList<>(); 11 | for(int j=0 ; j>j)&i)==1) { 13 | curr.add(a[j]); 14 | } 15 | } 16 | output.add(curr); 17 | } 18 | return output; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int a[] = {1,2,3}; 23 | 24 | System.out.println(Subset(a)); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/01]. Array/01_)_Reverse_Array.py: -------------------------------------------------------------------------------- 1 | def reverse(arr): 2 | """ 3 | This program simply reverse the given array 4 | Time Complexity : O(n) 5 | Space Complexity : O(1) 6 | """ 7 | return arr[::-1] 8 | 9 | 10 | print(reverse([1, 2, 3, 4])) 11 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/01]. Array/03_)_Kth_Max_Min_Element.py: -------------------------------------------------------------------------------- 1 | # Question link : https://practice.geeksforgeeks.org/problems/kth-smallest-element5635/1# 2 | 3 | def kth_min_element(arr, k): 4 | """ 5 | Time complexity : O(nlogn) 6 | Space Complexity : O(1) 7 | """ 8 | arr.sort() 9 | return arr[k - 1] 10 | 11 | 12 | print(kth_min_element([1, 2, 3, 4, 5], 3)) 13 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/01]. Array/06_)_Union_Of_Two_Array.py: -------------------------------------------------------------------------------- 1 | # Question link https://practice.geeksforgeeks.org/problems/union-of-two-arrays3538/1 2 | 3 | def union(arr1, arr2): 4 | """ 5 | Time Complexity : O(n) 6 | Space Complexity : O(n) 7 | """ 8 | return len(set(arr1 + arr2)) 9 | 10 | 11 | print(union([1, 2, 3, 4, 5], [1, 2, 3])) 12 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/01]. Array/07_)_Cyclically_Rotate_An_Array_By_One.py: -------------------------------------------------------------------------------- 1 | # Question link https://practice.geeksforgeeks.org/problems/cyclically-rotate-an-array-by-one2614/1 2 | 3 | """ 4 | INPUT : [1, 2, 3, 4, 5] 5 | OUTPUT : [5, 1, 2, 3, 4] 6 | Just rotate the array by one 7 | """ 8 | 9 | 10 | def rotate(arr): 11 | """ 12 | Time Complexity : O(n) 13 | Space Complexity : O(1) 14 | """ 15 | return arr.insert(0, arr.pop()) 16 | 17 | 18 | arr = [1, 2, 3, 4, 5] 19 | rotate(arr) 20 | print(arr) 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/01]. Array/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/01]. Array/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/02]. Matrix/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/02]. Matrix/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/03]. Strings/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/03]. Strings/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/04]. Searching & Sorting/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/04]. Searching & Sorting/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/05]. Linked List/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/05]. Linked List/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/06]. Binary Tree/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/06]. Binary Tree/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/07]. Binary Search Tree/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/07]. Binary Search Tree/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/08]. Greedy/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/08]. Greedy/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/10]. Stack & Queue/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/10]. Stack & Queue/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/11]. Heap/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/11]. Heap/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/12]. Graph/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/12]. Graph/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/13]. Trie/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/13]. Trie/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/14]. Dynamic Programming/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/14]. Dynamic Programming/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/15]. Bit Manipulation/Add Contents.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/3]. 450 DSA by Love Babbar/Python/15]. Bit Manipulation/Add Contents.txt -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/CodeStudio/2]. Pascal’s Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | vector> printPascal(int n) 3 | { 4 | // Write your code here. 5 | vector> mat(n); 6 | 7 | for(int i=0; i 2 | void sort012(int *arr, int n) 3 | { 4 | // Write your code here 5 | int low = 0, mid = 0, high = n-1; 6 | 7 | while(mid <= high) { 8 | switch(arr[mid]) { 9 | case 0: 10 | swap(arr[low++], arr[mid++]); 11 | break; 12 | case 1: 13 | mid++; 14 | break; 15 | case 2: 16 | swap(arr[mid], arr[high--]); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/CodeStudio/6]. Stock buy and Sell.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int maximumProfit(vector &prices){ 3 | // Write your code here. 4 | int buy = prices[0], res = 0; 5 | for(int i=1; i> Solution::solve(int A) { 2 | vector> mat(A); 3 | for(int i=0; i &A) { 2 | int curr_sum = A[0], max_sum = A[0]; 3 | for(int i=1; i max_sum) 7 | max_sum = curr_sum; 8 | } 9 | return max_sum; 10 | } 11 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/Interviewbit/5]. Sort an array of 0’s 1’s 2’s.cpp: -------------------------------------------------------------------------------- 1 | void Solution::sortColors(vector &A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int n = A.size(); 7 | int low = 0, mid = 0, high = n-1; 8 | 9 | while(mid <= high) { 10 | switch(A[mid]) { 11 | case 0: 12 | swap(A[low++], A[mid++]); 13 | break; 14 | case 1: 15 | mid++; 16 | break; 17 | case 2: 18 | swap(A[mid], A[high--]); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/Interviewbit/6]. Stock buy and Sell.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unmesh100/DSA-With-Unmesh/a4fcdbb2fa26951472a7558c62d34e3113c3ab8f/1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/Interviewbit/6]. Stock buy and Sell.cpp -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/Leetcode/2]. Pascal’s Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector> mat(numRows); 5 | 6 | for(int i=0; i& nums) 5 | { 6 | int curr_sum = nums[0], max_sum = nums[0]; 7 | for(int i=1; i max_sum) 11 | max_sum = curr_sum; 12 | } 13 | return max_sum; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/Leetcode/5]. Sort an array of 0’s 1’s 2’s.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | int n = nums.size(); 5 | int low = 0, mid = 0, high = n-1; 6 | 7 | while(mid <= high) { 8 | switch(nums[mid]) { 9 | case 0: 10 | swap(nums[low++], nums[mid++]); 11 | break; 12 | case 1: 13 | mid++; 14 | break; 15 | case 2: 16 | swap(nums[mid], nums[high--]); 17 | } 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/01]. Day-1 (Arrays)/Leetcode/6]. Stock buy and Sell.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxProfit(vector& prices) 5 | { 6 | int buy=prices[0], res=0; 7 | for(int i=1;i 2 | vector ninjaAndSortedArrays(vector& arr1, vector& arr2, int m, int n) { 3 | // Write your code here. 4 | int l=m-1, r=n-1, k=m+n-1; 5 | while(l>=0 and r>=0) { 6 | if(arr1[l] > arr2[r]) 7 | arr1[k--] = arr1[l--]; 8 | else 9 | arr1[k--] = arr2[r--]; 10 | } 11 | while(l>=0) 12 | arr1[k--] = arr1[l--]; 13 | while(r>=0) 14 | arr1[k--] = arr2[r--]; 15 | return arr1; 16 | } 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/02]. Day-2 (Arrays)/CodeStudio/4]. Find the duplicate in an array of N+1 integers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int findDuplicate(vector &arr, int n){ 3 | // Write your code here. 4 | int slow = arr[0], fast = arr[0]; 5 | do { 6 | slow = arr[slow]; 7 | fast = arr[arr[fast]]; 8 | } while (slow != fast); 9 | 10 | fast = arr[0]; 11 | while(slow != fast) { 12 | slow = arr[slow]; 13 | fast = arr[fast]; 14 | } 15 | return slow; 16 | } 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/02]. Day-2 (Arrays)/Leetcode/1]. Rotate Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int row = matrix.size(); 5 | for(int i=0; i> merge(vector>& intervals) { 4 | sort(intervals.begin(), intervals.end()); 5 | vector> res; 6 | 7 | for(int i=0; i v = {intervals[i][0], intervals[i][1]}; 10 | res.push_back(v); 11 | } 12 | else { 13 | res.back()[1] = max(res.back()[1], intervals[i][1]); 14 | } 15 | } 16 | return res; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/02]. Day-2 (Arrays)/Leetcode/3]. Merge two sorted Arrays without extra space.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | int l=m-1, r=n-1, k=m+n-1; 5 | while(l>=0 and r>=0) { 6 | if(nums1[l] > nums2[r]) 7 | nums1[k--] = nums1[l--]; 8 | else 9 | nums1[k--] = nums2[r--]; 10 | } 11 | while(l>=0) 12 | nums1[k--] = nums1[l--]; 13 | while(r>=0) 14 | nums1[k--] = nums2[r--]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/02]. Day-2 (Arrays)/Leetcode/4]. Find the duplicate in an array of N+1 integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int slow = nums[0], fast = nums[0]; 5 | do { 6 | slow = nums[slow]; 7 | fast = nums[nums[fast]]; 8 | } while (slow != fast); 9 | 10 | fast = nums[0]; 11 | while(slow != fast) { 12 | slow = nums[slow]; 13 | fast = nums[fast]; 14 | } 15 | return slow; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/CodeStudio/1]. Search in a 2d Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool findTargetInMatrix(vector < vector < int >> & mat, int m, int n, int target) { 5 | // Write your code here. 6 | int left = 0, right = m*n-1; 7 | while(left != right) { 8 | int mid = (left + (right - left)/2); 9 | if(mat[mid/n][mid%n] < target) 10 | left = mid+1; 11 | else 12 | right = mid; 13 | } 14 | return mat[right/n][right%n] == target; 15 | } 16 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/CodeStudio/2]. Pow(X,n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int modularExponentiation(int x, int n, int m) { 3 | // Write your code here. 4 | long long res = 1, x1 = x; 5 | while(n) { 6 | if(n%2 != 0) { 7 | res = (res*x1%m)%m; 8 | n--; 9 | } 10 | else { 11 | x1 = (x1%m * x1%m)%m; 12 | n = n/2; 13 | } 14 | } 15 | return res%m; 16 | } 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/CodeStudio/3]. Majority Element (N-2 times).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int findMajorityElement(int arr[], int n) { 3 | // Write your code here. 4 | int majority=0, count=0; 5 | 6 | for(int i=0; i 2 | int uniquePaths(int m, int n) { 3 | // Write your code here. 4 | int N = m+n-2; 5 | int r = m-1; 6 | double res = 1; 7 | 8 | for(int i=1; i<=r; i++) 9 | res = res*(N-r+i)/i; 10 | return (int)res; 11 | } 12 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/Leetcode/1]. Search in a 2d Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int row = matrix.size(); 5 | int col = matrix[0].size(); 6 | int left = 0, right = row*col - 1; 7 | while (left != right){ 8 | int mid = (left + (right-left)/2); 9 | if (matrix[mid / col][mid % col] < target) 10 | left = mid + 1; 11 | else 12 | right = mid; 13 | } 14 | return matrix[right / col][right % col] == target; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/Leetcode/2]. Pow(X,n).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | double res = 1; 5 | long long nn = n; 6 | if(nn < 0) 7 | nn = -1*nn; 8 | while(nn) { 9 | if(nn%2 != 0) { 10 | res = res*x; 11 | nn--; 12 | } 13 | else { 14 | x = x*x; 15 | nn = nn/2; 16 | } 17 | } 18 | if(n<0) 19 | res = 1/res; 20 | return res; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/Leetcode/3]. Majority Element (N-2 times).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | int majority = 0, count = 0; 5 | 6 | for(int i = 0; i < nums.size(); i++){ 7 | if(count == 0) 8 | majority = nums[i]; 9 | if(majority == nums[i]) 10 | count++; 11 | else 12 | count--; 13 | } 14 | return majority; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/03]. Day-3 (Arrays)/Leetcode/5]. Grid Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | int N = m+n-2; 5 | int r = m-1; 6 | double res = 1; 7 | 8 | for(int i=1; i<=r; i++) 9 | res = res*(N-r+i)/i; 10 | return (int)res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/04]. Day-4 (Arrays)/CodeStudio/1]. 2-Sum-Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | vector> pairSum(vector &arr, int s){ 3 | // Write your code here. 4 | vector res; 5 | vector> r; 6 | unordered_map seen; 7 | int n = arr.size(); 8 | for(int i=0; isecond; 11 | while(temp--) { 12 | res.push_back(s-arr[i]); 13 | res.push_back(arr[i]); 14 | sort(res.begin(), res.end()); 15 | r.push_back(res); 16 | res.clear(); 17 | } 18 | } 19 | seen[arr[i]]++; 20 | } 21 | sort(r.begin(), r.end()); 22 | return r; 23 | } 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/04]. Day-4 (Arrays)/CodeStudio/3]. Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int lengthOfLongestConsecutiveSequence(vector &arr, int n) { 3 | // Write your code here. 4 | unordered_set num; 5 | int ans = 0; 6 | 7 | for(int i : arr) { 8 | num.insert(i); 9 | } 10 | 11 | for(int i : arr) { 12 | if(!num.count(i-1)) { 13 | int count = 1; 14 | while(num.count(i+count)) { 15 | count++; 16 | } 17 | ans = max(ans, count); 18 | } 19 | } 20 | return ans; 21 | } 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/04]. Day-4 (Arrays)/CodeStudio/4]. Largest Subarray with 0 sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int LongestSubsetWithZeroSum(vector < int > arr) { 3 | // Write your code here 4 | int n = arr.size(); 5 | unordered_map mp; 6 | int sum = 0; 7 | int maxi = 0; 8 | for(int i = 0 ;i 2 | int subarraysXor(vector &arr, int x) 3 | { 4 | // Write your code here. 5 | map mp; 6 | int xr =0 , count = 0; 7 | for(int i=0;i 2 | int uniqueSubstrings(string input) 3 | { 4 | //Write your code here 5 | vector last_seen(256, -1); 6 | int start = 0, longest = 0, n = input.size(); 7 | 8 | for(int i=0; i &A, int B) { 2 | map mp; 3 | int xr =0 , count = 0; 4 | for(int i=0;i twoSum(vector& nums, int target) 5 | { 6 | unordered_map seen; 7 | for(int i=0; i& nums) { 4 | unordered_set num; 5 | int ans = 0; 6 | 7 | for(int i : nums) { 8 | num.insert(i); 9 | } 10 | 11 | for(int i : nums) { 12 | if(!num.count(i-1)) { 13 | int count = 1; 14 | while(num.count(i+count)) { 15 | count++; 16 | } 17 | ans = max(ans, count); 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/04]. Day-4 (Arrays)/Leetcode/6]. Longest Substring without repeat.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | vector last_seen(256, -1); 5 | int start =0, longest = 0, n = s.size(); 6 | 7 | for(int i=0; i 2 | /**************************************************************** 3 | 4 | Following is the class structure of the LinkedListNode class: 5 | 6 | template 7 | class LinkedListNode 8 | { 9 | public: 10 | T data; 11 | LinkedListNode *next; 12 | LinkedListNode(T data) 13 | { 14 | this->data = data; 15 | this->next = NULL; 16 | } 17 | }; 18 | 19 | *****************************************************************/ 20 | 21 | void deleteNode(LinkedListNode * node) { 22 | // Write your code here. 23 | *node = *node->next; 24 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/05]. Day-5 (Linked List)/Leetcode/1]. Reverse a LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseList(ListNode* head) { 14 | ListNode *dummynode = NULL; 15 | while(head != NULL){ 16 | ListNode *nextt = head->next; 17 | head->next = dummynode; 18 | dummynode = head; 19 | head = nextt; 20 | } 21 | return dummynode; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/05]. Day-5 (Linked List)/Leetcode/2]. Find the middle of LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* middleNode(ListNode* head) { 14 | ListNode *slow = head, *fast = head; 15 | while(fast and fast->next) { 16 | slow = slow->next; 17 | fast = fast->next->next; 18 | } 19 | return slow; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/05]. Day-5 (Linked List)/Leetcode/6]. Delete a given Node when a node is given.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | *node = *node->next; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/06]. Day-6 (Linked List)/Leetcode/1]. Find intersection point of Y LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 12 | ListNode *cur1 = headA, *cur2 = headB; 13 | while(cur1 != cur2){ 14 | cur1 = cur1?cur1->next:headB; 15 | cur2 = cur2?cur2->next:headA; 16 | } 17 | return cur1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/06]. Day-6 (Linked List)/Leetcode/2]. Detect a cycle in Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | if(head == NULL) 13 | return false; 14 | 15 | ListNode *slow = head; 16 | ListNode *fast = head; 17 | 18 | while(fast->next != NULL and fast->next->next != NULL) { 19 | slow = slow->next; 20 | fast = fast->next->next; 21 | if(slow == fast) 22 | return true; 23 | } 24 | return false; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/06]. Day-6 (Linked List)/Leetcode/4]. Check if a LinkedList is palindrome or not.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* temp; 14 | bool isPalindrome(ListNode* head) { 15 | temp = head; 16 | return check(head); 17 | } 18 | 19 | bool check(ListNode* p) { 20 | if (NULL == p) 21 | return true; 22 | bool isPal = check(p->next) & (temp->val == p->val); 23 | temp = temp->next; 24 | return isPal; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/07]. Day-7 (Linked List and Arrays)/CodeStudio/4]. Trapping rainwater.cpp: -------------------------------------------------------------------------------- 1 | long getTrappedWater(long *a, int n){ 2 | // Write your code here. 3 | long l = 0, r = n-1, level = 0, water = 0; 4 | while (l < r) { 5 | long lower = a[a[l] < a[r] ? l++ : r--]; 6 | level = max(level, lower); 7 | water += level - lower; 8 | } 9 | return water; 10 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/07]. Day-7 (Linked List and Arrays)/CodeStudio/5]. Remove Duplicate from Sorted array.cpp: -------------------------------------------------------------------------------- 1 | int removeDuplicates(vector &arr, int n) { 2 | // Write your code here. 3 | if(arr.size() == 0) 4 | return 0; 5 | int m = 1; 6 | for(int i =1; i < arr.size(); i++){ 7 | if(arr[i] > arr[m-1]){ 8 | arr[m] = arr[i]; 9 | m++; 10 | } 11 | } 12 | return m; 13 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/07]. Day-7 (Linked List and Arrays)/CodeStudio/6]. Max consecutive ones.cpp: -------------------------------------------------------------------------------- 1 | int consecutiveOnes(vector& arr){ 2 | //Write your code here. 3 | int total = 0; 4 | int res = 0; 5 | 6 | for(auto n : arr) { 7 | if(n == 1) { 8 | total++; 9 | res = max(res, total); 10 | } 11 | else { 12 | total = 0; 13 | } 14 | } 15 | return res; 16 | } 17 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/07]. Day-7 (Linked List and Arrays)/Leetcode/4]. Trapping rainwater.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int l = 0, r = height.size()-1, level = 0, water = 0; 5 | while (l < r) { 6 | int lower = height[height[l] < height[r] ? l++ : r--]; 7 | level = max(level, lower); 8 | water += level - lower; 9 | } 10 | return water; 11 | } 12 | }; -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/07]. Day-7 (Linked List and Arrays)/Leetcode/5]. Remove Duplicate from Sorted array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | if(nums.size() == 0) 5 | return 0; 6 | int m=1; 7 | for(int i=1; i nums[m-1]){ 9 | nums[m] = nums[i]; 10 | m++; 11 | } 12 | } 13 | return m; 14 | } 15 | }; -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/07]. Day-7 (Linked List and Arrays)/Leetcode/6]. Max consecutive ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector& nums) { 4 | int total = 0; 5 | int res = 0; 6 | 7 | for(auto n : nums) { 8 | if(n == 1) { 9 | total++; 10 | res = max(res, total); 11 | } 12 | else { 13 | total = 0; 14 | } 15 | } 16 | return res; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/08]. Day-8 (Greedy Algorithm)/CodeStudio/2]. Minimum number of platforms required for a railway.cpp: -------------------------------------------------------------------------------- 1 | int calculateMinPatforms(int at[], int dt[], int n) { 2 | // Write your code here. 3 | sort(at,at+n); 4 | sort(dt,dt+n); 5 | int j=0; 6 | int ans=0; 7 | 8 | for(int i=0;i=0;i--){ 7 | if(amount/arr[i]>=1){ 8 | mincoins+=amount/arr[i]; 9 | amount=amount%arr[i]; 10 | } 11 | if(amount==0) break; 12 | } 13 | return mincoins; 14 | } 15 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/08]. Day-8 (Greedy Algorithm)/CodeStudio/6]. Activity Selection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct job{ 4 | int start; 5 | int end; 6 | }; 7 | bool comp(struct job a, struct job b){ 8 | return (a.end < b.end); 9 | } 10 | 11 | int maximumActivities(vector &start, vector &finish) { 12 | // Write your code here. 13 | int n=start.size(); 14 | struct job v[n]; 15 | 16 | for(int i=0; i= end){ 26 | end = v[i].end; 27 | ans++; 28 | } 29 | } 30 | return ans; 31 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/09]. Day-9 (Recursion)/CodeStudio/1]. Subset Sums.cpp: -------------------------------------------------------------------------------- 1 | void func(int index,int sum,vectorarr,vector &sumOfSubsets){ 2 | 3 | if(index==arr.size()){ 4 | sumOfSubsets.push_back(sum); 5 | return; 6 | } 7 | func(index+1,sum+arr[index],arr,sumOfSubsets); 8 | func(index+1,sum,arr,sumOfSubsets); 9 | } 10 | 11 | vector subsetSum(vector &arr) 12 | { 13 | // Write your code here 14 | vectorsumOfSubsets; 15 | func(0,0,arr,sumOfSubsets); 16 | sort(sumOfSubsets.begin(),sumOfSubsets.end()); 17 | return sumOfSubsets; 18 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/09]. Day-9 (Recursion)/CodeStudio/2]. Subset-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void fun(int i,vectorarr,vector&subset,vector>&ans) 3 | { 4 | if(i==arr.size()) 5 | { 6 | ans.push_back(subset); 7 | return; 8 | } 9 | subset.push_back(arr[i]); 10 | fun(i+1,arr,subset,ans); 11 | subset.pop_back(); 12 | while(i+1 < arr.size() && arr[i]==arr[i+1]) 13 | i++; 14 | fun(i+1,arr,subset,ans); 15 | } 16 | vector> uniqueSubsets(int n, vector &arr) 17 | { 18 | // Write your code here. 19 | sort(arr.begin(),arr.end()); 20 | vector>ans; 21 | vectorsubset; 22 | fun(0,arr,subset,ans); 23 | sort(ans.begin(),ans.end()); 24 | return ans; 25 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/09]. Day-9 (Recursion)/CodeStudio/3]. Combination sum-1.cpp: -------------------------------------------------------------------------------- 1 | void Sum(vector &arr,int n,int i,int k, vector &v,vector>&ans) { 2 | if(k==0) { 3 | ans.push_back(v); 4 | } 5 | if(i>=n) return ; 6 | for(;i> findSubsetsThatSumToK(vector arr, int n, int k) 14 | { 15 | vector> ans; 16 | vector v; 17 | Sum(arr,n,0,k,v,ans); 18 | return ans; 19 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/09]. Day-9 (Recursion)/CodeStudio/6]. K-th permutation Sequence.cpp: -------------------------------------------------------------------------------- 1 | string kthPermutation(int n, int k) { 2 | // Write your code here. 3 | vectorarr; 4 | string ans=""; 5 | int fact=1; 6 | 7 | for(int i=1;iarr; 5 | string ans=""; 6 | int fact=1; 7 | 8 | for(int i=1;i&ans) 2 | { 3 | if(index==s.size()) 4 | { 5 | ans.push_back(s); 6 | return; 7 | } 8 | for(int i=index;i findPermutations(string &s) { 17 | // Write your code here. 18 | vectorans; 19 | int index=0; 20 | findpermutations(index,s,ans); 21 | return ans; 22 | } -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/10]. Day-10 (Recursion and Backtracking)/Leetcode/1]. Print all permutations of a string-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void permuteRecursive(vector &num, int begin, vector > &result) { 4 | if (begin >= num.size()) { 5 | result.push_back(num); 6 | return; 7 | } 8 | 9 | for (int i = begin; i < num.size(); i++) { 10 | swap(num[begin], num[i]); 11 | permuteRecursive(num, begin + 1, result); 12 | swap(num[begin], num[i]); 13 | } 14 | } 15 | 16 | vector> permute(vector& num) { 17 | vector> result; 18 | permuteRecursive(num, 0, result); 19 | return result; 20 | } 21 | }; -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/11]. Day-11 (Binary Search)/CodeStudio/1]. The N-th root of an integer.cpp: -------------------------------------------------------------------------------- 1 | double findNthRootOfM(int n, long long m) { 2 | // Write your code here. 3 | double t=1.000000/n; 4 | double ans=pow(m,t); 5 | return ans; 6 | } 7 | -------------------------------------------------------------------------------- /1]. DSA + CP/4]. Striver Series/30 Days of SDE Sheet/C++/11]. Day-11 (Binary Search)/CodeStudio/2]. Matrix Median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int getMedian(vector> &matrix) 3 | { 4 | // Write your code here. 5 | vector ans; 6 | for(int i=0;i